Need some help

Pages: 12
I meant that I'm having trouble coming up with a way to loop back to where it says

 
string choiceUser = "";


if you don't enter either "yes" or "no" at

1
2
3
4
5
6
7
8
9
10
11
12
if (choiceUser == "yes")
		{
			
		}
		else if (choiceUser == "no")
		{
			break;
		}
		else
		{
			return 0;
		}
Last edited on
In other words, I'm having trouble coming up with a way to use a loop instead of goto in this part of the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
repeat:
		string choiceUser = "";
		cout << "Repeat?: ";
		cin >> choiceUser;
		if (choiceUser == "yes")
		{
			
		}
		else if (choiceUser == "no")
		{
			break;
		}
		else
		{
			goto repeat;
		}


Though it doesn't make much sense if you only see that part of it, so here's the whole code:

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
#include "class.h"
using namespace std;

int main()
{
	while (1 == 1)
	{
		int tHappy = 0;
		do
		{
			cout << endl << endl << "How happy is Tom on a scale of 1 to 100?: ";
			cin >> tHappy;
			if (tHappy > 100 || tHappy < 0)
			{
				firstRet("Wrong, try again!");
			}
		} while (tHappy > 100 || tHappy < 0);

		int tHunger = 0;
		do
		{
			cout << endl << endl << "How hungry is Tom on a scale of 1 to 100?: ";
			cin >> tHunger;
			if (tHunger > 100 || tHunger < 0)
			{
				secondRet("Wrong, try again!");
			}
		} while (tHunger > 100 || tHunger < 0);

		vTom obj(tHappy, tHunger);

		cout << endl << endl << obj.retStat() << endl << endl;
		repeat:
		string choiceUser = "";
		cout << "Repeat?: ";
		cin >> choiceUser;
		if (choiceUser == "yes")
		{
			
		}
		else if (choiceUser == "no")
		{
			break;
		}
		else
		{
			goto repeat;
		}

	}
	return 0;
}
Last edited on
do
{

string choiceUser = "";
cout << "Repeat?: ";
cin >> choiceUser;

if(choiceUser == "yes")
{
// Do ya thing
}

}while(choiceUser == "no");


So this loop will run, as long as the answer entered by the user is "no". If he enters yes, it will go into the if statement and do your thing, then exit the loop. But Im not sure if that is what you actually want to do with your program, but that is how you have structured it. Please let me know.
Yes, thx, it finally works, I'd completely forgotten about using the do loop, thx for reminding me. So anyways, this is the structure I wanted:

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
#include "class.h"
using namespace std;

int main()
{
	while (1 == 1)
	{
		int tHappy = 0;
		do
		{
			cout << endl << endl << "How happy is Tom on a scale of 1 to 100?: ";
			cin >> tHappy;
			if (tHappy > 100 || tHappy < 0)
			{
				firstRet("Wrong, try again!");
			}
		} while (tHappy > 100 || tHappy < 0);

		int tHunger = 0;
		do
		{
			cout << endl << endl << "How hungry is Tom on a scale of 1 to 100?: ";
			cin >> tHunger;
			if (tHunger > 100 || tHunger < 0)
			{
				secondRet("Wrong, try again!");
			}
		} while (tHunger > 100 || tHunger < 0);

		vTom obj(tHappy, tHunger);
		string choiceUser = "";

		cout << endl << endl << obj.retStat() << endl << endl;
		do
		{
			cout << "Repeat?: ";
			cin >> choiceUser;

			if (choiceUser == "yes")
			{
				
			}
			else if (choiceUser == "no")
			{
				return 0;
			}

		} while (choiceUser != "yes" && choiceUser != "no");


	}
	return 0;
}


Though it looks a bit cluttered in my opinion, but I didn't know any other way to do it.

To put it shortly, I wanted the whole program to repeat if you enter "yes" or to close if you enter "no", but if you didn't enter either one, it would ask you again to enter something.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
do
		{
			cout << "Repeat?: ";
			cin >> choiceUser;

			if (choiceUser == "yes")
			{
				
			}
			else if (choiceUser == "no")
			{
				return 0;
			}

		} while (choiceUser != "yes" && choiceUser != "no");


I dont quite understand this part. I mean, if the choiceUser is yes, it doesnt repeat the program, it just does nothing.

Ive re-written what you did in a more cleaner way, tell me if it works as you want it to work.
Ive made it so it repeats the entire program if the user enters yes.

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
int tHappy = 0;
	int tHunger = 0;
	string userChoice;

	do
	{
		cout << endl << endl << "How happy is Tom on a scale of 1 to 100?: ";
		cin >> tHappy;

		while (tHappy > 100 || tHappy < 0)
		{
			firstRet("Wrong, try again!");
			cin >> tHappy;
		}

		cout << endl << endl << "How hungry is Tom on a scale of 1 to 100?: ";
		cin >> tHunger;

		while (tHunger > 100 || tHunger < 0)
		{
			secondRet("Wrong, try again!");
			cin >> tHunger;
		}

		vTom obj(tHappy, tHunger);

		cout << endl << endl << obj.retStat() << endl << endl;

		cout << "Repeat?: ";
		cin >> userChoice;

	} while (userChoice == "yes");
It seemingly works that way. If you enter "yes", it doesn't do anything and the do loop is complete, after which it goes to the end where it repeats the entire program cause I've put while(1 == 1) {} around the entire program. You can try running the code.

In other words, if you enter "yes", it doesn't do anything and goes to the bracket at line 51, where it reaches the end of the while loop, but since it's while( 1 == 1), the while loop will repeat forever and take you back to the very beginning of the code. But if you enter "no", it will manually return 0, but if you don't enter either, it asks you again to enter something.
Last edited on
Topic archived. No new replies allowed.
Pages: 12