0

Please consider the following code:

#include <iostream>

using std::cin;
using std::cout;
using std::endl;
int main(int argc, char** argv) {
    enum object {rock, paper, scissors};
    object player1, player2;
    cout << " "Enter "r" for rock, "p" for paper or "s" for scissors:";
    char p1, p2;
    cin >> p1 >> p2;

    if (p1 == 'r') {player1 = rock;}
    else if (p1 == 'p') {player1 = paper;}
    else if (p1 == 's') {player1 = scissors;}
    else {cout << "invalid input, play again" << endl; exit(1);}

    if (p2 == 'r') {player2 = rock;}
    else if (p2 == 'p') {player2 = paper;}
    else if (p2 == 's') {player2 = scissors;}
    else {cout << "invalid input, play again" << endl; exit(1);}

    if (player1==player2) cout <<"objects are equal";
    else if (player1==rock && player2==paper) cout << "player 2 is the winner";
    else if (player1==rock && player2==scissors) cout<<"player 1 is the winner";
    else if (player1==paper && player2==rock) cout << "player 1 is the winner";
    else if (player1==paper && player2==scissors) cout <<"Palyer 2 is the winnder";
    else if (player1==scissors && player2==paper) cout << "Player 1 is winner";
    else cout <<"Player 2 is the winner";
    cout << endl;
}

How can the above code be rewritten with the switch statement while the user has to give the full word (rock, paper, scissors) instead of the initials for the input.

Update: I have converted part of the code with switch, but I was interested to see if I could use it for the entire code:

For instance:

switch (p1) {
        case 'r': player1=rock; break;
        case 'p': player1=paper; break;
        case 's': player1=scissors; break;
    }

instead of

if (p1 == 'r') {player1 = rock;}
    else if (p1 == 'p') {player1 = paper;}
    else if (p1 == 's') {player1 = scissors;}
    else {cout << "invalid input, play again" << endl; exit(1);}
developer
  • 283
  • 2
  • 8

3 Answers3

1

You can't do it this simple. switch does not work with strings. You could however circumvent this problem and calculate the object before doing the switch:

std::string player1Input, player2Input;
//...
std::cin >> player1Input >> player2Input;

object player1Choice = getChoice(player1Input);
object player2Choice = getChoice(player2Input);

Where getChoice could look like this:

object getChoice(std::string input)
{
    if(input == "rock")
        return object::rock;
    else if(input == "paper")
        return object::paper;
    else if(input == "scisor")
        return object::sciscor;
    else {
        //invalid input
    }
}

Then you can use your switch:

switch(player1Choice)
{
case object::rock:
    switch(player2Choice)
    {
    case object::rock: //rock vs rock
        break;
    ...
    };

    break;
...
}

Lukas-T
  • 11,133
  • 3
  • 20
  • 30
0

It can't.

switch works on built-in types, like int, bool and char.

It is not a complex comparison feature that can perform checks on strings.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

It seems you mean something like the following

#include <string>
#include <iterator>
#include <algorithm>

//...

const char * object_name[] = { "rock", "paper", "scissors" };
enum object {rock, paper, scissors};

std::string choice;
std::cin >> choice;

//...

auto it = std::find_if( std::begin( object_name ), std::end( object_name ),
                        [&choice]( const char *item )
                        {
                            return choice == item;
                        } );

if ( it != std::end( object_name ) )
{
    int p1 = std::distance( std::begin( object_name ), it );

    switch ( p1 )
    {
    case rock:
        //...
        break;
    case paper:
        //...
        break;
    case scissors:
        //...
        break;
    }
}

Here is a demonstrative program

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>

int main() 
{
    const char * object_name[] = { "rock", "paper", "scissors" };
    enum object {rock, paper, scissors};

    std::string choice;

    std::cout << "Enter \"" << object_name[0] << "\", or \""  << object_name[1]
              << "\", or \"" << object_name[2] << "\": ";
    std::cin >> choice;

    auto it = std::find_if( std::begin( object_name ), std::end( object_name ),
                        [&choice]( const char *item )
                        {
                            return choice == item;
                        } );

    if ( it != std::end( object_name ) )
    {
        int p1 = std::distance( std::begin( object_name ), it );

        switch ( p1 )
        {
        case rock:
            std::cout << object_name[rock];
            break;

        case paper:
            std::cout << object_name[paper];
            break;
        case scissors:
            std::cout << object_name[scissors];
            break;
        }
    }

    return 0;
} 

Its output might look like

Enter "rock", or "paper", or "scissors": paper
paper
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335