Help with Case/Switch

So this was an assignment I had to do for my Intro to Comp Science I, and the main objective was to write the functions that are seperated by each comment saying which it is.

My question is more about the void running() funciton I wrote. I wanted to have it execute all the functions above to make it a simple progrem runnings each. Using a case/switch and a while loop to do this. With cases 1,2,3 ONLY, it works as I expect. But the moment I add in the 4,5,6th cases it just cycles through them instead of returning back to the start of the while loop.

Can anyone help me understand what is going wrong?

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
  /* For some reason the while loops case switches have an issues where they are cycling
thru the cases and not returning to the base while loops. IT was ok until I added cases 4,5,6*/
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;
//global space

/* Function for the first problem. Wholesale markup
*/
double retailPrice(int whole,double markup){
    cout << markup << endl;
	if(markup == 100){
		whole = whole*2;
		cout << "The retail price should be " << whole << " dollars. " << endl;}
	else{
		markup = markup / 100; 
		markup = 1 - markup;
		markup = 1 + markup;
			whole = whole * markup;
		cout << "The retail price should be " << whole << " dollars. " << endl;}
}

/* Coin toss simulation. Question 2
*/
int cointoss(){   
  int coin;
  srand (time(NULL));
  int flip = (rand()%2)+ 1 ;
  if(flip == 1){
      cout << "You got heads."<<endl;}
  else{
      cout << "You got Tails. "<<endl;}

  }


/* 3rd question. Check if a number is odd or even
*/
int evenCheck(int number){
	if( number % 2 == 0){
		cout << "That number is even.\n";}
	else{
	cout << "That number is odd.\n";}
}


/* 4th Question determine if a letter is a consonant or vowel.
*/
bool isVowel(char letter) {
   if (letter == 'a' || letter == 'e' || letter == 'i' || letter =='o' || letter == 'u') {
         return true;
       }
    return false;
}

int countVowels(string str) {
  int count = 0;
  int pos = 0;
  while (pos < str.length()) {
      if (isVowel(str.at(pos))) {
          count=count;
      }
      else{
      ++count;}
      ++pos;
  }
  cout << "There are " << count << "consonants."<<endl;}
  
  
  
  /* 5th question 
  Grades */
  
  
  int gradeChecker(int grade){
if (grade == 100) {
  cout << "Above Average" << endl;
}
if (grade >= 90 && grade <=99) {
  cout << "A" << endl;
}
if (grade >= 80 && grade <=89) {
  cout << "B" << endl;
}
if (grade >= 70 && grade <=79) {
  cout << "C" << endl;
}
if (grade >= 60 && grade <=69) {
  cout << "D" << endl;
}
if (grade <= 59){
  cout << "You failed" << endl;

}
  }

void running(){
	int run = 1;
	int choice;
	while(run == 1){
			cout << "Which program would you like to run?\n" << "1: Markup Program\n" << "2: Coin Toss Simulator\n"<<"3: Is it even?\n"<<"4: Consonant Counter\n"<<"5:Grade check\n";
			cin >> choice;
			switch(choice){
				int m;
				int w;
				case 1:  cout << "What is the wholesale price? "<< endl;
				cin >> w;
				cout << "Markup %? "<< endl;
				cin >> m;
				retailPrice(w,m);
				case 2: cout << "Flip the coin?\n"<<"1:yes | 2:no"<<endl;
				int toss;
				cin >> toss;
				if(toss == 1){
					cointoss();
				}
				case 3: cout << "Type a number."<<endl;
				int userN;
				cin >> userN;
				evenCheck(userN);
				case 4:{ cout <<"Type a word."<<endl; // not sure why the program 
				string word;// continues from case 4 to 5 on it's own 
				cin>>word;//followed by closing itself out even, meaning case 6 is being hit
				countVowels(word);}
				case 5: cout << "What was your grade(i.e 88)" << endl;
				int input;
				cin >> input;
				gradeChecker(input);
				case 6: run = 0; 
				
				
			    
			}
			}
}
int main()
{
	running();
	
	}
switches fall through until they see a break.
so
switch(x)
case 1:
blah;
case 2:
blah;
case 3:
blah;
break;

will do 1, 2 and 3 if you enter 1, 2 and 3 if you enter 2, and 3 if you enter 3. If you put a break on the 1 and 2 cases, it would only do the one you input.

general tips...
- this feature is the only time I use switches. You can't write the fall-through as efficiently with if statements for some logic. This is rare, in my experience. They are very limited (only integers, only exact match conditions) compared to if statements and this feature is the only thing it can do that if statements cannot.
- switches should always have a default: branch.


Last edited on
Ok, thank you.

So each case needs to have a break added to it basically which would cause it to cycle back to the beginning of the While loop.


And yes, usually I don't use Case/Switches, but ti seemed like it was very appropriate for the task I was trying to do; At least for as far as my knowledge goes.

Thanks again!
case 6: would expect 6 to be entered, but by the looks of it I think you're intending it to be the default statement that must be run when the user clicks on any other number.

For that you must use default://write code below this . This case will be run when none of the other cases are matched.

I assumed this because there wasn't an option 6 in your menu and you've put run = 0 which would stop your while loop ;p. So even if that menu were to have an option 6, how about instead telling the user to click on any other key to exit? Makes life simple eh?
Ok, thank you.

So each case needs to have a break added to it basically which would cause it to cycle back to the beginning of the While loop.


yes, for your situation, this is what you need.
but you also need to understand when to not use the break, to exploit the power of switches and their unique ability.
Topic archived. No new replies allowed.