How To Display A Whole Number or Decimal Point

This is a discussion on How To Display A Whole Number or Decimal Point within the C++ Programming forums, part of the General Programming Boards category; Hello Guys its my first program and im confuse on how to put or to display decimal number and sorry ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    3

    Lightbulb 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();
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    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.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    This is really weird, it's like you have the same code
    Confuse Using Switch With If Satement - C And C++ | Dream.In.Code
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    3
    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
    Last edited by Glodel; 06-28-2012 at 04:03 AM.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    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.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    3
    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();
    }
    Last edited by Glodel; 06-28-2012 at 04:48 AM.

  7. #7
    and the hat of Marriage Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    178
    Quote Originally Posted by Salem View Post
    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.
    How to ask smart questions

    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesWritten);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get a value Before/After a decimal point in C ??
    By ammar555 in forum C Programming
    Replies: 7
    Last Post: 05-18-2011, 01:47 PM
  2. Typing decimal number and cover it binary number!
    By Kazumi in forum C Programming
    Replies: 32
    Last Post: 04-16-2011, 07:21 PM
  3. Replies: 4
    Last Post: 06-28-2010, 03:13 PM
  4. how to convert decimal to floating point number
    By -EquinoX- in forum C Programming
    Replies: 98
    Last Post: 03-04-2008, 12:25 PM
  5. Decimal places on Floating point number
    By manutdfan in forum C Programming
    Replies: 1
    Last Post: 10-29-2006, 11:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21