switch statement

i'm having trouble:
1. grasping the concept of switch
2. making the program loop to the menu before the switch.

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// newproject2.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <fstream>
#include <string>
#include <math.h>
#include <cstdlib>

using namespace std;

class userMenu{
public:
	userMenu();
	string homeMenu;
	string add;
	string remove;
	string edit;
};		//webUser menu class

userMenu::userMenu(){
	cout<<"Select an option below (1,2,3)"<<endl
		<<"1.  Add Booking"<<endl
		<<"2.  Edit Booking"<<endl
		<<"3.  Remove Booking"<<endl;
};


class addBooking{			
	public:
	addBooking();
	string addMenu;
	string bookName;
	int startDate;
	int currentDate;
	int lengthStay;
};							//add booking class

addBooking::addBooking(){
	cout<<"Welcome to the Add Booking Menu!:"<<endl;
};			 //contructor  

class rates{
public:
	rates();
	int displayMonthRates();
	int displayWeekRates();
	int displayDayRates();
	int monthRate;
	int weekRate;
	int dayRate;
};

rates::rates(){

	dayRate=100;
	weekRate=525;
	monthRate=1575;
};





class newUser{				//new user class

public:
	newUser();
	string name;
	string password;
	
};		
newUser::newUser(){		//new user constrctor

	

};	 


int main(){

	

	/*ifstream inFile;

	inFile.open("reservationLog.txt");
	if(inFile.fail()){
		cout<<"File unsuccessfully opened."<<endl
			<<"Please check to see if file exists."<<endl;
		exit(1);
	}

	cout<<"\n the file has been successfully opened for reading."<<endl;

*/
	cout<<"Welcome new user!:"<<endl;

	int add=1;
	int edit=2;
	int remove=3;
	newUser newUserOne;
	int opselect;

	
	cout<<"Please enter user name:"<<endl;
	cin>>newUserOne.name;
		cout<<endl;
	cout<<"Please enter passworrd:"<<endl;
		cin>>newUserOne.password;

		userMenu menuOne;
		cout<<menuOne.homeMenu<<endl;
		cin>>opselect;
		switch(opselect){
		case 1:{
			
				cout<<"If Booked 2 weeks in advanced, 3% off lodging only"<<endl;
				addBooking addMenu;
				cout<<"Please enter book name:"<<endl;
				cin>>addMenu.bookName;
				cout<<endl<<"please enter toady's date(yyyymmdd):"<<endl;
				cin>>addMenu.currentDate;
				cout<<endl<<"please enter start date:(yyyymmdd)"<<endl;
				cin>>addMenu.startDate;
			 
				rates showRate;

				cout<<"Monthly Rate:"<<showRate.monthRate<<endl
					<<"Weekly Rate:"<<showRate.weekRate<<endl
					<<"Daily Rate:"<<showRate.dayRate<<endl;

				cout<<"Please enter length of stay:"<<endl;
				cin>>addMenu.lengthStay;
				cout<<addMenu.lengthStay/30<<"Months"<<endl
				<<addMenu.lengthStay%30/7<<"Weeks"<<endl
				<<addMenu.lengthStay%30%7<<"Days"<<endl;
			   }
			break;
			   
		case 2:{
			if(cin>>edit){
				cout<<"Welcome to the edit booking menu!"<<endl;
			}
				break;
			}

		case 3:{
			if(cin>>remove){
				cout<<"welcome to the remove booking menu!:"<<endl;
			}
				break;
			}
		}
			   
			return 0;
}
		
		
if(cin>>edit)
You realize this will ask the user to input again, and this will always be true unless they type in something that cin can't understand?

Your switch statement already takes care of which option the user selected, you should not be trying to check it.
I can tell you how the switch works. Its prototype looks like this

1
2
3
4
5
6
function(condition){
                 case constant:    //statements to execute 
                 case constant:
                 case constant: //statements to execute

               }           


Its really mainly used to decide from a big selection of options such as a menu or different paths but if we look at an if statement its the same as:

1
2
3
4
5
6
if(opselect == 0){
            //insert statements to do if true
break;
if(opselect==1){
//insert statements to do if true
}};


Just a nested IF.
Switchs just skip what doesnt match the criteria you gave until it finds a match and then executes. Same as a nested if, in this case if i select one then it will skip the first if and execute the next. The more options the more if's you need. Which is why switch is an option in some cases.

Dont use switches outside of menu options, not good for logic. IF's for logic, skip the switch. Switch for menu options, skip the if.

I cant answer your other question but hopefully this helps

I would remove the IF's and re-write your code to match the prototype. such as instead of what you have:

1
2
3
switch(opselect){
           case1:  cout<<"Welcome to the edit booking menu!"<<endl;
			}



and so on.

EDIT: A suggestion about your second question, maybe put it all in a function before the switch or with the switch. That way if you ever want to avert back to the menu you can just call the function. Just a thought.
Last edited on
okay, i get the concept, i removed the cin>>edit and cin>>remove. it makes sense now. what i'm really trying to do is after the case is complete (in this example
1
2
3
4
5
6
cout<<"Please enter length of stay:"<<endl;
				cin>>addMenu.lengthStay;
				cout<<addMenu.lengthStay/30<<"Months"<<endl
				<<addMenu.lengthStay%30/7<<"Weeks"<<endl
				<<addMenu.lengthStay%30%7<<"Days"<<endl;
			   }
)

i want it to return to the beginning of the whole menu process. idk if that involves code completely different.
I'll admit i'm a little unsure if this is the BEST way but it should work.

Wrap your initial menu in a function first:

1
2
3
initialmenu(){
 //statements
}


then anytime you need to bring the user back you can call that function.

you may be able to use a while or something but for this i would use a function.
Don't use recursion to start something over. Use a looping structure such as do-while. ;)
i'm trying to think of how to do a do-while loop, like do the whole process but idk for the while part. im thinking something like while cin>>return, but then that means that it will only happen once the user enters return. maybe give a sample code in mine, not just an abstract instance?
do

menu statements

while (opselect==0) ? So if you press 0 it should execute the menu but it should show the menu once as well. Just gotta work with it and tweak it till you get your results if this doesnt achieve it.

Topic archived. No new replies allowed.