| | |
Floating point numbers
![]() |
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
i got my program to do stuff, but i wondered if i can take it to the next level...
when a user enters a number of 1458.125478 i got it to do abs.... google is soo good!! but now im wondering if you could display just + or - depending on what is entered. I tried creating an int for the sign to go into but it just doesnt want to know!
then i thought about a string so i went down that route not knowing that you cant combine a string and a float together!
so i had another idea
[php]
int main(){
float n; //n is the number entered
cout << " please input your number with the sign";
cin >> n; // prints everything
cout << n<<endl;
return 0;
}
[/php]
is it possible to put the float which is entered into an array? that way i can chose what is printed out on the screen?
... unless anyone else has other ideas which i welcome
when a user enters a number of 1458.125478 i got it to do abs.... google is soo good!! but now im wondering if you could display just + or - depending on what is entered. I tried creating an int for the sign to go into but it just doesnt want to know!
then i thought about a string so i went down that route not knowing that you cant combine a string and a float together!
so i had another idea
[php]
int main(){
float n; //n is the number entered
cout << " please input your number with the sign";
cin >> n; // prints everything
cout << n<<endl;
return 0;
}
[/php]
is it possible to put the float which is entered into an array? that way i can chose what is printed out on the screen?
... unless anyone else has other ideas which i welcome
•
•
•
•
Originally Posted by Acidburn
but now im wondering if you could display just + or - depending on what is entered.
•
•
•
•
Originally Posted by Acidburn
is it possible to put the float which is entered into an array?
C++ Syntax (Toggle Plain Text)
#include <iostream> int main() { float value[3]; int i; for ( i = 0; i < 3; ++i ) { std::cout << "Please input your number with the sign: "; std::cin >> value[i]; // put user-entered value into an array element if ( value[i] < 0 ) { std::cout << "-" << '\n'; } else { std::cout << "+" << '\n'; } } return 0; } /* my input/output Please input your number with the sign: 1458.125478 + Please input your number with the sign: -2.3 - Please input your number with the sign: 0 + */
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
float value[3];
int i;
for ( i = 0; i < 3; ++i )
{
cout << "Please input your number with the sign: ";
cin >> value[i]; // put user-entered value into an array element
if ( value[i] < 0 )
could you explain that bit a little more so i can try and follow whats going on. From my interpration
you got a float thats an array - true?
you print out the array using the loop i
but one thing i dont get is how do you stop it printing the rest out ? just the sign
int i;
for ( i = 0; i < 3; ++i )
{
cout << "Please input your number with the sign: ";
cin >> value[i]; // put user-entered value into an array element
if ( value[i] < 0 )
could you explain that bit a little more so i can try and follow whats going on. From my interpration
you got a float thats an array - true?
you print out the array using the loop i
but one thing i dont get is how do you stop it printing the rest out ? just the sign
>could you explain that bit a little more so i can try and follow whats going on. I can't imagine you're having trouble with that. Is this comment confusing?
>you print out the array using the loop i
No. I print a plus or minus. I do so depending on whether the value entered is less than zero.
C++ Syntax (Toggle Plain Text)
float value[3]; // an array of 3 floats int i; // a loop counter for ( i = 0; i < 3; ++i ) // loop 3 times
C++ Syntax (Toggle Plain Text)
cin >> value[i]; // put user-entered value into an array element
>you print out the array using the loop i
No. I print a plus or minus. I do so depending on whether the value entered is less than zero.
C++ Syntax (Toggle Plain Text)
if ( value[i] < 0 )
•
•
Join Date: Dec 2004
Posts: 12
Reputation:
Solved Threads: 0
Intresting topic we have here, I'm wondering if theres a away to just print out the values after the decimal point.
This is what I thought -
If you entered it into a string you'd have some for loop that finds the decimal point and then from their print everything out google has been unsucessful at finding any information regarding this matter
This is what I thought -
If you entered it into a string you'd have some for loop that finds the decimal point and then from their print everything out google has been unsucessful at finding any information regarding this matter
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
•
•
•
•
Originally Posted by happyHour
Intresting topic we have here, I'm wondering if theres a away to just print out the values after the decimal point.
This is what I thought -
If you entered it into a string you'd have some for loop that finds the decimal point and then from their print everything out google has been unsucessful at finding any information regarding this matter
Nor could i code this without research , have a go if your still struggling the forum is always here
>I'm wondering if theres a away to just print out the values after the decimal point.
There's a library function in <cmath> called modf that breaks up a floating-point value into its constituent pieces. Alternatively, you could copy the value to an int (which truncates the fractional part) and subtract that from the original floating-point value. Either way, the result is the fractional part of the floating-point value, and you still have access to the whole part.
There's a library function in <cmath> called modf that breaks up a floating-point value into its constituent pieces. Alternatively, you could copy the value to an int (which truncates the fractional part) and subtract that from the original floating-point value. Either way, the result is the fractional part of the floating-point value, and you still have access to the whole part.
I'm here to prove you wrong.
I have a feeling that in the iostream classes there is a formatting option to set the precision and decimal places. I have never used them myself so i couldnt tell you what they were but a google search should reveal it.
I think it was something along the lines of
If you cant find it try searching for a hexadecimal number tutorial as hex is a formatting flag which tells cout to display numbers in hexadecimal.... as far as i remember...
I think it was something along the lines of
C++ Syntax (Toggle Plain Text)
cout.flags( options ... ) or cout << options... << text
If you cant find it try searching for a hexadecimal number tutorial as hex is a formatting flag which tells cout to display numbers in hexadecimal.... as far as i remember...
http://sales.carina-e.com
no www
no nonsense
coming soon to a pc near you! :cool:
no www
no nonsense
coming soon to a pc near you! :cool:
![]() |
Similar Threads
- Floating point numbers (Assembly)
- Assembly floating point (Assembly)
- Fixed to Floating point conversions (C)
- Floating point numbers (C)
- Dynamic Array, Writing to CSV, Floating Point ?'s (C)
- IEEE Floating-point fromat (C)
Other Threads in the C++ Forum
- Previous Thread: memory
- Next Thread: Borland C++ vr 4.5
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph guess gui homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






