![]() |
| ||
| Setprecision help please 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
|
| ||
| Re: Setprecision help please Don't use the old and outdated header files - instead, use iostream and iomanip: #include <iostream> 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. |
| ||
| Re: Setprecision help please A few points:
#include <iostream>
PS: Grr..Joey beat me to it. |
| ||
| 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. |
| All times are GMT -4. The time now is 5:12 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC