Thread: can someone look at my code and provide suggestions ?

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    25

    can someone look at my code and provide suggestions ?

    i was hoping you could first run my program and have a look. It is extremely basic. i didnt know how to output the commission values in from case 3 (line 87 to 91) and output them in case 4, and i was hoping you could help me with any suggestions
    Thanks
    Code:
    #include <iostream>  				//the usual headers are used including string
    #include <string>
    #include <conio.h>
    
    void WeeklySales(); 									//these are functions i created
    
    
    main()
    {
    	int number; 													// declare variables
       char answer;
       double sales, sales_amount, quantity, total_sales, vat, commission;
    
       sales = (sales_amount * quantity);  				//initialise variables
       total_sales = (sales + vat);
    
    
    
       do {                   							//start of a do-while loop
       	      			//output displays the menu
       	cout << "\n\n******Sales System******";
          cout << ("\n\n");
          cout << "1. Display Company Logo.\n"; endl;
          cout << "2. Input/Validate weekly sales data.\n";endl;
          cout << "3. Calculate weekly sales.\n";endl;
          cout << "4. Display reciept.\n";endl;
          cout << "5. SHUT DOWN & LOG OFF.\n";endl;
    		cout << "\nEnter a number...\n";endl;  			//prompts the user
    
          cin >> number;                     //input
          cout << ("\n\n");
    
          switch (number)  //<--the expression is a variable (number) & controls
                //the switch the value of (number) is tested against a list of
                //constants. When a match is found, the statement sequence
                //assosciated with that match is executed.
                   {
          				case 1:    //<-----  const = 1
    
         //displays the company logo
                			cout << "\n\tUU\tUU \tEEEEEEEE \tLL\t\t \SSSSSSS\n\tUU\tU";
                         cout << "U \tEE \t\tLL\t\tS\n\tUU\tUU \tEE \t\tLL\t\tS\n\t";
                         cout << "UU\tUU \tEEEEEEE \t\LL\t\t\ SSSSSS\n\tUU\tUU \tEE";
                         cout << " \t\tLL\t\t       S\n\tUU\tUU  \tEE  \t  \t\LL  ";
                         cout << "\t\t       S \n\t UUUUUUUU   *\tEEEEEEEE  *\tLLLLL";
                         cout << "LL  *\tSSSSSSS   *\n\n\n\n\n";
    
       //the break statement causes program flow to exit from the entire switch statement
                         break;
    
                		case 2:   //<------const = 2. if match found, executes,
                               //then calls the weeklysales function below
    
                			cout << ("\nWeekly sales data\n\----------------- ");
                                    WeeklySales();  //<-------------   function call here
                                    break;
    
                		case 3:
    
                			cout << ("\nCalculate weekly sales \n\n");
                      	cout << "\n\nWould you like to calculate the weekly sales ? y/n\n";
       						cin >> answer;
    
            //a switch within a switch
       						switch (answer)	{
             					case 'n':       //<---if 'n' is entered the text is ouputted
                   				cout << "\n\n\nThank You!\n\n";
                   				break;  //<----break takes it out of the switch
                				case 'y':  //<---if 'y' then program is run below
    				cout << "\n\nTOTAL SALES = sales amount * quantity\n" << endl;
    				cout << "\nEnter the sales amount\n";
    				cin >>  sales_amount;
    				cout << "\nEnter the quantity\n" ;
    				cin >>  quantity ;
    
    				sales = (sales_amount * quantity);
    				cout << "\n" << sales_amount << "  *  " << quantity << " = " << sales << "\n";
    				cout << "\n" << "Sales is: \t" << sales << "\n";
    
       			vat = 0.175*sales;
       			total_sales = (sales + vat);
    				cout << "\VAT is :\t  " << vat << "\nTOTAL SALES inc VAT is: \t" << total_sales << "\n";
    
    				if(total_sales<=25)
    					cout << " and No commission";
    				else if(total_sales<=50)
    					cout << "\n5% commission of total sales is " << 0.05 * total_sales << "\n\n\n";
    				else if(total_sales<=75)
    					cout << "\n10% commission of total sales is " << 0.10 * total_sales << "\n\n\n";
    				else if(total_sales>75)
    					cout << "\n20% commission of total sales is " << 0.20 * total_sales << "\n\n\n";
    	}     //<-------end of the switch statement within the switch statement (nested)
                                    break;
                		case 4:      //displaying the invoice
    
                			cout << ("\nDisplay receipt \n\n");
                         cout << "\nUELS INVOICE\n";
       						cout << "------------\n\n";
                         cout << "Sales Amount :   " << sales_amount << "\n";
                         cout << "Quantity :       " << quantity << "\n";
                         cout << "Sales :          " << sales << "\n";
                         cout << "VAT :            " << vat << "\n";
                         cout << "Total Sales :    " << total_sales << "\n";
                         cout << "Commission :     " << commission << "\n";  //<------------------i couldn't figure out how to calculate the commission
                         cout << "\n\n\n\n\n";
                         break;
    
                		case 5:
    
                			cout << ("\nGoodbye, and thank you for using U.E.L.S. \n\n");break;
    
             //default statement sequence is executed if no matches are found
                		default:
    
                			cout << ("Enter a number from 1-5 only!\n\n\n\n\n\n");
    
             		}
          	} while (number !=5); //program will NOT stop looping till 5 is entered
          getch();
       }
    
    
    
    void WeeklySales() //<--------weekly sales function not part of main() function
    {                  //when function is completed, goes back to case 3:
    //declare variables
    	int sales_code, bikeprice, model_code, quantity;
       string date;
       char answer; //either yes or no
    
       cout << "\n\nWould you like to input weekly sales data ? \n";
       cin >> answer;
       	switch (answer)	{
             	case 'n':       //
                   cout << "\n\n\nThank You!\n\n";
                   break;
                case 'y':
       do
            //the instructions to enter the i.d. code will be repeated (do...while
            //loop) as long as the sales_code is less than 1 or greater than 20.
       	{
    
          	cout << "\n\nPlease enter your identifiction code \n";
             cin >> sales_code;
    
             //the condition is tested
             if (sales_code < 1 || sales_code > 20)
    
             	{
                	cout << ("\nTHIS I.D. CODE IS NOT VALID. TRY AGAIN \n");
                }
          }
    	//the instructions to enter the i.d. code will be repeated (do...while
       //loop) as long as the salescode is less than 1 or greater than 20.
       while (1 > sales_code || sales_code > 20);
    
       //another do...while loop
       do
       	{
          	cout << "\nPlease enter the bike price (one unit = 1 Euro) \n";
          	cin >> bikeprice;
    
             if (bikeprice < 1 || bikeprice > 500)
    
             	{
                	cout << ("\nINVALID PRICE. Bike prices cannot be 0 or more than 500. TRY AGAIN \n");
                }
          }
    
       while (bikeprice < 1 || bikeprice > 500);
    
       //another do...while loop
       do
       	{
          	cout << "\nPlease enter the 3 digit model code \n";
          	cin >> model_code;
    
             if (model_code < 0 || model_code > 999)
    
             	{
                	cout << ("\nINVALID MODEL CODE. TRY AGAIN \n");
                }
          }
    
       while (model_code < 0 || model_code > 999);
    
       //another do...while loop
       do
       	{
             cout << ("\nPlease enter the quantity sold \n");
          	cin >> quantity;
    
             if (quantity < 1 || quantity > 10)
    
             {
             	cout << ("\nINVALID QUANTITY. TRY A NUMBER FROM 1-10 \n");
             }
          }
    
       while (quantity < 1 || quantity > 10);
    
    	do {
    
    
    			cout << "\nPlease enter the date in this format dd/mm/yyyy: \n";
             cin >> date;
    
    			cout << "\nIs this the correct date ?\t" << date << "\tpress y/n: \n\n";
             cin >> answer;   //gets answer from user
    
    			switch (answer)	{
             	case 'y':
                	cout << "\n\n\nThank You!\n\n";
                   break;
                }
             }while (answer != 'y');
    
        }
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The cool part about = is that the value of the expression is the value that was assigned. This is used for chaining assignments together, but it also means you can really really abuse the language and do
    Code:
    cout << "\n20% commission of total sales is " << commission = 0.20 * total_sales << "\n\n\n";

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    lol nice logo

    I copied it in a cpp, it works well but you have to notice that you use escape characters which dont exist sometimes, like \L

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Adopt a really consistent approach to indentation if you want people to look at your code.

    Mixing spaces and tabs is a disaster waiting to happen. Your smart IDE can cope, but HTML (and a host of other primitive tools) will generally make a mess of your masterpiece.
    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.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    18
    indent properly

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    25
    Quote Originally Posted by tabstop View Post
    The cool part about = is that the value of the expression is the value that was assigned. This is used for chaining assignments together, but it also means you can really really abuse the language and do
    Code:
    cout << "\n20% commission of total sales is " << commission = 0.20 * total_sales << "\n\n\n";
    ok. i had tried
    Code:
    cout << "\n20% commission of total sales is " << total_sales * 0.20 = commission << "\n\n\n";
    before and it never worked. i guess i had to do it the way you suggested.
    Thanks.

    ps why are people so touchy about indents ? if i indented my work consistently, my code would span two pages wide.
    Is this acceptable ?

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    I dont think it would much longer, and its much easier to read

    for example this sort of stuff is pretty bad

    Code:
    case 2:   //<------const = 2. if match found, executes,
                               //then calls the weeklysales function below
    
                			cout << ("\nWeekly sales data\n\----------------- ");
                                    WeeklySales();  //<-------------   function call here
                                    break;
    
                		case 3:
    
                			cout << ("\nCalculate weekly sales \n\n");
                      	cout << "\n\nWould you like to calculate the weekly sales ? y/n\n";
       						cin >> answer;

  8. #8
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    More to the point, most folks use indentation to further delimit logical blocks of code in addition to using {}. When your formatting is all over the place like yours is, it is doubly hard for folks trying to help find your answers for you to actually do so.

    Code clarity can be not just important to folks trying to understand your code but also for you to help spot logical errors (by comparison, syntax errors are a breeze; finding errors in logic can be really hard if you didn't write the code yourself and have the insight of the 'intent' of the code.

    One other thing that probably would not fix anything but will almost certainly help with debugging other issues is not to embed one switch statement inside another like that. Not saying you cannot but if you took the 'inner' switch and put it into its own function which just returned the results.

    Exporting that switch to another function, fixing the non-standard escape chars (notes above), fixing the stray 'endl;'s (its not a function and cannot be called. Plus you are doing \n in the string and then endl right after it...did you mean to do that?), fixed the platform-specific call to getch() (now compiles on both Windows and Linux) and cleaned up most of the indentation, the code now looks like:
    Code:
    
    #ifdef WIN32
    #include <conio.h>
    #else
    #include <stdio.h>
    #include <termios.h>
    #include <unistd.h>
    
    int getch( )
    {
        struct termios oldt,
                    newt;
        int            ch;
        tcgetattr( STDIN_FILENO, &oldt );
        newt = oldt;
        newt.c_lflag &= ~( ICANON | ECHO );
        tcsetattr( STDIN_FILENO, TCSANOW, &newt );
        ch = getchar();
        tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
        return ch;
    }
    //--------------------------------------------------------------------------------
    #endif
    #include <iostream>                 //the usual headers are used including string
    #include <string>
    using namespace std;
    
    void WeeklySales();                                     //these are functions i created
    
    void processAnswer( char cAnswer, double &total_sales, double &vat, double &sales, double &quantity, double &sales_amount)
    {
        switch (cAnswer)
        {
        case 'n':       //<---if 'n' is entered the text is ouputted
            cout << "\n\n\nThank You!\n\n";
            break;  //<----break takes it out of the switch
        case 'y':  //<---if 'y' then program is run below
            cout << "\n\nTOTAL SALES = sales amount * quantity\n" << endl;
            cout << "\nEnter the sales amount\n";
            cin >>  sales_amount;
            cout << "\nEnter the quantity\n" ;
            cin >>  quantity ;
    
            sales = (sales_amount * quantity);
            cout << "\n" << sales_amount << "  *  " << quantity << " = " << sales << "\n";
            cout << "\n" << "Sales is: \t" << sales << "\n";
    
            vat = 0.175*sales;
            total_sales = (sales + vat);
            cout << "VAT is :\t  " << vat << "\nTOTAL SALES inc VAT is: \t" << total_sales << "\n";
    
            if (total_sales<=25)
                cout << " and No commission";
            else if (total_sales<=50)
                cout << "\n5% commission of total sales is " << 0.05 * total_sales << "\n\n\n";
            else if (total_sales<=75)
                cout << "\n10% commission of total sales is " << 0.10 * total_sales << "\n\n\n";
            else if (total_sales>75)
                cout << "\n20% commission of total sales is " << 0.20 * total_sales << "\n\n\n";
        }     //<-------end of the switch statement within the switch statement (nested)
    
    }
    
    
    main()
    {
        int number;                                                  // declare variables
        char answer;
        double sales, sales_amount, quantity, total_sales, vat, commission;
    
        sales = (sales_amount * quantity);               //initialise variables
        total_sales = (sales + vat);
    
    
    
        do                                               //start of a do-while loop
        {
            //output displays the menu
            cout << "\n\n******Sales System******";
            cout << ("\n\n");
            cout << "1. Display Company Logo.\n" << endl;
            cout << "2. Input/Validate weekly sales data.\n" << endl;
            cout << "3. Calculate weekly sales.\n" << endl;
            cout << "4. Display reciept.\n" << endl;
            cout << "5. SHUT DOWN & LOG OFF.\n" << endl;
            cout << "\nEnter a number...\n" << endl;
    
            cin >> number;                     //input
            cout << ("\n\n");
    
            switch (number)  //<--the expression is a variable (number) & controls
                //the switch the value of (number) is tested against a list of
                //constants. When a match is found, the statement sequence
                //assosciated with that match is executed.
            {
            case 1:    //<-----  const = 1
    
                //displays the company logo
                cout << "\n\tUU\tUU \tEEEEEEEE \tLL\t\t SSSSSSS\n\tUU\tU";
                cout << "U \tEE \t\tLL\t\tS\n\tUU\tUU \tEE \t\tLL\t\tS\n\t";
                cout << "UU\tUU \tEEEEEEE \t LL\t\t SSSSSS\n\tUU\tUU \tEE";
                cout << " \t\tLL\t\t       S\n\tUU\tUU  \tEE  \t  \t LL  ";
                cout << "\t\t       S \n\t UUUUUUUU   *\tEEEEEEEE  *\tLLLLL";
                cout << "LL  *\tSSSSSSS   *\n\n\n\n\n";
    
                //the break statement causes program flow to exit from the entire switch statement
                break;
    
            case 2:   //<------const = 2. if match found, executes,
                //then calls the weeklysales function below
    
                cout << ("\nWeekly sales data\n ----------------- ");
                WeeklySales();  //<-------------   function call here
                break;
    
            case 3:
    
                cout << ("\nCalculate weekly sales \n\n");
                cout << "\n\nWould you like to calculate the weekly sales ? y/n\n";
                cin >> answer;
    
                //a switch in another function
                processAnswer( answer,total_sales,vat, sales, quantity, sales);
    
                break;
            case 4:      //displaying the invoice
    
                cout << ("\nDisplay receipt \n\n");
                cout << "\nUELS INVOICE\n";
                cout << "------------\n\n";
                cout << "Sales Amount :   " << sales_amount << "\n";
                cout << "Quantity :       " << quantity << "\n";
                cout << "Sales :          " << sales << "\n";
                cout << "VAT :            " << vat << "\n";
                cout << "Total Sales :    " << total_sales << "\n";
                cout << "Commission :     " << commission << "\n";  //<------------------i couldn't figure out how to calculate the commission
                cout << "\n\n\n\n\n";
                break;
    
            case 5:
    
                cout << ("\nGoodbye, and thank you for using U.E.L.S. \n\n");
                break;
    
                //default statement sequence is executed if no matches are found
            default:
    
                cout << ("Enter a number from 1-5 only!\n\n\n\n\n\n");
    
            }
        }
        while (number !=5); //program will NOT stop looping till 5 is entered
        getch();
    }
    
    
    
    void WeeklySales() //<--------weekly sales function not part of main() function
    {
        //when function is completed, goes back to case 3:
    //declare variables
        int sales_code, bikeprice, model_code, quantity;
        string date;
        char answer; //either yes or no
    
        cout << "\n\nWould you like to input weekly sales data ? \n";
        cin >> answer;
        switch (answer)
        {
        case 'n':       //
            cout << "\n\n\nThank You!\n\n";
            break;
        case 'y':
            do
                //the instructions to enter the i.d. code will be repeated (do...while
                //loop) as long as the sales_code is less than 1 or greater than 20.
            {
    
                cout << "\n\nPlease enter your identifiction code \n";
                cin >> sales_code;
    
                //the condition is tested
                if (sales_code < 1 || sales_code > 20)
    
                {
                    cout << ("\nTHIS I.D. CODE IS NOT VALID. TRY AGAIN \n");
                }
            }
            //the instructions to enter the i.d. code will be repeated (do...while
            //loop) as long as the salescode is less than 1 or greater than 20.
            while (1 > sales_code || sales_code > 20);
    
            //another do...while loop
            do
            {
                cout << "\nPlease enter the bike price (one unit = 1 Euro) \n";
                cin >> bikeprice;
    
                if (bikeprice < 1 || bikeprice > 500)
    
                {
                    cout << ("\nINVALID PRICE. Bike prices cannot be 0 or more than 500. TRY AGAIN \n");
                }
            }
    
            while (bikeprice < 1 || bikeprice > 500);
    
            //another do...while loop
            do
            {
                cout << "\nPlease enter the 3 digit model code \n";
                cin >> model_code;
    
                if (model_code < 0 || model_code > 999)
    
                {
                    cout << ("\nINVALID MODEL CODE. TRY AGAIN \n");
                }
            }
    
            while (model_code < 0 || model_code > 999);
    
            //another do...while loop
            do
            {
                cout << ("\nPlease enter the quantity sold \n");
                cin >> quantity;
    
                if (quantity < 1 || quantity > 10)
    
                {
                    cout << ("\nINVALID QUANTITY. TRY A NUMBER FROM 1-10 \n");
                }
            }
    
            while (quantity < 1 || quantity > 10);
    
            do
            {
    
    
                cout << "\nPlease enter the date in this format dd/mm/yyyy: \n";
                cin >> date;
    
                cout << "\nIs this the correct date ?\t" << date << "\tpress y/n: \n\n";
                cin >> answer;   //gets answer from user
    
                switch (answer)
                {
                case 'y':
                    cout << "\n\n\nThank You!\n\n";
                    break;
                }
            }
            while (answer != 'y');
    
        }
    }
    Easier to analyze, no?
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  9. #9
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Also I am curious what lead you to using a switch statement with only one condition (an if() would have worked and been clearer) and no default: case (which would have been safer or more defensive):
    Code:
     switch (answer)
                {
                case 'y':
                    cout << "\n\n\nThank You!\n\n";
                    break;
                }
    IOW it could have been:
    Code:
                if(answer == 'y')
                {
                    cout << "\n\n\nThank You!\n\n";
                }
    (cleaner) or
    Code:
                switch (answer)
                {
                    case 'y':
                    {
                        cout << "\n\n\nThank You!\n\n";
                        break;
                    }
                    default:
                    {
                        cout << "\n\nIllegal character.\n\n";
                        break;
                    }
                }
    (safer)...
    Last edited by jeffcobb; 05-11-2010 at 08:20 PM.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    25
    Quote Originally Posted by jeffcobb View Post
    Also I am curious what lead you to using a switch statement with only one condition (an if() would have worked and been clearer) and no default: case (which would have been safer or more defensive):
    Code:
     switch (answer)
                {
                case 'y':
                    cout << "\n\n\nThank You!\n\n";
                    break;
                }
    IOW it could have been:
    Code:
                if(answer == 'y')
                {
                    cout << "\n\n\nThank You!\n\n";
                }
    (cleaner) or
    Code:
                switch (answer)
                {
                    case 'y':
                    {
                        cout << "\n\n\nThank You!\n\n";
                        break;
                    }
                    default:
                    {
                        cout << "\n\nIllegal character.\n\n";
                        break;
                    }
                }
    (safer)...
    if the input was 'y', it wouldn't go to the next bit of code. it would just loop back to the menu. so that wouldnt work.
    if 'n' was inputted, it would say illegal character.

  11. #11
    Registered User
    Join Date
    Apr 2010
    Posts
    25
    Quote Originally Posted by atlas07 View Post
    if the input was 'y', it wouldn't go to the next bit of code. it would just loop back to the menu. so that wouldnt work.
    if 'n' was inputted, it would say illegal character.
    my fault, i thought you were talking about another piece of code

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    25

    any ideas ?

    Quote Originally Posted by tabstop View Post
    The cool part about = is that the value of the expression is the value that was assigned. This is used for chaining assignments together, but it also means you can really really abuse the language and do
    Code:
    cout << "\n20% commission of total sales is " << commission = 0.20 * total_sales << "\n\n\n";
    ok, i have reformatted my code and sorted out my issue. For all those who were unhappy.
    I now have a new problem, how would i output the commission value in case 4? (line 107).
    here i have tried using
    Code:
    cout << "\n5% commission of total sales is " << commission = 0.05 * total_sales << "\n\n\n";
    to assign commission a value, but i get the error

    illegal use of floating point.

    Any ideas?

    Info :Compiling E:\program\kyeiassignforinternet.cpp
    Warn : string.h(549,3):Functions containing for are not expanded inline
    Warn : string.h(557,3):Functions containing while are not expanded inline
    Warn : string.h(563,3):Functions containing for are not expanded inline
    Warn : string.h(575,3):Functions containing for are not expanded inline
    Warn : string.cc(686,32):Comparing signed and unsigned values
    Warn : kyeiassignforinternet.cpp(14,10)ossible use of 'sales_amount' before definition
    Warn : kyeiassignforinternet.cpp(14,10)ossible use of 'quantity' before definition
    Warn : kyeiassignforinternet.cpp(15,16)ossible use of 'vat' before definition
    Error: kyeiassignforinternet.cpp(90,103):Illegal use of floating point
    Error: kyeiassignforinternet.cpp(92,104):Illegal use of floating point
    Error: kyeiassignforinternet.cpp(94,104):Illegal use of floating point
    Warn : kyeiassignforinternet.cpp(133,16):Use qualified name to access member type 'std::string'

    oh yeah, here is my new and improved program:
    Code:
    #include <iostream>  				//the usual headers are used including string
    #include <string>
    #include <conio.h>
    
    void WeeklySales(); 									//function i created
    
    
    main()
    {
      int number; 													// declare variables
      char answer;
      double sales, sales_amount, quantity, total_sales, vat, commission;
    
      sales = (sales_amount * quantity);  				//initialise variables
      total_sales = (sales + vat);
    
    
    
      do {                   							//start of a do-while loop
       	      			//output displays the menu
       	cout << "\n\n******Sales System******";
          cout << ("\n\n");
          cout << "1. Display Company Logo.\n"; endl;
          cout << "2. Input/Validate weekly sales data.\n";endl;
          cout << "3. Calculate weekly sales.\n";endl;
          cout << "4. Display reciept.\n";endl;
          cout << "5. SHUT DOWN & LOG OFF.\n";endl;
    		cout << "\nEnter a number...\n";endl;  			//prompts the user
    
          cin >> number;                     //input
          cout << ("\n\n");
    
          switch (number)  //<--the expression is a variable (number) & controls
                //the switch the value of (number) is tested against a list of
                //constants. When a match is found, the statement sequence
                //assosciated with that match is executed.
                   {     //<-----------start of the main switch
                     case 1:    //<-----  const
                                //displays the company logo
                       cout << "\n\tUU\tUU \tEEEEEEEE \tLL\t\t \SSSSSSS\n\tUU\tU";
                       cout << "U \tEE \t\tLL\t\tS\n\tUU\tUU \tEE \t\tLL\t\tS\n\t";
                       cout << "UU\tUU \tEEEEEEE \t\LL\t\t\ SSSSSS\n\tUU\tUU \tEE";
                       cout << " \t\tLL\t\t       S\n\tUU\tUU  \tEE  \t  \t\LL  ";
                       cout << "\t\t       S \n\t UUUUUUUU   *\tEEEEEEEE  *\tLLLLL";
                       cout << "LL  *\tSSSSSSS   *\n\n\n\n\n";
         //the break statement causes program flow to exit from the entire switch statement
                       break;
                    case 2:    //<------const = 2. if match found, executes,
                               //then calls the weeklysales function below
                       cout << ("\nWeekly sales data\n\----------------- ");
                       WeeklySales();  //<-------------   function call here
                       break;
    
                	  case 3:
                       cout << ("\nCalculate weekly sales \n\n");
                       cout << "\n\nWould you like to calculate the weekly sales ? y/n\n";
       					 cin >> answer;
                                           //a switch within a switch
       					 switch (answer) {
             					case 'n':       //<---if 'n' is entered the text is ouputted
                   				cout << "\n\n\nThank You!\n\n";
                   				break;  //<----break takes it out of the switch
                            case 'y':  //<---if 'y' then program is run below
    				               cout << "\n\nTOTAL SALES = sales amount * quantity\n" << endl;
    				               cout << "\nEnter the sales amount\n";
    				               cin >>  sales_amount;
    				               cout << "\nEnter the quantity\n" ;
    				               cin >>  quantity ;
    
    				               sales = (sales_amount * quantity);
    				               cout << "\n" << sales_amount << "  *  " << quantity << " = " << sales << "\n";
    				               cout << "\n" << "Sales is: \t" << sales << "\n";
    
       			               vat = 0.175*sales;
       			               total_sales = (sales + vat);
    				               cout << "\VAT is :\t  " << vat << "\nTOTAL SALES inc VAT is: \t" << total_sales << "\n";
    
    				               if(total_sales<=25)
    					               cout << " and No commission";
    				               else if(total_sales<=50)
    					               cout << "\n5% commission of total sales is " << commission = 0.05 * total_sales << "\n\n\n";
    				               else if(total_sales<=75)
    					               cout << "\n10% commission of total sales is " << commission = 0.10 * total_sales << "\n\n\n";
    				               else if(total_sales>75)
    					               cout << "\n20% commission of total sales is " << commission = 0.20 * total_sales << "\n\n\n";
    	                }     //<-------end of the switch statement within the switch statement (nested)
                               break;
                	  case 4:      //displaying the invoice
                       cout << ("\nDisplay receipt \n\n");
                       cout << "\nUELS INVOICE\n";
       					 cout << "------------\n\n";
                       cout << "Sales Amount :   " << sales_amount << "\n";
                       cout << "Quantity :       " << quantity << "\n";
                       cout << "Sales :          " << sales << "\n";
                       cout << "VAT :            " << vat << "\n";
                       cout << "Total Sales :    " << total_sales << "\n";
                       cout << "Commission :     " << commission << "\n";  //<------------------i couldn't figure out how to calculate the commission
                       cout << "\n\n\n\n\n";
                       break;
    
                	  case 5:
                       cout << ("\nGoodbye, and thank you for using U.E.L.S. \n\n");break;
                                //default statement sequence is executed if no matches are found
                	  default:
                       cout << ("Enter a number from 1-5 only!\n\n\n\n\n\n");
                   }    // <-----end of the main switch
         } while (number !=5); //<--end of do while loop. (program will NOT stop looping till 5 is entered)
    
      getch();
    
    } // <---------end of main
    
    
    
    void WeeklySales() //<--------weekly sales function not part of main() function
    {                  //when function is completed, goes back to case 3:
    //declare variables
    	int sales_code, bikeprice, model_code, quantity;
       string date;
       char answer; //either yes or no
    
       cout << "\n\nWould you like to input weekly sales data ? \n";
       cin >> answer;
       	switch (answer)	{  //<--------start of main switch
             	case 'n':
                   cout << "\n\n\nThank You!\n\n";
                   break;
                case 'y':
       				do
            //the instructions to enter the i.d. code will be repeated (do...while
            //loop) as long as the sales_code is less than 1 or greater than 20.
       	            {
            	           cout << "\n\nPlease enter your identifiction code \n";
                        cin >> sales_code;
               //the condition is tested
                        if (sales_code < 1 || sales_code > 20)
               	          {
                	         cout << ("\nTHIS I.D. CODE IS NOT VALID. TRY AGAIN \n");
                          }
                       }
    	//the instructions to enter the i.d. code will be repeated (do...while
       //loop) as long as the salescode is less than 1 or greater than 20.
                          while (1 > sales_code || sales_code > 20);
         //another do...while loop
                   do
       	            {
          	           cout << "\nPlease enter the bike price (one unit = 1 Euro) \n";
          	           cin >> bikeprice;
    
                        if (bikeprice < 1 || bikeprice > 500)
               	           {
                	          cout << ("\nINVALID PRICE. Bike prices cannot be 0 or more than 500. TRY AGAIN \n");
                           }
                      } while (bikeprice < 1 || bikeprice > 500);
    
       //another do...while loop
                   do
       	            {
          	           cout << "\nPlease enter the 3 digit model code \n";
          	           cin >> model_code;
    
                        if (model_code < 0 || model_code > 999)
                          {
                	         cout << ("\nINVALID MODEL CODE. TRY AGAIN \n");
                          }
                      } while (model_code < 0 || model_code > 999);
    
       //another do...while loop
                   do
       	            {
                        cout << ("\nPlease enter the quantity sold \n");
          	           cin >> quantity;
    
                        if (quantity < 1 || quantity > 10)
                          {
             	            cout << ("\nINVALID QUANTITY. TRY A NUMBER FROM 1-10 \n");
                          }
                      } while (quantity < 1 || quantity > 10);
    
    	            do
                      {
                        cout << "\nPlease enter the date in this format dd/mm/yyyy: \n";
                        cin >> date;
    
    			           cout << "\nIs this the correct date ?\t" << date << "\tpress y/n: \n\n";
                        cin >> answer;   //gets answer from user
    
    			           switch (answer)	{  //<--------start of main switch
             				 case 'y':
                            {
                              cout << "\n\n\nThank You!\n\n";
                              break;
                            }
                          default:
       			            {
                              cout << "\n\nIllegal character.\n\n";
                              break;
                            } //<---end of main switch statement
                         }
                      }  while (answer != 'y');
                } //<---end of main switch statement
    }
    no matter what i do, it won't indent. i'm sorry.

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    25
    i guess, it's because i'm using borland 5.02 which is out of date, but hey thats what we HAVE to use for this program

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > ps why are people so touchy about indents ?
    Did you actually look at your original post before you submitted it?
    Does it actually LOOK that messy in your IDE?
    Why ask us to look at something ugly if you're using something pretty.


    > if i indented my work consistently, my code would span two pages wide.
    > Is this acceptable ?
    a) choose a smaller tab-setting
    b) break the function into smaller sub-functions.

    As a general rule, if you can't see the whole function on screen at once, then you might need to think about refactoring it.

    For example, your case 3: and the nested switch, that could so easily be removed to another function, and collapse a few more indent levels.

    Another example, '//another do...while loop' are 4 nearly identical loops. If you wrote a function to accept parameters for prompt, range and error message, and return result, the code would be so much smaller.


    On the subject of mixing spaces and tabs, here is what I see.
    Sure I could play a game of "guess your tab setting", but why should we? You're the one asking for a review, make it easy for people to review (or it won't get done).
    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.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Oh, and FWIW, your original post with board-friendly spaces only for indentation
    Code:
    #include <iostream>          //the usual headers are used including string
    #include <string>
    #include <conio.h>
    
    void WeeklySales();                   //these are functions i created
    
    
    main()
    {
      int number;                           // declare variables
      char answer;
      double sales, sales_amount, quantity, total_sales, vat, commission;
    
      sales = (sales_amount * quantity);          //initialise variables
      total_sales = (sales + vat);
    
      do {                                 //start of a do-while loop
        //output displays the menu
        cout << "\n\n******Sales System******";
        cout << ("\n\n");
        cout << "1. Display Company Logo.\n"; endl;
        cout << "2. Input/Validate weekly sales data.\n";endl;
        cout << "3. Calculate weekly sales.\n";endl;
        cout << "4. Display reciept.\n";endl;
        cout << "5. SHUT DOWN & LOG OFF.\n";endl;
        cout << "\nEnter a number...\n";endl;        //prompts the user
    
        cin >> number;                     //input
        cout << ("\n\n");
    
        switch (number)  //<--the expression is a variable (number) & controls
        //the switch the value of (number) is tested against a list of
        //constants. When a match is found, the statement sequence
        //assosciated with that match is executed.
        {
        case 1:    //<-----  const = 1
    
          //displays the company logo
          cout << "\n\tUU\tUU \tEEEEEEEE \tLL\t\t \SSSSSSS\n\tUU\tU";
          cout << "U \tEE \t\tLL\t\tS\n\tUU\tUU \tEE \t\tLL\t\tS\n\t";
          cout << "UU\tUU \tEEEEEEE \t\LL\t\t\ SSSSSS\n\tUU\tUU \tEE";
          cout << " \t\tLL\t\t       S\n\tUU\tUU  \tEE  \t  \t\LL  ";
          cout << "\t\t       S \n\t UUUUUUUU   *\tEEEEEEEE  *\tLLLLL";
          cout << "LL  *\tSSSSSSS   *\n\n\n\n\n";
    
          //the break statement causes program flow to exit from the entire switch statement
          break;
    
        case 2:   //<------const = 2. if match found, executes,
          //then calls the weeklysales function below
    
          cout << ("\nWeekly sales data\n\----------------- ");
          WeeklySales();  //<-------------   function call here
          break;
    
        case 3:
    
          cout << ("\nCalculate weekly sales \n\n");
          cout << "\n\nWould you like to calculate the weekly sales ? y/n\n";
          cin >> answer;
    
          //a switch within a switch
          switch (answer)  {
          case 'n':       //<---if 'n' is entered the text is ouputted
            cout << "\n\n\nThank You!\n\n";
            break;  //<----break takes it out of the switch
          case 'y':  //<---if 'y' then program is run below
            cout << "\n\nTOTAL SALES = sales amount * quantity\n" << endl;
            cout << "\nEnter the sales amount\n";
            cin >>  sales_amount;
            cout << "\nEnter the quantity\n" ;
            cin >>  quantity ;
    
            sales = (sales_amount * quantity);
            cout << "\n" << sales_amount << "  *  " << quantity << " = " << sales << "\n";
            cout << "\n" << "Sales is: \t" << sales << "\n";
    
            vat = 0.175*sales;
            total_sales = (sales + vat);
            cout << "\VAT is :\t  " << vat << "\nTOTAL SALES inc VAT is: \t" << total_sales << "\n";
    
            if(total_sales<=25)
              cout << " and No commission";
            else if(total_sales<=50)
              cout << "\n5% commission of total sales is " << 0.05 * total_sales << "\n\n\n";
            else if(total_sales<=75)
              cout << "\n10% commission of total sales is " << 0.10 * total_sales << "\n\n\n";
            else if(total_sales>75)
              cout << "\n20% commission of total sales is " << 0.20 * total_sales << "\n\n\n";
          }     //<-------end of the switch statement within the switch statement (nested)
          break;
        case 4:      //displaying the invoice
    
          cout << ("\nDisplay receipt \n\n");
          cout << "\nUELS INVOICE\n";
          cout << "------------\n\n";
          cout << "Sales Amount :   " << sales_amount << "\n";
          cout << "Quantity :       " << quantity << "\n";
          cout << "Sales :          " << sales << "\n";
          cout << "VAT :            " << vat << "\n";
          cout << "Total Sales :    " << total_sales << "\n";
          cout << "Commission :     " << commission << "\n";  //<------------------i couldn't figure out how to calculate the commission
          cout << "\n\n\n\n\n";
          break;
    
        case 5:
    
          cout << ("\nGoodbye, and thank you for using U.E.L.S. \n\n");break;
    
          //default statement sequence is executed if no matches are found
        default:
    
          cout << ("Enter a number from 1-5 only!\n\n\n\n\n\n");
    
        }
      } while (number !=5); //program will NOT stop looping till 5 is entered
      getch();
    }
    
    
    
    void WeeklySales() //<--------weekly sales function not part of main() function
    {                  //when function is completed, goes back to case 3:
      //declare variables
      int sales_code, bikeprice, model_code, quantity;
      string date;
      char answer; //either yes or no
    
      cout << "\n\nWould you like to input weekly sales data ? \n";
      cin >> answer;
      switch (answer)  {
      case 'n':       //
        cout << "\n\n\nThank You!\n\n";
        break;
      case 'y':
        do
        //the instructions to enter the i.d. code will be repeated (do...while
        //loop) as long as the sales_code is less than 1 or greater than 20.
        {
    
          cout << "\n\nPlease enter your identifiction code \n";
          cin >> sales_code;
    
          //the condition is tested
          if (sales_code < 1 || sales_code > 20)
          {
            cout << ("\nTHIS I.D. CODE IS NOT VALID. TRY AGAIN \n");
          }
        }
        //the instructions to enter the i.d. code will be repeated (do...while
        //loop) as long as the salescode is less than 1 or greater than 20.
        while (1 > sales_code || sales_code > 20);
    
        //another do...while loop
        do
        {
          cout << "\nPlease enter the bike price (one unit = 1 Euro) \n";
          cin >> bikeprice;
    
          if (bikeprice < 1 || bikeprice > 500)
          {
            cout << ("\nINVALID PRICE. Bike prices cannot be 0 or more than 500. TRY AGAIN \n");
          }
        }
        while (bikeprice < 1 || bikeprice > 500);
    
        //another do...while loop
        do
        {
          cout << "\nPlease enter the 3 digit model code \n";
          cin >> model_code;
    
          if (model_code < 0 || model_code > 999)
          {
            cout << ("\nINVALID MODEL CODE. TRY AGAIN \n");
          }
        }
        while (model_code < 0 || model_code > 999);
    
        //another do...while loop
        do
        {
          cout << ("\nPlease enter the quantity sold \n");
          cin >> quantity;
    
          if (quantity < 1 || quantity > 10)
          {
            cout << ("\nINVALID QUANTITY. TRY A NUMBER FROM 1-10 \n");
          }
        }
        while (quantity < 1 || quantity > 10);
    
        do {
          cout << "\nPlease enter the date in this format dd/mm/yyyy: \n";
          cin >> date;
    
          cout << "\nIs this the correct date ?\t" << date << "\tpress y/n: \n\n";
          cin >> answer;   //gets answer from user
    
          switch (answer)  {
          case 'y':
            cout << "\n\n\nThank You!\n\n";
            break;
          }
        }while (answer != 'y');
    
      }
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help would be much appreciated.
    By jitterbug in forum C++ Programming
    Replies: 6
    Last Post: 04-01-2009, 08:02 PM
  2. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  3. Replies: 8
    Last Post: 04-28-2003, 07:29 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. Merging files (code included)
    By TankCDR in forum C Programming
    Replies: 3
    Last Post: 10-28-2001, 11:06 AM