# How To Display A Whole Number or Decimal Point

• 06-28-2012
Glodel
How To Display A Whole Number or Decimal Point
Hello Guys its my first program and im confuse on how to put or to display decimal number and sorry and forgive me im still new on program languages i was wondering if you could help me with my code
Code:

```#include<iostream.h> #include<math.h> int main(){     char choice;     double comission;     double sales;     cout<<"Welcome\n";     cout<<"Please Choose Option Below\n";     cout<<"1: Please  Input If Sales are Less than 1000\n";     cout<<"2: Please Input If Sales are from 3000-5000\n";     cout<<"3: Please Sales are from 1000-2000\n";     cout<<"4: Please Sales are less than 500 \n";     cout<<"";cin>>choice;         switch (choice)                 {                 case '1':                         cout<<"Please Enter Sales Less Than 1000:S";                         cin>>sales;                         if(sales<=1000){                                 comission = sales * 0.05;                                 cout <<"S"<< comission<< endl << endl;                         }                         break;                 case '2':                         cout<<"Please Input If Sales are from 3000-5000:S";                         cin>>sales;                         if(sales>=3000 && sales<=5000){                         comission = sales * 0.1;                                 cout << "S"<< comission<< endl << endl;                         }                         break;                 case '3':                         cout<<"Please Input If Sales are from 1000-2000:S";                         cin>>sales;                         if(sales>=1000 && sales<=2000){                         comission = sales * 0.2;                                 cout << "S"<< comission <<  endl << endl;                         }                         break;                 case '4':                         cout<<"Please Input If Sales are from 500:S";                         cin>>sales;                         if(sales<=500){                         comission = sales * 0.02;                                 cout <<  "S" <<comission << endl << endl;                         }                         break;                 default:                         cout << "Invalid input" << endl;                 }                 getch(); }```
• 06-28-2012
grumpy
Sorry, that question is as clear as mud. I'm inferring what I think you mean, but may have answered incorrectly.

If you output a floating point value (as you are) it will be formatted and appear as a decimal value, complete with decimal point if needed. If you need (say) a maximum of two decimal places, then look up setprecision() (a manipulator of output streams that affects the next output floating point value). The stream operations will do the work.

As to other problems in your code, which you might need to think about .....

I fail to see why you first need to select the range, then check the range an input value is in. The whole switch/case block is pointless. Simply input the value, and the code can work out what range it is in, and act accordingly. The user should not need to be pestered to both identify the range, and then the value, since your code can work that out.

Note that it is generally a really BAD idea to use floating point variables to store monetary values. A fundamental property of floating point types is that they CANNOT represent certain fractions (in particular 0.1, 0.01, and any negative power of 10) exactly, which makes things rather ugly if you use a floating point value to represent dollars and cents.

Oh, and "commission" is spelt with two 'm's, not one.
• 06-28-2012
Salem
This is really weird, it's like you have the same code
Confuse Using Switch With If Satement - C And C++ | Dream.In.Code
• 06-28-2012
Glodel
sorry but the spelling im just concern about my program i tried using float and using const float and my professor told me not to use setprecision() only use const or define and i still dont know where to put setprecision() in my code because it my first time programming

and if i input sales 500.00 and evening using float the ouput would be like
ouput 25 but my professor wants me to display like 25.00 and if only my professor let me use printf but he will not let me i dont know how where to put decimal point in cout im use to using printf
• 06-28-2012
grumpy
So work out, assuming a value to be output is (say) 25.05, how to extract the values of 25 and 5 as two distinct integers. Then work out how to output them as "500.05".

If you can describe that on paper unambiguously you will be able to write code to do it. All code is (if done correctly) a set of unambiguous instructions to a computer. The trick is in the word "unambiguous" .... you can't rely on imprecise and ambiguous descriptions (which is what you are currently doing) because a computer will not make the sorts of leaps of judgement that a person might.

Using printf() would not solve the problem any more than using C++ streams. printf() format specifiers that affect precision are functionally nothing different from an iostream's setprecision manipulator.
• 06-28-2012
Glodel
Ah Thanks For your Words of encourage me when i read it i kinda deeply made me sad but at the same time finding new ways i tried using setprecision() it work but my professor will not let me use it anyway thanks anyway God-bless i was wondering if you could also give me other option on using decimal point without using setprecision() i wont ask for you fix my code i only want an example of other option on using decimal point im still new at this programming language im just NOOB in learning

Code:

```#include<iostream.h>#include<conio.h> #include<math.h> #include <iomanip> using namespace std; int main(){         char choice;     double comission = 0.0000;     double sales;     cout<<"Welcome\n";     cout<<"Please Choose Option Below\n";     cout<<"1: Please  Input If Sales are Less than 1000\n";     cout<<"2: Please Input If Sales are from 3000-5000\n";     cout<<"3: Please Sales are from 1000-2000\n";     cout<<"4: Please Sales are less than 500 \n";     cout<<"";cin>>choice;     switch (choice)         {         case '1':             cout<<"Please Enter Sales Less Than 1000:S";             cin>>sales;                             comission = sales * 0.05;                 cout << fixed << showpoint;                 cout << setprecision(3)<<fixed;;                 cout <<"S"<< comission<< endl << endl;                         break;         case '2':             cout<<"Please Input If Sales are from 3000-5000:S";             cin>>sales;                         comission = sales * 0.1;                 cout << setprecision(5)<<fixed;;                 cout << "S"<< comission<< endl << endl;                         break;         case '3':             cout<<"Please Input If Sales are from 1000-2000:S";             cin>>sales;                         comission = sales * 0.2;                 cout << setprecision(5)<<fixed;;                 cout << "S"<< comission <<  endl << endl;                         break;         case '4':             cout<<"Please Input If Sales are from 500:S";             cin>>sales;                         comission = sales * 0.02;                 cout << setprecision(5)<<fixed;;                 cout <<  "S" <<comission << endl << endl;                         break;         default:             cout << "Invalid input" << endl;         }         getch(); }```
• 06-28-2012
Rodaxoleaux
Quote:

Originally Posted by Salem
This is really weird, it's like you have the same code
Confuse Using Switch With If Satement - C And C++ | Dream.In.Code

Maybe he just doesn't like being called a DiC head.