Problem with infinite loop

Hello, I am pretty new to C++ but I'm liking it so far. I was working on a project and came across an issue of getting caught in infinite loops. I think I went more in depth than my instructor taught us on loops, but I am wondering if break; in these examples are the actual solution or a quick and dirty fix.. As well at the end in my last else statement I came across another problem so I threw in "goto" which we haven't been 'taught' and one of my friends told me goto was bad practice.. but I just cannot figure it out. I am sure there are A LOT of things wrong with this program, but it does work as the book wants. I am open to suggestions on improvement. Thanks

edit: I rewrote it as a switch, now my only problem is forcing the user to re-enter choice1 if the variable is invalid


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
#include <iostream>

using namespace std;

int main()
{
	char choice1, choice2, rerun;
	do
	{
			cout << "enter first choice ";
			cin >> choice1;
			switch (choice1)
			{
			case 'R':
			case 'r':
				do
				{
					cout << "Enter second choice ";
					cin >> choice2;
					if (choice2 == 'R' || choice2 == 'r')
					{
						cout << "Tie, nobody wins" << endl;
						break;
					}
					else if (choice2 == 'P' || choice2 == 'p')
					{
						cout << "Player 2 wins. Paper covers rock" << endl;
						break;
					}
					else if (choice2 == 'S' || choice2 == 's')
					{
						cout << "Player 1 wins. Rock breaks scissors." << endl;
						break;
					}
					else
					{
						cout << "Please enter \'R\', \'r\', \'P\', \'p\', \'S\' or \'s\'." << endl;
					}
				} while ((choice2 != 'R' || choice2 != 'r') && (choice2 != 'P' || choice2 != 'p') && (choice2 != 'S' || choice2 != 's'));
				break;
			case 'P':
			case 'p':
				do
				{
					cout << "Enter second choice ";
					cin >> choice2;
					if (choice2 == 'R' || choice2 == 'r')
					{
						cout << "Player 1 wins. Paper covers rock" << endl;
						break;
					}
					else if (choice2 == 'P' || choice2 == 'p')
					{
						cout << "Tie, nobody wins" << endl;
						break;
					}
					else if (choice2 == 'S' || choice2 == 's')
					{
						cout << "Player 2 wins. Scissors cuts paper" << endl;
						break;
					}
					else
					{
						cout << "Please enter \'R\', \'r\', \'P\', \'p\', \'S\' or \'s\'." << endl;
					}
				} while ((choice2 != 'R' || choice2 != 'r') && (choice2 != 'P' || choice2 != 'p') && (choice2 != 'S' || choice2 != 's'));
				break;
			case 'S':
			case 's':
				do
				{
					cout << "Enter second choice ";
					cin >> choice2;
					if (choice2 == 'R' || choice2 == 'r')
					{
						cout << "Player 2 wins. Rock breaks scissors." << endl;
						break;
					}
					else if (choice2 == 'P' || choice2 == 'p')
					{
						cout << "Player 1 wins. Scissors cuts paper." << endl;
						break;
					}
					else if (choice2 == 'S' || choice2 == 's')
					{
						cout << "Tie, nobody wins." << endl;
						break;
					}
					else
					{
						cout << "Please enter \'R\', \'r\', \'P\', \'p\', \'S\' or \'s\'." << endl;
					}
				} while ((choice2 != 'R' || choice2 != 'r') && (choice2 != 'P' || choice2 != 'p') && (choice2 != 'S' || choice2 != 's'));
				break;
			default:
				cout << "Please enter \'R\', \'r\', \'P\', \'p\', \'S\' or \'s\'" << endl;
				break;
			}
		cout << "run again? Y\\N ";
		cin >> rerun;
	} while (rerun == 'Y' || rerun == 'y');
	system("pause");
	return(0);
}
Last edited on
tolower() function from <cctype> library to turn characters to their lower case representation.
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
#include<iostream>
#include<cctype>
int main()
{
	char choice,choice2;
	
	std::cout<<"Welcome to rock,paper, scissors!\n";
	
	
	do
	{
	
		std::cout<<"Player 1 enter move: ";
		std::cin>>choice;
		choice=tolower(choice);
	    while(true)
	{
		if(choice=='r' || choice=='s'|| choice=='p')
		{
		break;
		}
		std::cout<<"Player 1 please enter a valid choice: ";
		std::cin>>choice;
	}
	
	std::cout<<"Player 2 enter move: ";
	std::cin>>choice2;
	choice2=tolower(choice2);
	while(true)
	{
		if(choice2=='p' || choice2=='r'|| choice2=='s')
		{
		break;
		}
		std::cout<<"Player 2 please enter a valid choice: ";
		std::cin>>choice2;
	}
	
	if(choice==choice2)
	{
		std::cout<<"It's a tie.";
	} 
	else if(choice<choice2)
	{
		if(choice=='p' && choice2=='s')
		{
			std::cout<<"Player two wins!";
		}
		else
		{
			std::cout<<"Player one wins!";
		}
	}
	else
	{
		if(choice=='s' && choice2=='p')
		{
			std::cout<<"Player one wins!";
		}
		else
		{
			std::cout<<"Player two wins!";
		}
	}
	std::cout<<"\nPlay again?(y or n): ";	
	std::cin>>choice;
}while(choice=='y');
	
}
Last edited on
Topic archived. No new replies allowed.