Rock paper scissors, program not recognizing userinput and getting same answer everytime
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
|
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
srand(time(NULL));
int randnum = rand() %3;
string userchoice;
string computerchoice;
cout << "Welcome to rock, paper, scissors!\n";
cout << "rock, paper, or scissors?\n";
cin >> userchoice;
if (randnum == 1)
{
computerchoice = "rock\n";
}
else if (randnum == 2)
{
computerchoice = "paper\n";
}
else
{
computerchoice = "scissors\n";
}
cout << computerchoice;
if (userchoice == "rock" && computerchoice == "rock")
{
cout << "its a tie!";
}
else if (userchoice == "scissors" && computerchoice == "scissors")
{
cout << "its a tie!";
}
else if (userchoice == "paper" && computerchoice == "paper")
{
cout << "its a tie!";
}
else if (userchoice == "rock" && computerchoice == "scissors")
{
cout << "you win!";
}
else if (userchoice == "rock" && computerchoice == "paper")
{
cout << "better luck next time";
}
else if (userchoice == "scissors" && computerchoice == "rock")
{
cout << "better luck next time";
}
else if (userchoice == "scissors" && computerchoice == "paper")
{
cout << "you win";
}
else if (userchoice == "paper" && computerchoice == "rock")
{
cout << "you win";
}
else if (userchoice == "paper" && computerchoice == "scissors");
{
cout << "better luck next time";
}
return 0;
}
|
Last edited on
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
|
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
srand(time(NULL));
int randnum = rand() %3;
string userchoice;
string computerchoice;
cout << "Welcome to rock, paper, scissors!\n";
cout << "rock, paper, or scissors?\n";
cin >> userchoice;
if (randnum == 1)
{
computerchoice = "rock"; // <--
}
else if (randnum == 2)
{
computerchoice = "paper"; // <--
}
else
{
computerchoice = "scissors"; // <--
}
cout << computerchoice;
if (userchoice == "rock" && computerchoice == "rock")// <--
{
cout << "its a tie!";
}
else if (userchoice == "scissors" && computerchoice == "scissors")
{
cout << "its a tie!";
}
else if (userchoice == "paper" && computerchoice == "paper")
{
cout << "its a tie!";
}
else if (userchoice == "rock" && computerchoice == "scissors")
{
cout << "you win!";
}
else if (userchoice == "rock" && computerchoice == "paper")
{
cout << "better luck next time";
}
else if (userchoice == "scissors" && computerchoice == "rock")
{
cout << "better luck next time";
}
else if (userchoice == "scissors" && computerchoice == "paper")
{
cout << "you win";
}
else if (userchoice == "paper" && computerchoice == "rock")
{
cout << "you win";
}
else if (userchoice == "paper" && computerchoice == "scissors") // <--
{
cout << "better luck next time";
}
return 0;
}
|
You've appended a newline to computerchoice for some reason,
so it will never equal the strings you are comparing it to.
You also have an extraneous semicolon at the end of your last else if.
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
|
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(nullptr));
string objects[3] = {"rock", "paper", "scissors"};
string computer = objects[rand() % 3];
cout << "Welcome to rock, paper, scissors!\n";
cout << "rock, paper, or scissors?\n";
string player;
while (true)
{
cin >> player;
int i = 0;
for ( ; i < 3 && player != objects[i]; ++i)
;
if (i < 3) break;
cout << "You must enter rock, paper, or scissors.\n";
}
cout << computer << '\n';
if (player == computer)
cout << "its a tie!\n";
else if (player == "rock" && computer == "scissors")
cout << "you win!\n";
else if (player == "rock" && computer == "paper")
cout << "better luck next time\n";
else if (player == "scissors" && computer == "rock")
cout << "better luck next time\n";
else if (player == "scissors" && computer == "paper")
cout << "you win\n";
else if (player == "paper" && computer == "rock")
cout << "you win\n";
else // must be player: paper, computer: scissors
cout << "better luck next time\n";
}
|
Alternatively:
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
|
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(nullptr));
string objects[3] = {"rock", "paper", "scissors"};
cout << "Welcome to rock, paper, scissors!\n";
cout << "rock, paper, or scissors?\n";
int player;
while (true)
{
string obj;
cin >> obj;
int i = 0;
for ( ; i < 3 && obj != objects[i]; ++i)
;
if (i < 3)
{
player = i;
break;
}
cout << "You must enter rock, paper, or scissors.\n";
}
int computer = rand() % 3;
cout << objects[computer] << '\n';
if (player == computer)
cout << "its a tie!\n";
else if (computer == (player + 1) % 3)
cout << "better luck next time\n";
else
cout << "you win!\n";
}
|
Last edited on
BTW, if you don't start using code tags like salem suggested I'll delete your account. :-)
Sorry about that. I'm new to this.
Topic archived. No new replies allowed.