Setprecision help please

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2006
Posts: 4
Reputation: killerdrummer2 is an unknown quantity at this point 
Solved Threads: 0
killerdrummer2 killerdrummer2 is offline Offline
Newbie Poster

Setprecision help please

 
0
  #1
Jan 12th, 2007
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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Setprecision help please

 
0
  #2
Jan 12th, 2007
Don't use the old and outdated header files - instead, use iostream and iomanip:
  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.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Setprecision help please

 
0
  #3
Jan 12th, 2007
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 ;)
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 60
Reputation: murschech is an unknown quantity at this point 
Solved Threads: 1
murschech murschech is offline Offline
Junior Poster in Training

Re: Setprecision help please

 
0
  #4
Jan 15th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC