user3547330
user3547330

Reputation: 1

(C++) If - Else if statements for a CommandLine rock paper scissors game

I'm fairly new to c++ but have been reading alot of documentation and cant figure out whats going on here. I've used if / else if statements on other things but maybe this is just a brain fart. i don't really know. when i type "1" when it ask's for an input and i press enter and it just goes directly to the Else statement


#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{

    char choice;
    int p2 = 2;
    int r1 = 1;
    int s3 = 3;


    //questions

    cout << "Lets Play Rock Paper Scissors" <<endl;
    cout << "(Use Letter) Rock (1) - Paper (2) - Scissors (3)" <<endl;
    que:
    cout << "What is your choice? : ";
    cin >> choice;

    if (choice == p1){
        cout << "You choose Rock" <<endl;}
    else if (choice == p2){
        cout << "You choose Paper" <<endl;}
    else if (choice == s3){
        cout << "You choose Scissors" <<endl;}
    else
        goto que;


    system("PAUSE");
}

Upvotes: 0

Views: 2292

Answers (7)

Vlad from Moscow
Vlad from Moscow

Reputation: 311048

The problem of the code is that the type of an object value of which is entered is char. So when you type for example 1 it is entered as character '1'. At the same time r1 has integral value 1. '1' is not equal to 1. When '1' is compared with 1 then there is compared the internal code of character '1'. For ASCII its value is 49. For EBCDIC its value is 241. You could write for example

choice -= '0'; 
 if ( choice == r1 )
{
        cout << "You choose Rock" <<endl;
}
//...

you can write a test program

#include <iostream>

int main()
{
   char c = '1';
   int i  = 1;

   if ( c == i ) std::cout << ( int )c << " is equal to " << i << std::endl;
   else std::cout << ( int )c << " is not equal to " << i << std::endl;
}

I hope you will get result:)

49 is not equal to 1

Also take into account that your code has a typo. I think in this statement

if (choice == p1){
    cout << "You choose Rock" <<endl;}

there has to be r1 instead of p1

if (choice == r1){
    cout << "You choose Rock" <<endl;}

Also it is a bad idea to use goto statement.

The program could look as

#include <cstdlib>
#include <iostream>

int main()
{
    const char *name[] = { "Rock", "Paper", "Scissors" };
    enum { Rock, Paper, Scissors };
    char choice;

    //questions

    std::cout << "Lets Play Rock Paper Scissors" << std::endl;
    std::cout << "(Use Letter) " << name[Rock]     << " (" << Rock + 1     << ") - "
                                 << name[Paper]    << " (" << Paper + 1    << ") - "
                                 << name[Scissors] << " (" << Scissors + 1 << ")" << std::endl;

    do
    {
       std::cout << "What is your choice? : ";
       std::cin >> choice;

       switch ( choice - '0' - 1 )
       {
       case Rock:    
          std::cout << "You choose " << name[Rock] << std::endl;
          break;
       case Paper:
          std::cout << "You choose " << name[Paper] << std::endl;
          break;
       case Scissors:
          std::cout << "You choose " << name[Scissors] << std::endl;
          break;
       default:
          break; 
       }
    } while ( choice < '1' || choice > '3' );

    std::system( "PAUSE" );
}

EDIT: I updated some typos in the last program.

Upvotes: 0

Shadow
Shadow

Reputation: 4016

Why are you comparing an int to a char? Also if that is all your code, string, cstdlib, and sstream libraries aren't used...

#include <iostream>
using namespace std;

int main()
{
    char choice;
    char rock = '1';
    char paper = '2';
    char scissors = '3';

    cout << "Let's play rock paper scissors\n";
    cout << "Rock = 1, Paper = 2, Scissors = 3\n";
    que:
    cout << "What is your choice? : ";

    cin >> choice;
    if (choice == rock)
    {
        cout << "You picked Rock!";
    }
    else if (choice == paper)
    {
        cout << "You picked Paper!";
    }
    else if (choice == scissors)
    {
        cout << "You picked Scissors!";
    }
    else
    {
        goto que;
    }
}

Upvotes: 0

magu_
magu_

Reputation: 4856

As already mentioned use int as type. Also I would not use goto, since it is bad practice. Furthermore, this looks like a perfect job for switch. Here is some altered code.

#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main() {

int choice;
int p2 = 2;
int r1 = 1;
int s3 = 3;
//questions

cout << "Lets Play Rock Paper Scissors" <<endl;
for(;;) {
    cout << "(Use Letter) Rock (1) - Paper (2) - Scissors (3)" <<endl;
    cout << "What is your choice? : ";
    cin >> choice;
        switch (choice) {
            case 1: cout << "You choose Rock" <<endl; break;
            case 2: cout << "You choose Rock" <<endl; break;
            case 3: cout << "You choose Rock" <<endl; break;
            default: cout << "Unknown Option" << endl; break;
            }
}}

Upvotes: 0

jaket
jaket

Reputation: 9341

The problem is that cin >> choice is giving characters e.g. '1'. The character '1' is stored as a byte value of 49 and so when compared to the integer 1, the comparison fails.

iow: '1' == 1 is equivalent to 49 == 1 which is false.

There are two ways you can fix this.

1) Change the type of choice to int:

int main()
{    
    int choice;
    int p2 = 2;
    int r1 = 1;
    int s3 = 3;
    ...
}

or 2) Change the type of p2, r1, s3 to char:

int main()
{    
    char choice;
    char p2 = '2';
    char r1 = '1';
    char s3 = '3';
    ...
}

Upvotes: 1

user657267
user657267

Reputation: 21000

The characters for '1', '2' and '3' are not the same as the integers 1, 2 and 3. Under ascii encoding they will be 49, 50 and 51 for instance.

You can fix this by comparing the inputted character to the actual characters as follows:

char r1 = '1';
char p2 = '2';
char s3 = '3';

Upvotes: 1

PomfCaster
PomfCaster

Reputation: 842

You're trying to compare a char to an int, which isn't going to work how you want it to, change the type of choice to int.

Also there's no variable called p1, that should probably be changed to r1.

Upvotes: 2

Abhishek Sathe
Abhishek Sathe

Reputation: 128

Change the declaration int r1=1; to int pa=1;. Hopefully it will work out.

Upvotes: 0

Related Questions