assignment problem. unsure what title to describe my problem

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#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";   
                     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');
}
Do you want the values outputted automatically, so that regardless of whether you chose 3 or 4, and execute what is in 4?

If so, an if statement outside of the switch block might be useful.

And check your code in 3: What happens if total_sales is equal to 75?

-Albatross
Last edited on
if the total_sales is 75, wouldnt the second be executed ?
i guess i should use >= and <=
You may find it helpful to move the code for each case: statement into functions.

That way it is easier to see the program flow separate from the details.

A bit like this. But its entirely up to you:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
void display_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
}

int get_menu_input()
{
	int number;
	cin >> number;
	return number;
}

void display_the_company_logo()
{
	// ...
}

void display_receipt()
{
	// ..
}

void calculate_weekly_sales()
{
	// ..
}

int main()
{
	do
	{
		display_the_menu();
		number = get_menu_input();
		cout << ("\n\n");

		switch(number)
		{
			case 1: //<-----  const = 1
				display_the_company_logo();
				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;

				switch(answer)
				{
					case 'n':
						cout << "\n\n\nThank You!\n\n";
						break; 
					case 'y': 
						calculate_weekly_sales();
				}
				break;  // REMOVE THIS if you want case 4 to always run after case 3:

				case 4:
					display_receipt();
		}
	}
	while(1);
}


If you look at line 66 above if you remove that break statement then if the user opts for 3, number 4 will also run. The break; statement stops that happening.
Last edited on
thank you Galik.
Topic archived. No new replies allowed.