Default not working in switch statement!!

Pages: 123
no i didn't...lol...i missed that
In general, duplicating code, if even only a few lines of it, is an indication of bad design.

Lines 9 & 10 are duplicates of lines 36 and 37.

Functionally speaking, the line 33 while loop duplicates the switch statement checking. The default case ONLY executes if the input is not S, W, or B, so there's no point to retest that the input is not S, W, or B.

This is a minor modification, and may seem trivial for this example, but it's a good habit to get into as your projects get more complicated:
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
void choice1 (int &pStrength, int &bStrength, double &milkMoney)
{
     char choice   = ' ';
     
     cout <<"How do you wish to get to school today?" << endl;

     do{
		 cout <<"Please press: " << endl;
		 cout <<"   S to take the school bus" << endl <<"   B to ride your bike" << endl;
		 cout <<"   W to walk to school" << endl;
		 cin  >> choice;
		 choice = toupper(choice);

		 switch (choice)
		 {
				case 'S':
					 {
						 outcomes (pStrength, bStrength, milkMoney);
						 return;
					 }
				case 'W':
					 {
						 outcomes (pStrength, bStrength, milkMoney);
						 return;
					 }
				case 'B':
					 {
						 outcomes (pStrength, bStrength, milkMoney);
						 return;
					 }
				default:
					 {
							  cout << " You've entered an invalid response." << endl;
						 
					 }
		 }
	}while(true);
}

If you decided to change how you prompted the user for input, the above code would be easier to modify. Only one location to change instead of two. You could easily forget to change the second input.

If you decided to add more options, the above code would be easier to modify. In your original code, you'd have to modify the original prompt, add a new case statement, update your while statement, and update your invalid response output. In the above code, you modify the original prompt and add a new case statement.
Now I feel just stupid...that was a lot easier and simpler and not only that, but the way its setup, when the program is ran an invalid response is entered, it doesn just what I would have liked to see happen but didn't know how to do.

I learned a little bit about a do/while loop but missed the day that he covered it in detail. I'm sure this was in it that bit of information that I miss. This works a lot smoother and simpler. Thank you.
Can someone post an example of a bool function? or a function that returns a string? I'm lost when it comes to that.
Trivial example of a bool function:
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
bool evenNum(int num){
	return !(num%2);
}

int main(int argc, char *argv[])
{

	int val;
	cout << "Input a number: ";
	cin >> val;
	
	//Function call can be made directly inside a conditional
	//return value will be used in evaluating the expresion
	if(evenNum(val))
		cout << "Number is even.";
	else
		cout << "Number is odd.";
	
	cout << endl;
	
	//Function call can be made as an assignment to another variable
	bool checkEven = evenNum(val);
	
	if(checkEven)
		cout << "Number is still even.";
	else
		cout << "Number is still odd.";

}


As for strings, are you talking C strings or STL strings? The latter are far easier to work with.
try to include

#include<cctype>

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
void choice1 (int &pStrength, int &bStrength, double &milkMoney)
{
     char choice;
     
     cout <<"How do you wish to get to school today?" << endl;
     cout <<"Please press: " << endl;
     cout <<"   S to take the school bus" << endl <<"   B to ride your bike" << endl;
     cout <<"   W to walk to school" << endl;

     do
     {
          switch (choice)
         {
          case 'S':
	    {
	         outcomes (pStrength, bStrength, milkMoney);
                 break;
             }
          case 'W': 
             {
                 outcomes (pStrength, bStrength, milkMoney);
                 break;
             }
          case 'B':
             {
                 outcomes (pStrength, bStrength, milkMoney);
                 break;
             }
          default:
             {
                  cout <<"You've entered an invalid response. Please only select 'S','B',or'W'."<<endl;
                  cout<<"Enter your choice: ";
                  cin >> choice;
	          choice = toupper(choice);
              }             
	    }
	 }while (choice == 'S' || choice == 'W' || choice == 'B');
}


good luck!
Last edited on
Glad to see you got your problem solved.

As for returning strings C style look below:-

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
#ifdef __cplusplus
#include <cstdio>
#else
#include <stdio.h>
#endif

char *hello_message (void)
{
  /*
   * Must be static.
   * Otherwise the memory will be unallocated before you get to use it.
   */
    static char message[] = "Hello, world!";
    return (char*)message;
}

int main (int argc, char **argv)
{
    puts (hello_message()); /* Will print "Hello, world!" */

    /* I like to pause sometimes... */
    printf ("Press RETURN to proceed...");
    getchar ();

    return 0;
}
okay. So i created this as a value-returning function but now I don't know how to call to 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
string playerInfo (int &num)
{
       string firstName  = "";
       string lastName   = "";
       string fullName   = "";
       
       fullName = firstName + " " + lastName;
       
       cout <<" Player, please enter your First Name." << endl;
       cin >> firstName;
       cout <<"Thank you. Now, please enter your Last name." << endl;
       cin >> lastName;
       cout <<"Thank you. Welcome to Bully Avoidance 101 " << fullName <<"." << endl;
       cout <<"This game is simple. Choose your method of getting to school." << endl;
       cout <<"If the method you choose avoids the bully, you are able to get" << endl;
       cout <<"some milk for that day which will increase your strength. If " <<endl;
       cout <<"you happen to run into the bully on your way to school, you have" <<endl;
       cout <<"two options. Option one is to fight. If you beat him, you gain strength." << endl;
       cout <<"But if you lose, the bully takes your money and gains strength." << endl;
       cout <<"At the end of the game, whoever has the most strength wins." << endl << endl;
       cout <<"What d'ya say? Wanna play?"<<endl<<"Press 1 to begin." << endl << endl;
       cin >> num;
       
       return (fullName);
}


Like, I want to use the fullName field in another function but how do I call to it?
You have a flaw in your logic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string playerInfo (int &num)
{
       string firstName  = "";
       string lastName   = "";
       string fullName   = "";
       
       fullName = firstName + " " + lastName;  // since firstName and lastName are both empty strings, this means 'fullName'
                 // will just be " " (a single space)
       
       cout <<" Player, please enter your First Name." << endl;
       cin >> firstName;
       cout <<"Thank you. Now, please enter your Last name." << endl;
       cin >> lastName;   // here is where you get first and last name from the user

       //  so you'd have to set 'fullName' after that, not before it. 


as for calling the function:

1
2
int num;
string fullname = playerInfo(num);
Thank you.
New problem showed up now. When I run this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main ()
{
    srand (time(0));
    int num   = 0;
    string fullName = playerInfo (num);
    
    playerInfo (num);
    dowStart (num);
    
    
    dailyRoutine (num, fullName);
    
    system ("pause");
    return 0;
    
}

instead of going into the next function after playerInfo when I press 1, it simply starts the playerInfo function over again.
I tried moving the dowStart function from the int main function and adding it to playerInfo function. The problem persists. When I run the program, it prompts for names and displays the info. When I push 1 to start the game, it starts over with enter your name. after running through it a second time, it starts the game. How do I get it to go into the game the first time through? Do you need the whole code to see what I'm talking about?
We... could use a glimpse of the code you used, because that sounds like a loop with no break.

-Albatross
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <iostream>
#include <algorithm>
#include <time.h>
using namespace std;

void dailyRoutine (int &num, string &fullName);
void choice1 (int &pStrength, int &bStrength, double &milkMoney);
void outcomes (int &pStrength, int &bStrength, double &milkMoney);
void fight (int &pStrength, int &bStrength, double &milkMoney);
void walk (int &pStrength, int &bStrength, double &milkMoney);
void days (int &num);
void dowStart (int &num);
string playerInfo (int &num);

int main ()
{
    srand (time(0));
    int num   = 0;
    string fullName = playerInfo (num);
    
    playerInfo (num);
    dailyRoutine (num, fullName);
    
    system ("pause");
    return 0;
    
}

void dailyRoutine (int &num, string &fullName)
{
     int pStrength     =  100;
     int bStrength     =  101;
     double milkMoney  = 1.25;
     
     for (num =1; milkMoney >= .25; num ++)
     {
         days (num);
         cout <<"Your current strength is " << pStrength <<"." << endl;
         cout <<"The bully's current strength is "<< bStrength <<"."<< endl;
         cout <<"You have "<< milkMoney <<" left to spend on milk." << endl << endl;
         choice1 (pStrength, bStrength, milkMoney);
     }
     
     if (pStrength > bStrength)
    {
         cout <<"With Final Strength of " << pStrength <<" our winner is..." << endl << endl << endl;
         cout <<"                        " <<fullName<< endl << endl;
         cout <<"CONGRATULATIONS!!! YOU DEFEATED THE BULLY ONCE AND FOR ALL!" << endl;
    }
    else if (bStrength > pStrength)
    {
         cout <<"With Final Strength of " << bStrength <<" our winner is..." << endl << endl << endl;
         cout <<"                       THE BULLY" << endl << endl;
         cout <<" Tough break kid. You tried, I grant you that. Maybe next week." << endl;
    }
    else if (pStrength == bStrength)
    {
         cout <<"With a matching strength of " << pStrength <<" our winner is..." << endl << endl << endl;
         cout <<"                        NO ONE!!!" << endl << endl;
         cout <<" Because you and the bully were tied at the end of the game, we have no winner." << endl;
         cout <<" Please play the game again to try again at beating the bully." << endl;
    }
}

void choice1 (int &pStrength, int &bStrength, double &milkMoney)
{
     char choice   = ' ';
     
     cout <<"How do you wish to get to school today?" << endl;

     do{
		 cout <<"Please press: " << endl;
		 cout <<"   S to take the school bus" << endl <<"   B to ride your bike" << endl;
		 cout <<"   W to walk to school" << endl;
		 cin  >> choice;
		 choice = toupper(choice);

		 switch (choice)
		 {
				case 'S':
					 {
						 outcomes (pStrength, bStrength, milkMoney);
						 return;
					 }
				case 'W':
					 {
						 outcomes (pStrength, bStrength, milkMoney);
						 return;
					 }
				case 'B':
					 {
						 outcomes (pStrength, bStrength, milkMoney);
						 return;
					 }
				default:
					 {
							  cout << " You've entered an invalid response." << endl;
						 
					 }
		 }
	}while(true);
}

void outcomes (int &pStrength, int &bStrength, double &milkMoney)
{
     int randNum    = 0;

     randNum = 1 + rand() % (2 - 1 + 1);

     if (randNum == 1)
     {    
          cout << "You did not run into the bully today." << endl;
          cout << "You bought milk today for 25 cents." << endl;
          cout <<"Because you had your milk for the day, your" << endl;
          cout << "strength has increased by 1 point." << endl << endl << endl;
          pStrength = pStrength + 1;
          milkMoney = milkMoney - .25;
     }
     else if (randNum == 2)
     {
          walk (pStrength, bStrength, milkMoney);
          cout << endl << endl << endl;
     }
}

void walk (int &pStrength, int &bStrength, double &milkMoney)
{
     char choice2  = ' ';
     
     cout << endl << "Good Grief! You ran into the bully today. What would you like to do?" << endl;
     do{
		 cout <<"Please press: " << endl;
		 cout <<"   F to stand up for yourself and fight" << endl <<"   R to run away like think the little girl you are" << endl;
		 cin  >> choice2;
		 choice2 = toupper(choice2);

		 switch (choice2)
		 {
				case 'F':
					 {
						 fight (pStrength, bStrength, milkMoney);
						 return;
					 }
				case 'R':
					 {
						 cout << "You give the bully your 25 cents. He lets you go but you don't get any milk today." << endl;
                         cout << "He takes your 25 cents and drinks your milk, adding 1 point to his strength." << endl << endl;
                         bStrength = bStrength + 1;
                         milkMoney = milkMoney - .25;
						 return;
					 }
				default:
					 {
							  cout << " You've entered an invalid response." << endl;
					 }
		 }
	}while(true);
}

void fight (int &pStrength, int &bStrength, double &milkMoney) 
{
    int pPunch       =   0;
    int bPunch       =   0;
    pPunch = 1 + rand() % (pStrength - 1 + 1);
    bPunch = 1 + rand() % (bStrength - 1 + 1);
    
    cout <<"You hit the bully with a punch strength of " << pPunch <<"." << endl;
    cout <<"The bully hit you with a punch strength of " << bPunch <<"." << endl << endl;

    if (pPunch > bPunch)
    {
               cout << "Yea! You defeated the bully." << endl;
               cout << "You bought milk today for 25 cents. Because you had your milk for the day," << endl;
               cout << " your strength has increased by 1 point." << endl; 
               pStrength = pStrength + 1;
               bStrength = bStrength - 1;
               milkMoney = milkMoney - .25;
    }
    else if (pPunch < bPunch)
    {
               cout << "Awww... Poor thing. The bully has defeated you." <<endl <<"And worse yet, he took your 25 cents" << endl << "and drank your milk." << endl;
               cout << "He adds 1 point to his strength while you take the day to recover." << endl <<"You lose 1 point." << endl;
               pStrength = pStrength - 1;
               bStrength = bStrength + 1;
               milkMoney = milkMoney - .25;
    }
    else if (pPunch == bPunch)
    {
               cout <<"You and the bully fight to a tie." << endl;
               cout <<"You lose your 25 cents during the fight." << endl;
               cout <<"Neither of you change strength." << endl << endl;
               milkMoney = milkMoney - .25;
    }
    
}

void days (int &num)
{
     if (num == 1)
     {
          cout << "Today is Monday." << endl;
     }
     else if (num == 2)
     {
          cout << "Today is Tuesday." << endl;
     }
     else if (num ==3)
     {
          cout << "Today is Wednesday." << endl;
     }
     else if (num ==4)
     {
          cout << "Today is Thrusday." << endl;
     }
     else if (num ==5)
     {
          cout << "Today is Friday." << endl;
     }
}

void dowStart (int &num)
{
     if (num == 1)
     {
          cout << "Welcome to town. You must be new here." << endl;
     }
     else  
     {
           while (num != 1)
           {
                 cout << endl << "I said press 1 to start. When you make your game you may have the " << endl <<"player press " << num <<" to start your game. This is my game." << endl;
                 cout << endl << "Please prese 1 to begin." << endl;
                 cin >> num;
                 cout << endl;
           }
     }
}
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
string playerInfo (int &num)
{
       string firstName  = "";
       string lastName   = "";
       string fullName   = "";
       
       cout <<" Player, please enter your First Name." << endl;
       cin >> firstName;
       cout <<"Thank you. Now, please enter your Last name." << endl;
       cin >> lastName;
       cout << endl << endl << endl;
       fullName = firstName + " " + lastName;
       
       cout <<"Thank you. Welcome to Bully Avoidance 101 " << fullName <<"." << endl;
       cout <<"This game is simple. Choose your method of getting to school." << endl;
       cout <<"If the method you choose avoids the bully, you are able to get" << endl;
       cout <<"some milk for that day which will increase your strength. If " <<endl;
       cout <<"you happen to run into the bully on your way to school, you have" <<endl;
       cout <<"two options. Option one is to fight. If you beat him, you gain strength." << endl;
       cout <<"But if you lose, the bully takes your money and gains strength." << endl;
       cout <<"At the end of the game, whoever has the most strength wins." << endl << endl;
       cout <<"What d'ya say? Wanna play?"<<endl<<"Press 1 to begin." << endl << endl;
       cin >> num;
       dowStart (num);
    
       
       return (fullName);
}
That's the whole thing. Now, can you see anywhere that would loop the playerInfo function?
I stand corrected. You just had playerInfo one time too many.
In main():
1
2
3
string fullName = playerInfo (num);
    
    playerInfo (num);


-Albatross
So any ideas on how to fix the problem presented?
Just delete line 21.
Why does that work? It works but I don't know or see why it works. Can you you explain this to me?
Pages: 123