943,929 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 9631
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 29th, 2004
0

Floating point numbers

Expand Post »
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
Similar Threads
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Dec 29th, 2004
0

Re: Floating point numbers

Quote originally posted by Acidburn ...
but now im wondering if you could display just + or - depending on what is entered.
If it's less than zero, print '-'; otherwise, print '+'.

Quote originally posted by Acidburn ...
is it possible to put the float which is entered into an array?
Of course.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. float value[3];
  6. int i;
  7. for ( i = 0; i < 3; ++i )
  8. {
  9. std::cout << "Please input your number with the sign: ";
  10. std::cin >> value[i]; // put user-entered value into an array element
  11. if ( value[i] < 0 )
  12. {
  13. std::cout << "-" << '\n';
  14. }
  15. else
  16. {
  17. std::cout << "+" << '\n';
  18. }
  19. }
  20. return 0;
  21. }
  22.  
  23. /* my input/output
  24. Please input your number with the sign: 1458.125478
  25. +
  26. Please input your number with the sign: -2.3
  27. -
  28. Please input your number with the sign: 0
  29. +
  30. */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 29th, 2004
0

Re: Floating point numbers

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
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Dec 29th, 2004
0

Re: Floating point numbers

>could you explain that bit a little more so i can try and follow whats going on.
C++ Syntax (Toggle Plain Text)
  1. float value[3]; // an array of 3 floats
  2. int i; // a loop counter
  3. for ( i = 0; i < 3; ++i ) // loop 3 times
I can't imagine you're having trouble with that.
C++ Syntax (Toggle Plain Text)
  1. cin >> value[i]; // put user-entered value into an array element
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)
  1. if ( value[i] < 0 )
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 29th, 2004
0

Re: Floating point numbers

ok im going back to my book! I should of known how to do that and i cant belive it was that easy...
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Dec 30th, 2004
0

Re: Floating point numbers

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
happyHour is offline Offline
12 posts
since Dec 2004
Dec 30th, 2004
0

Re: Floating point numbers

Quote 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
crude but effective idea would be to change the string to an int. Why well with my little depth of c++ itsall i can think of ... But think about it for a moment you convert it to an int ... What can an int hold?? 4 bytes of real numbers... Whole real numbers! so in feect you'd loose your decimal point. All you have to do then is to take away the number left in the string with the one you created - Hence leaving you with the decimal parts.. .Note that you won't get the 0. xxxxxx Not sure how that works ...

Nor could i code this without research , have a go if your still struggling the forum is always here
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Dec 30th, 2004
0

Re: Floating point numbers

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 31st, 2004
0

Re: Floating point numbers

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

C++ Syntax (Toggle Plain Text)
  1. cout.flags( options ... )
  2. or
  3. 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...
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Jan 1st, 2005
0

Re: Floating point numbers

Thanks for the replies, just wondering if you can split up a string of numbers? ... say all the numbers before the decimal point...into string1
and all the others after the decimal point into string 2?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
happyHour is offline Offline
12 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: memory
Next Thread in C++ Forum Timeline: Borland C++ vr 4.5





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC