Thread: Switch statements - displaying values from one case in another

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

    Switch statements - displaying values from one case in another

    i have a piece of an unfinished program.
    try to run it...
    if you go straight to option 3 and input values, how do i output these values in option 4 ?
    i tried outputting my declared variables
    it wont work ? why is this ?
    i apologize the code is so long

    I hope someone can tell me how i'd go about it


    Code:
    #include <iostream>  				//the usual headers are used including string
    #include <string>
    #include <conio.h>
    
    void WeeklySales(); 									//these are functions i created
    void yesnofunc();
    
    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;
          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:
    
                			cout << ("\nWeekly sales data\n\----------------- ");
    
                                    break;
    
                		case 3:
    
                			cout << ("\nCalculate weekly sales \n\n");
                      	cout << "\n\nWould you like to calculate the weekly sales ? \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
    
                //variables declared
       			double sales, sales_amount, quantity, total_sales, vat;
    				total_sales = sales + vat;
    
       			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 << "\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 ;
    				else if(total_sales<75)
    					cout << "\n10% commission of total sales is " << 0.10 * total_sales;
    				else if(total_sales>75)
    					cout << "\n20% commission of total sales is " << 0.20 * total_sales;
    	}
                                    break;
                		case 4:
    //how would i display total sales and sales entered above in case 3 ?
                			cout << ("\nDisplay receipt \n\n");
                         cout << "\nUELS\n";
       						cout << "----\n\n";
                         cout << total_sales << "\n";   <-------------------i only get an address and not the output from case 3
                         cout << sales;
    
                         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 yesnofunc()
    {
    	char answer;
    
    do {
    		cout << "\nIs this correct ?\n";
       	cin >> answer;
    
       	switch (answer) {
          	case 'y':
             	cout << "\nThank you!!\n";
                break;
    
          }
    	}while (answer != 'y');
    }
    i guess i'm trying to say; how do i output values from one case within a switch statement to another ?

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    43
    Have you tried running this code? There are some issues you need to fix:
    • main() always returns an int.
    • You need a name qualifier for your iostream objects (std::cin, etc.).
    • You initialize sales and total_sales using uninitialized values.
    • You have extraneous escape sequences.
    • You declare some of your variables twice (once at the beginning of main() and again in the body of the switch() statement).
    • You have two function declarations, one of which is not implemented, and the other is a)never used, and b)does nothing useful.


    Somewhere in your code, you need to have some actual data, or else you're going to get junk results.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    25
    Quote Originally Posted by Mostly Harmless View Post
    Have you tried running this code? There are some issues you need to fix:
    • main() always returns an int.
    • You need a name qualifier for your iostream objects (std::cin, etc.).
    • You initialize sales and total_sales using uninitialized values.
    • You have extraneous escape sequences.
    • You declare some of your variables twice (once at the beginning of main() and again in the body of the switch() statement).
    • You have two function declarations, one of which is not implemented, and the other is a)never used, and b)does nothing useful.


    Somewhere in your code, you need to have some actual data, or else you're going to get junk results.
    this is just SOME of my code. The entire code would be too large to print. it runs ok, i just need someone to look at case 3 and 4 in the switch statement.
    Can someone please explain to me why i cant transfer values inputted in the 3rd case and output them in the 4th ?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Based on the code you have posted, you can quite easily do so; running your program above (after fixing all the endl errors and the like) gives a (meaningless, but valid) 1.49095e-38 as total sales. Your comment that an address is printing when you try to print total_sales in case 4 indicates that you have vastly different code on your machine than what is posted.

    If you don't want to post the entire code, that's fine; but in that case, you still need to create a complete minimal example that can be compiled by other people and that demonstrates the problem.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    When using switch statements with cases that have a lot of code prefer to place the code in a function and call that function from the case. It makes your code a lot easier to read. I would help but your code is not very friendly to the eye and therefore I really don't want to go through it.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    What he meant is, indent your code properly so people will read it.

    There may even be an option in your IDE that will do it for you.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Actually the indenting isnt' horrible but the switch statements are horrendous. Most of the formatting issues with comments being at the left margin and so forth are probably due to a bad copy and paste. If you don't manually edit your post after a copy and paste that is normally what it looks like since the board doesn't retain the formatting all that well. Using spaces instead of tabs in your IDE helps a bit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  4. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM
  5. Changing bkgrnd color of Child windows
    By cMADsc in forum Windows Programming
    Replies: 11
    Last Post: 09-10-2002, 11:21 PM