help with do-while loop

I'm trying to use a do-while loop to prompt the user until he/she inputs the correct input but when the user does input one of the correct input it still continues to prompt the user. What should I do? Help me understand where I went wrong please.

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
/*************
Title:	Lab 7
Author:	C. Smith
Date:	29 Nov 2013

Objective - To write a program that will predict the future
Method - 
1. Use the main procedure to
a.	Prompt the user for his/her lucky number. Use this number as input to the srand function found 
	in stdlib.h
b.	Prompt the user for which life topic he wishes his predictions based.  Based on the user's 
	response call one of three functions. Suggestions are love, finance, and school, but you may 
	use three of your own choosing. Use a nested if-else statement to control which function is called.

2. Each function will
c.	Call the rand function
d.	Use value generated by the rand function and modulo division as the selector expression in a 
	switch statement.
e.	The switch statement will print out the predictions.
f.	Allow for five different predictions per function.

3.	Prompt the user to see if they want another prediction. Keep executing the program until the 
	user tells you to quit.  Make sure that you code for all possible user inputs, including wrong ones.

Hand In -
1.	A copy of your program
2.	A copy of the output. As the output scrolls past the black box, keep selecting the output and 
	pasting it into the text editor as you go.  Make sure that the output shows that your program 
	will work under all possible scenarios. You may have to run the program more than once to do this.

Have fun!!

The function srand takes a number (called a seed) and sets the starting point for a random number 
generator.  For example, if the seed for the random number generator is in the variable num, the 
syntax of the function call would be:
srand(num);

The call to srand is only used once in a program (typically immediately after the word main, or 
after the first set of declarations).

The function rand() returns a random number between 1 and RAND_MAX (32767 in VC++).  If you type
cout<<rand();

a random number will be printed.  In order for the random number to be between 1 and N use the following formula:
rand()%N + 1

For example:   x = rand()%7 + 1;  places a random number between 1 and 7 into the variable x.

***************/

#include<iostream>
#include<string>
#include<sstream>
#include<cstdlib>

using namespace std;

//function Relationship
void firstworldproblems(void)
{
	int	x = rand() % 5 + 1;

	switch (x)
	{
	case 5:
		cout << " Roses are red, voilets are not blue, smile today if you want your true" 
			"\n love to find you."; break;
	case 4:
		cout << " Walk up to your crush, smile, extend your hand and say hello and your day will "
		"\n never be the same."; break;
	case 3:
		cout << " Don't be shy, take some risk in your relationships, today is the day."; break;
	case 2:
		cout << " Go to a comedy club, watch something funny or listen to something funny, "
				"\nthis will help strengthen your relationship."; break;
	case 1:
		cout << " Step outside your comfort zone if you want to meet new people."; break;
	}
}

//function School Life
void thuglyfe(void)
{
	int	x = rand() % 5 + 1;
	switch (x)
	{
	case 5:
		cout << " You will get a B + if you study for that upcoming test."; break;
	case 4:
		cout << " School will be closed soon."; break;
	case 3:
		cout << " Your teachers will assign homework for thanksgiving."; break;
	case 2:
		cout << " Find a study buddy, they will be the reason you start to excel in class."; break;
	case 1:
		cout << " Review your previous class notes so you wont be surprise of the pop quiz."; break;
	}
}

//Function Financial Situation
void donaldtrump(void)
{
	int	x = rand() % 5 + 1;
	switch (x)
	{
	case 5:
		cout << " Invest in yourself and in life if you wish to be succesful."; break;
	case 4:
		cout << " Donate a dollar, you will find a bill worth way more later."; break;
	case 3:
		cout << " You can not be cheap with everything, especially when it comes to your health and needs."; break;
	case 2:
		cout << " Put your needs over your wants if you wish to be financly regret free."; break;
	case 1:
		cout << " Learn to cook, the fast food you eat is doing a lot more than satisfying your hunger."; break;
	}
}

char problemsolving(char&)
{
	char choice;

	do
	{
		cout << "\n\n Would you like another prediction? Y/N" << endl << endl;
		cin >> choice;
		
	} while (choice != 'y' || choice != 'n');
	return choice;

}

int main()
{
	unsigned seed;
	char life;
	bool valid = false;
	char choice;

	//Talking to user

	while (!valid)
	{
		valid = true;

		cout << " Welcome to the Matrix.\n";
		cout << " You have been automaticly connected to the Psychic Computer Network.\n";
		cout << " My name is Dionne, and I will be your psychic computer guide.\n";
		cout << endl << " Before we continute please enter a number: ";

		cin >> seed;


		if (cin.fail() == true) //cin.fail() checking to see if the value in the cin
			//stream is the correct type, if not it returns true,
			//false otherwise.
		{
			cin.clear(); //suppse to corect stream
			cin.ignore(); //to skip the left over stream data.
			cout << "\n Please enter an Integer only." << endl << endl;
			valid = false; //The cin was not an integer so try again.
		}
	}

		cout << " \n Okay now, lets move on.\n";
		cout << " Please enter an 'R' for a prediction on your relationships.\n";
		cout << " Please enter an 'E' for a prediction on school life.\n";
		cout << " And finally you can enter 'D' for a prediction on your financial situation.\n"
			<< endl;
		cin >> life;
		cout << endl << endl;
	
		
		if (life == 'R' || life == 'r')
		{
			do
			{
				//all r function
				firstworldproblems();
				//break;
				problemsolving(choice);
			} while (choice != 'n' || choice != 'N');
		}


	else if (life == 'E' || life == 'e')
	{
		do
		{
			//all r function
			thuglyfe();
			//break;
			problemsolving(choice);
		} while (choice != 'n' || choice != 'N');
	}

	else if (life == 'D' || life == 'd')
	{
		do
		{
			//all r function
			donaldtrump();
			//break;
			problemsolving(choice);
			
		} while (choice != 'n' || choice != 'N');
	}


	else return 0;
}
Hi @tavuk,

i think that
this example may
help!


Enter number 1: -1
Enter number 1: number one
Enter number 1: 0
Enter number 1: 2
Enter number 1: 1
Finally!!!!
Bye!


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
//choice.cpp
//##

#include <iostream>
using std::cout;
using std::cin;
using std::endl;


int main(){

int choice;
bool userFails=true;

	do{
		cout<<"Enter number 1: ";
		cin>>choice;		
		
		while(cin.fail()){
			cin.clear();
			cin.ignore(1000,'\n');
		}//end while
		if(choice==1)
			userFails=false;

	}while(userFails);

	cout<<"Finally!!!!\nBye!"<<endl;

return 0; //indicates success
}//end main 
BOOYAH!

Thank you so much, check it out it works!

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
/*************
Title:	Lab 7
Author:	C. Smith
Date:	29 Nov 2013

Objective - To write a program that will predict the future
Method - 
1. Use the main procedure to
a.	Prompt the user for his/her lucky number. Use this number as input to the srand function found 
	in stdlib.h
b.	Prompt the user for which life topic he wishes his predictions based.  Based on the user's 
	response call one of three functions. Suggestions are love, finance, and school, but you may 
	use three of your own choosing. Use a nested if-else statement to control which function is called.

2. Each function will
c.	Call the rand function
d.	Use value generated by the rand function and modulo division as the selector expression in a 
	switch statement.
e.	The switch statement will print out the predictions.
f.	Allow for five different predictions per function.

3.	Prompt the user to see if they want another prediction. Keep executing the program until the 
	user tells you to quit.  Make sure that you code for all possible user inputs, including wrong ones.

Hand In -
1.	A copy of your program
2.	A copy of the output. As the output scrolls past the black box, keep selecting the output and 
	pasting it into the text editor as you go.  Make sure that the output shows that your program 
	will work under all possible scenarios. You may have to run the program more than once to do this.

Have fun!!

The function srand takes a number (called a seed) and sets the starting point for a random number 
generator.  For example, if the seed for the random number generator is in the variable num, the 
syntax of the function call would be:
srand(num);

The call to srand is only used once in a program (typically immediately after the word main, or 
after the first set of declarations).

The function rand() returns a random number between 1 and RAND_MAX (32767 in VC++).  If you type
cout<<rand();

a random number will be printed.  In order for the random number to be between 1 and N use the following formula:
rand()%N + 1

For example:   x = rand()%7 + 1;  places a random number between 1 and 7 into the variable x.

***************/

//COPY OF PROGRAM:
#include<iostream>
#include<string>
#include<sstream>
#include<cstdlib>

using namespace std;

//function Relationship aka first world problems
void firstworldproblems(void)
{
	int	x = rand() % 5 + 1; // only 5 predictions right?

	switch (x)
	{
	case 5:
		cout << " Roses are red, voilets are not blue, smile today if you want your true" 
			"\n love to find you."; break; //must break or else T_T
	case 4:
		cout << " Walk up to your crush, smile, extend your hand and say hello and your day will "
		"\n never be the same."; break;
	case 3:
		cout << " Don't be shy, take some risk in your relationships, today is the day."; break;
	case 2:
		cout << " Go to a comedy club, watch something funny or listen to something funny, "
				"\nthis will help strengthen your relationship."; break;
	case 1:
		cout << " Step outside your comfort zone if you want to meet new people."; break;
	}
}

//function School Life aka wannabe thug lyfers
void thuglyfe(void)
{
	int	x = rand() % 5 + 1;
	switch (x)
	{
	case 5:
		cout << " You will get a B + if you study for that upcoming test."; break;
	case 4:
		cout << " School will be closed soon."; break;
	case 3:
		cout << " Your teachers will assign homework for thanksgiving."; break;
	case 2:
		cout << " Find a study buddy, they will be the reason you start to excel in class."; break;
	case 1:
		cout << " Review your previous class notes so you wont be surprise of the pop quiz."; break;
	}
}

//Function Financial Situation aka wigs!!!!
void donaldtrump(void)
{
	int	x = rand() % 5 + 1;
	switch (x)
	{
	case 5:
		cout << " Invest in yourself and in life if you wish to be succesful."; break;
	case 4:
		cout << " Donate a dollar, you will find a bill worth way more later."; break;
	case 3:
		cout << " You can not be cheap with everything, especially when it comes to your health and needs."; break;
	case 2:
		cout << " Put your needs over your wants if you wish to be financly regret free."; break;
	case 1:
		cout << " Learn to cook, the fast food you eat is doing a lot more than satisfying your hunger."; break;
	}
}

//function let me use a function because this code is aggravating me
void problemsolving(void)
{
	char life;//life...short for life topic
	bool userfails = true; //userfails? tru!
	do
	{
		cout << " Please enter an 'R' for a prediction on your relationships.\n";
		cout << " Please enter an 'E' for a prediction on school life.\n"; // e as in education
		cout << " And finally you can enter 'D' for a prediction on your financial situation.\n" //d as in......actual i dont know why i picked d...
			<< endl;
		cin >> life;
		cout << endl << endl;

		if (life == 'R' || life == 'r')
		{
			firstworldproblems();
			userfails = false; //without this the loop will start over again
		}


		else if (life == 'E' || life == 'e')
		{
			thuglyfe();
			userfails = false;//without this the loop will start over again
		}

		else if (life == 'D' || life == 'd')
		{
			donaldtrump();
			userfails = false;//without this the loop will start over again
		}

		else
		{
			userfails = true;//cant be too safe
		}
	} while (userfails);//repeat loop while user keeps failing
	
	

}

int main()
{
	unsigned seed; //cause i was asked to have this
	bool valid = false; //for my while loop to make sure user doesnt crash program
	char choice;

	//Talking to user

	while (!valid)
	{
		valid = true;

		cout << " Welcome to the Matrix.\n"; //awh yeah
		cout << " You have been automatically connected to the Psychic Computer Network.\n"; //awh sht
		cout << " My name is Dionne, and I will be your psychic computer guide.\n";
		cout << endl << " Before we continue please enter a number: ";

		cin >> seed;//cause i was asked to do this


		if (cin.fail() == true) //cin.fail() checking to see if the value in the cin
			//stream is the correct type, if not it returns true,
			//false otherwise.
		{
			cin.clear(); //suppse to corect stream
			cin.ignore(); //to skip the left over stream data.
			cout << "\n Please enter an Integer only." << endl << endl;
			valid = false; //The cin was not an integer so try again.
		}
	}

	cout << " \n Okay now, lets move on.\n";
	problemsolving();//calling superman

	bool userFails = true; //another fail safe cuase users love to crash programs

	do//do-while loop as in do you want to read another prediction?
	{
		cout << "\n\n Would you like another prediction? Y/N" << endl << endl;
		cin >> choice;

		if (choice == 'y' || choice == 'Y')
		{
			cout << endl << endl;
			problemsolving();//calling batman
		}

		else if (choice == 'n' || choice == 'N')
		{
			userFails = false;
			break;
		}

		else
			cout << "\n " << choice << " is not y/n/Y/N. Please only enter Y,N,y or n."; //let user know what he/she did wrong
			userFails = true;//extra safe

	} while (userFails); //back to preK with you!

	cout << endl << endl;//cuz i love my spaces
	return 0;
}
You are welcome!
Topic archived. No new replies allowed.