Hi all I am trying to make a rock, paper, scissors game.
My goal is to have the game run until someone reaches 5 wins, but I haven't inputted that code, because I am having problems with my conditional statements.
Here is my code
#include <iostream>
#include <ctime>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
int rand1, userChoice, compInput;
int userWin, compWin;
srand(time(0));
rand1 = (1 + rand() % 3);
cout << "Lets play a game of Rock-Paper-Scissors" << endl;
cout << "Choose \n 1 for Rock \n 2 for Paper \n 3 for Scissors" << endl;
cin >> userChoice;
while (userChoice < 1 || userChoice > 3) {
cout << "Please enter a valid selection between 1-3.\n";
cin >> userChoice;
}
compInput = rand1;
if ((userChoice == 1 && compInput == 3) || (userChoice == 2 && compInput == 1) ||
(userChoice == 3 && compInput == 2)) {
cout << "You win!" << userChoice << compInput << endl;
userWin++;
}
if ((userChoice == 1 && compInput == 2) || (userChoice == 2 && compInput == 3) ||
(userChoice == 3 && compInput == 1)) {
cout << "You lose!" << cout << userChoice << compInput << endl;
compWin++;
}
return 0;
}
I am getting errors if I run the code with the second "if" statement, however if I put the second if statement in comments the code runs just fine.
I am not sure what I am doing wrong here. In my first if statement I am trying to check if the user wins, if so increment useWin by 1.
In my second if statement I am try to check if the user loses, and if that is the case increment compWin by 1.
As you can tell I am new to C++ and this is not homework, just practice for me. Thank you for your help!
Aucun commentaire:
Enregistrer un commentaire