if else with a switch

Can anyone please tell me ho to use if/else with a switch to make menus??

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
// This Program is for Homework #5, Frederick A. Bowser
// Define named constants; line, item prices, etc.
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include <cstdlib>

using namespace std;
int main ()
{
	const string menu =
	" [01] Meat	   [02] Bakery  [03] Fruits     [04] Frozen \n"
	" [05] Milk & Diary [06] Drinks	[07] Cereal	[08] Snacks \n";
	cout << menu << endl;
	cout << "______________________________________________________________________" << endl;
double A,B,C,D;
	
	int choice;
	choice = 1,2,3,4,5,6,7,8;
	cout << "Which would you like = " << endl;
	cin >> choice;
if (choice == 1)
	{ cout <<  " [A] bacon	   [B] hotdog  [C] beef    [D] steak \n";}
if (choice == 2)
	{ cout <<  " [A] bread	   [B] cake    [C] cupcake [D] cookies \n";}
if (choice == 3)
	{ cout <<  " [A] apples   [B] Banana  [C] grapes  [D] strawbarry \n";}
if (choice == 4)
	{ cout <<  " [A] pizza	   [B] wings   [C] TvDinners [D] ice \n";}
if (choice == 5)
	{ cout <<  " [A] milk	   [B] cheese  [C] yogurt    [D] butter \n";}
if (choice == 6)
	{ cout <<  " [A] soda	   [B] water  [C] fruit juice  [D] ice tea \n";}
if (choice == 7)
	{ cout <<  " [A] Corn Flakes [B] Apple Jacks  [C] Coco Puffs  [D] Chex Mixs  \n";}
if (choice == 8)
	{ cout <<  " [A] Honey Buns [B] Chips  [C] Candy  [D] Gum  \n";}
cout << "_________________________________________________________________________" << endl;

cout << " what would you like = ";

	cin >> A,B,C,D;

A= 2.25; 
	B= 1.52;
	C= 3.85;
	D= 1.75;
	
	




	return 0;
}
 
Last edited on
What kind of menu would you like? We can't be of much help without a little more detail.
I put the code in the msg. But my teacher wants it to be all Switch/case?
Im like WHAT YOU MEANNNNN??!!?
Your teacher means use a switch statement.
1
2
3
4
5
6
7
8
9
10
 
switch(choice) 
{
    case: 1
    //Do something 
    break;
    case: 2 
    //Do something
    break;
}


In place of your if statements.
what do you mean in how to use ?,,, the syntax, style etc.. ?

by the way, here is a simple example:

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
/****************************
 *   A Simple Program using *
 * if - else if - else      *
 ****************************/

#include <iostream>

using namespace std;

int main ()
{
    int nNumber;
    
    cout << "Select a Drink below:" << endl;
    cout << "[1] Coca - Cola" << endl;
    cout << "[2] Sprite" << endl;
    cout << "[3] Royal" << endl;
    
    cin >> nNumber;
    
    if ( nNumber == 1 ) { // we use  == operator to compare 2 values if it is equal
        cout << "You Choose Coca - Cola !";
        /* Other COMMANDS */
    } //  the braces '{ }' can be ommited if you only have 1 statement to be performed
      // under an if condition
    else if ( nNumver == 2 ) {
    
        /* put the commands you want to perform beetween the braces under this condition*/
    }
    else if ( ... ) {
        /* I assume you know what to do next */
    }
    else { //  If the condition is never met in the linked if - else if else
           // you can use else to do other commands:
           cout << "An Invalid Choice is Made !"; << endl;
    }
    return 0;
}


a clearer one:
http://www.cplusplus.com/doc/tutorial/control/
You have some issues with what your code does.

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
// This Program is for Homework #5, Frederick A. Bowser
// Define named constants; line, item prices, etc.
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include <cstdlib>

using namespace std;
int main ()
{
	const string menu =
	" [01] Meat	   [02] Bakery  [03] Fruits     [04] Frozen \n"
	" [05] Milk & Diary [06] Drinks	[07] Cereal	[08] Snacks \n";
	cout << menu << endl;
	cout << "______________________________________________________________________" << endl;
double A,B,C,D;
	
	int choice;
	choice = 1,2,3,4,5,6,7,8;  //this line does nothing for your code because of the next line
	cout << "Which would you like = " << endl;
	cin >> choice;
if (choice == 1)
	{ cout <<  " [A] bacon	   [B] hotdog  [C] beef    [D] steak \n";}
if (choice == 2)
	{ cout <<  " [A] bread	   [B] cake    [C] cupcake [D] cookies \n";}
if (choice == 3)
	{ cout <<  " [A] apples   [B] Banana  [C] grapes  [D] strawbarry \n";}
if (choice == 4)
	{ cout <<  " [A] pizza	   [B] wings   [C] TvDinners [D] ice \n";}
if (choice == 5)
	{ cout <<  " [A] milk	   [B] cheese  [C] yogurt    [D] butter \n";}
if (choice == 6)
	{ cout <<  " [A] soda	   [B] water  [C] fruit juice  [D] ice tea \n";}
if (choice == 7)
	{ cout <<  " [A] Corn Flakes [B] Apple Jacks  [C] Coco Puffs  [D] Chex Mixs  \n";}
if (choice == 8)
	{ cout <<  " [A] Honey Buns [B] Chips  [C] Candy  [D] Gum  \n";}
cout << "_________________________________________________________________________" << endl;

cout << " what would you like = ";

	cin >> A,B,C,D;  //this line does nothing.  You set everything below regardless of user input

A= 2.25; 
	B= 1.52;
	C= 3.85;
	D= 1.75;
	
	




	return 0;
}
Topic archived. No new replies allowed.