944,142 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3922
  • C++ RSS
Jan 12th, 2007
0

Setprecision help please

Expand Post »
Hi, I am trying to use a command setprecision from the <iomanip.h> header.

It works well when i declare the number of decimals to display to the right of the decimal point, however i am attempting to have the user input how many places of precision they want. So far it will only out put about 15 characters on the last output no matter what my input is...

I am new to c++ and any help would be awesome.
Thanks
-Scott

  1.  
  2. #include <iostream.h>
  3. #include <iomanip.h>
  4. int main()
  5. {
  6. float x,y;
  7. cout << "Enter two float-point number (Example: 2.415 1.312)\n";
  8. cin >> x >> y;
  9.  
  10. float z=x*y;
  11. cout << z << '\n';
  12. cin.ignore(50, '\n');
  13. cout << "how many digits do you want to display to the right of the decimal point?\n";
  14.  
  15. char q;
  16. cin >> q;
  17. cin.ignore(50, '\n');
  18. cout << setprecision (q) << z << endl;
  19. system("PAUSE");
  20. return 0;
  21. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
killerdrummer2 is offline Offline
4 posts
since Jul 2006
Jan 12th, 2007
0

Re: Setprecision help please

Don't use the old and outdated header files - instead, use iostream and iomanip:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;

Secondly, your code isn't going to work as expected because you're inputting the amount as a char. char does happen to be an int, but if you look at an ascii table, you'll find that '1' is not equal to the actual value 1. Thus, you are giving setprecision() far larger values than you expect.

Change 'q' to an int, which should fix your problem. Better yet, avoid the cin >> method and use cin.getline() and write your own parser, which although more lengthy is more robust and avoids problems like a \n left in the stream after reading. Don't worry about doing this now, as your program is relatively small.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Jan 12th, 2007
0

Re: Setprecision help please

A few points:
  • Dont use the old style of header inclusion (ie the .h one). Include your headers as
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std ;
  • Don't use system("pause") to pause your program. Its a non portable way and takes way too much of resources to just pause the program (OS calls and all that). Better use cin.get() to pause for user input.
  • Any specific reason why you have chosen q to be character type ? That's where your problem lies. Even if the user enters 1, it would be taken as '1' (character 1) and its decimal equivalent would be 49. So in the end, you set the stream precision to 49.
  • Change the data type of q from char to int or something like that and you should be fine.
Hope it helped, bye.

PS: Grr..Joey beat me to it.
Last edited by ~s.o.s~; Jan 12th, 2007 at 10:32 pm. Reason: Lost a battle ;)
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Jan 15th, 2007
0

Re: Setprecision help please

Good for your compiler! The stored value of a floating point number has a limited precision, probably 50 or 60 bits. This is equivalent to a certain number of decimal digits, say about 15. If the computer were to give you a 20 decimal digit answer the last few digits would be random garbage. Your problem isn't reading in the precision. In fact, if you set the precision to 50 in the program you'd get the same result.
Reputation Points: 21
Solved Threads: 1
Junior Poster in Training
murschech is offline Offline
60 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: Short code - it works on VC++ Express but not on Bloodshed!!
Next Thread in C++ Forum Timeline: MyString Woes





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


Follow us on Twitter


© 2011 DaniWeb® LLC