The other day, I asked for some help here in regards to a rock paper scissors program I was making, and what was wrong with it. After taking your advice, I was able to fix it, but upon hiding the input in an attempt to make the game fair, I have encountered a new problem.
When answer1/answer2 are input, they are shown as "*", but the code doesnt seem to recognise the variables anymore and just outputs "one of you hasnt entered..". I know getch() isnt such a good idea, but it was the only solution i could find to hiding the input.
So my two questions are:
1. Is there a way to fix the problem with the variables/a better method of hiding input?
2. Also, is there a way of limiting the input that can be entered to one character?
//Rock, Paper, Scissors
#include <iostream>
#include <conio.h>
usingnamespace std;
int main()
{
char choice;
char answer1[1]; // you want this to hold the letter and the '\r' and the '\0'
char answer2[1]; // so should be atleast size 3. c++ strings need to be null terminated
int charcount;
int charcount2;
charcount=0;
charcount2=0;
cout << "Rock Paper Scissors\n\n";
cout << "Play a game? y/n\n";
cin >> choice;
while (choice=='y')
{
cout << "\nPlayer 1, Choose either rock(r), paper(p) or scissors(s).\n";
while((answer1[charcount]=getch())!='\r')
{cout << "*";
charcount++;}
cout << "\nPlayer 2, Choose either rock(r), paper(p) or scissors(s).\n";
while((answer2[charcount2]=getch())!='\r')
{cout << "*";
charcount2++;}
if((answer1=="r"&answer2=="s")|| // you wait for the user to enter '\r' so you should
(answer1=="s"&answer2=="p")|| // compare it to "r\n" not just "r"
(answer1=="p"&answer2=="r"))
{cout << "\nPlayer 1 Wins.\n";}
elseif((answer2=="r"&answer1=="s")|| // also should be 2 ampersands even though
(answer2=="s"&answer1=="p")|| // they both work i think
(answer2=="p"&answer1=="r"))
{cout << "\nPlayer 2 Wins.\n";}
elseif((answer1=="r"&answer2=="r")||
(answer1=="s"&answer2=="s")||
(answer1=="p"&answer2=="p"))
{cout << "\nIt's a Draw.\n";}
else {cout << "\nOne of you hasn't entered rock(r), paper(p) or scissors(s).\n";}
cout << "Play Again? y/n\n";
cin >> choice;
}
}