Hidden Variables Not Recognised?

Oct 16, 2011 at 12:37pm
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?

Heres the code:

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
//Rock, Paper, Scissors
#include <iostream>
#include <conio.h>

using namespace std;
int main()
{   
    char choice;
    char answer1[1];
    char answer2[1];
    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")||
                 (answer1=="s"&answer2=="p")||
                 (answer1=="p"&answer2=="r"))
                 {cout << "\nPlayer 1 Wins.\n";}
                 
         else if((answer2=="r"&answer1=="s")||
                 (answer2=="s"&answer1=="p")||
                 (answer2=="p"&answer1=="r"))
                 {cout << "\nPlayer 2 Wins.\n";}
           
         else if((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;
           } 
           }
Oct 16, 2011 at 12:47pm
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
//Rock, Paper, Scissors
#include <iostream>
#include <conio.h>

using namespace 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";}
                 
         else if((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";}
           
         else if((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;
           } 
           }
Last edited on Oct 16, 2011 at 12:56pm
Oct 16, 2011 at 2:41pm
I've tried comparing to 'r\n' and 'r\r', and increased the char limit to 10, but still the same outcome.
Oct 16, 2011 at 4:38pm
answer1=="r"warning: comparison with string literal results in unspecified behaviour
I think you want answer[0] == 'r' or
1
2
char answer;
answer == 'r';


1
2
3
    while((answer2[charcount2]=getch())!='\r')
          {cout << "*";
          charcount2++;}
That is dangerous and obfuscated.
1
2
3
4
char answer;
do
  answer = getch(); //keypad should be FALSE, or use a temporary int
while(not valid(answer));


I know getch() isnt such a good idea
¿Why not?
Oct 16, 2011 at 5:42pm
can you explain the second part of that to me. i figured getch was platform specific
Oct 16, 2011 at 7:16pm
Sorry, you're right. ncurses has no current port for DOS or windows.
Maybe you could try pdcurses.
Oct 16, 2011 at 8:01pm
Oct 16, 2011 at 9:01pm
so why is my current method dangerous?
Topic archived. No new replies allowed.