I'm extremely new to c++ coding, but I've read a few tutorials and have learned some very basic coding. I'm trying to write a code for a simple multiplayer Rock Paper Scissors game but i cant get it to loop if you type anything besides 1, 2, or 3. it just keeps repeating this part of the code:
default:
system("cls");
cout << "invalid entry\nplease enter either 1, 2, or 3\n";
system("pause");
b = (0);
break;
i probably did a lot of things wrong so any help would be appreciated.
Here's what i have so far:
//rock paper scissors
#include <iostream>
#include <string>
using namespace std;
//p1 input
int a;
//loop variable
int b;
//p2 input
int c;
int main()
{
//p1 txt
while (b == 0)
cout << "player 1,choose one of the following:" << endl;
cout << "1) rock\n";
cout << "2) paper\n";
cout << "3) scissors\n";
cin >> a;
b = (1);
//p1 input check
while (b == 1)
{
switch (a){
case 1:
cout << "rock\n";
system("pause");
b = (-1);
break;
case 2:
cout << "paper\n";
system("pause");
b = (-1);
break;
case 3:
cout << "scissors\n";
system("pause");
b = (-1);
break;
default:
system("cls");
cout << "invalid entry\nplease enter either 1, 2, or 3\n";
system("pause");
b = (0);
break;
}
}
//p2 txt
while (b == -1)
{
system("cls");
cout << "player 2,choose one of the following:" << endl;
cout << "1) rock\n";
cout << "2) paper\n";
cout << "3) scissors\n";
cin >> c;
b = (3);
}
//p1 input check
while (b == 3)
{
switch (c){
case 1:
cout << "rock\n";
system("pause");
b = (-3);
break;
case 2:
cout << "paper\n";
system("pause");
b = (-3);
break;
case 3:
cout << "scissors\n";
system("pause");
b = (-3);
break;
default:
system("cls");
cout << "invalid entry\nplease enter either 1, 2, or 3\n";
system("pause");
b = (-1);
break;
}
}
//winning deccision
while (b == -3)
{
}
}
i want it to loop all of this:
while (b == 0)
cout << "player 1,choose one of the following:" << endl;
cout << "1) rock\n";
cout << "2) paper\n";
cout << "3) scissors\n";
cin >> a;
b = (1);
//p1 input check
while (b == 1)
{
switch (a){
case 1:
cout << "rock\n";
system("pause");
b = (-1);
break;
case 2:
cout << "paper\n";
system("pause");
b = (-1);
break;
case 3:
cout << "scissors\n";
system("pause");
b = (-1);
break;
default:
system("cls");
cout << "invalid entry\nplease enter either 1, 2, or 3\n";
system("pause");
b = (0);
break;
}
Copyright © 2024 Q2A.ES - All rights reserved.
Answers & Comments
Verified answer
i would suggest
#include <iostream>
using namespace std;
void main()
{
int a;
while(true)
{
if( a < 0)
{
exit(); // it might just be exit been awhile and i dont have my codes open so cant check it
}
else
{
switch(a)
{
case 1:
cout << "you have chosen rock" << endl;
break;
case 2:
cout << "you have chosen scissors" << endl;
break;
case 3:
cout << "you have chosen paper" << endl;
break;
default:
cout << "you have picked a invalid choice" << endl;
break; // you might not need break for the default
}
}
}
Looking quickly, I do not see braces.
While (whatever)
{
do stuff
}//ends while