954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

mixing floats and ints...(don't ban me please for being such a noob)

okay, i know i might be boring with my questions but could someone tell me how can i read an array of floats mixed with ints and then print them out same as they were on input...?
typecasting thing again? anyone knows a good book i could use to stop torturing this forum, i got the c++ primer but it aint helpful at all. and i don't wanna hear B from Bjarne...

wellibedamned
Light Poster
33 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

you noob. you're banned.

just kidding.

but seriously, you cant have a single array of floats "mixed with" ints.

they're all either one or the other. probably floats. some of the floats just happen to not have any fractional part.

i'm not sure i fully understand your question... you want to print them out exactly as entered? i guess theres a number of ways you could do this.

one way would be to input them as strings, and store them in a string array, then convert them to the appropriate value when needed. ... or, along those lines, a structure where you have the "as enetered" string element, along with a floating point "value" element

is this making sense? is this relevant to your question?


.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

Yup, a string was what i thought, but i wondered if i could have an array of floats that i can print out as integers if they don't have fractional parts. and if i could print a float to the last digit, without zeros at the end, for example if the input is 1.6 i print 1.6, not 1.60000

wellibedamned
Light Poster
33 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

well, you can use format specifiers

"%.3f" for instance will print up to 3 decimal places but only if they are non-zero digits.

"%.03f" will always print 3 decimal places even if they are zero digits.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 
you cant have a single array of floats "mixed with" ints


You can have an array with different data types, you just need to point to them using void*

example:

void *array[] = {(int*)123, (char*)"Hello"};
cout << (int)array[0]; // prints 123
cout << (char*)array[1]; // prints "Hello"
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 

aha. i knew someone was going to call me on that.

:P

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You