decimal - How to express large numbers to the hundredth place in C++ Calculator -


i trying write calculator in c++ basic functions of /, *, -, or + , show answer hundredth decimal place (0.01). example 100.1 * 100.1 should print result 10020.01 instead -4e-171. understanding overflow that's why choose long double.

#include <iostream> #include <iomanip> using namespace std;  long double getuserinput() {     cout << "please enter number: \n";     long double x;     cin >> x;     return x; }  char getmathematicaloperation() {     cout << "please enter operator want (add +, subtract -, multiply *, or divide /): \n";     char o;     cin >> o;     return o; }  long double calculateresult(long double nx, char o, long double ny) { // note: use == operator compare 2 values see if equal // need use if statements here because there's no direct way convert choperation appropriate operator  if (o == '+') // if user chose addition     return nx + ny; // execute line if (o == '-') // if user chose subtraction     return nx - ny; // execute line if (o == '*') // if user chose multiplication     return nx * ny; // execute line if (o == '/') // if user chose division     return nx / ny; // execute line return -1; // default "error" value in case user passed in invalid choperation }  void printresult(long double x) {     cout << "the answer is: " << setprecision(0.01) << x << "\n"; }  long double calc() { // first number user     long double ninput1 = getuserinput();  // mathematical operations user     char o = getmathematicaloperation();  // second number user     long double ninput2 = getuserinput();  // calculate result , store in temporary variable (for readability/debug-ability)     long double nresult = calculateresult(ninput1, o, ninput2);  // print result     printresult(nresult);     return 0; } 

it not due overflow strange result. doubles can hold numbers in range showing.

try print result without setprecision.

edit: after trying

long double x = 100.1; cout << x << endl; 

i see doesn't work on windows system.

so searched little , found:

print long double on windows

maybe explanation.

so tried

long double x = 100.1; cout << (double)x << endl; 

which worked fine.

2nd edit:

also see link provided raphael

http://oldwiki.mingw.org/index.php/long%20double


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -