-4

In C++, how to deal with other data types in switch cases?
Like cases are of int type and user inputs char type.

Screenshot 1

int play(Player &Player1, Player &Player2)
{
  int answer = 0, guess = 0;
  srand(22);
  answer = rand() %100;
  bool win = false;

  while (!win)
    {
    cout<< "Player1s' turn to guess " <<end1;
    guess = Player1.getGuess();
    win = checkForWin(guess, answer);
    if (win) return 0;
    cout<< "Player2's turn to guess " <<end1;
    guess = Player2.getGuess();
    win = checkForWin(guess, answer);
    }
 }

 int main()
 {
     system("COLOR 1E");

        Humanplayer Player1,Player2;
        play( Player1, Player2);

        system("pause");
        getch();
}

Screenshot 2

**left window**
:start
{
    cout<<"Select Options \n";
    cout<<" 1 for Human vs Human\n 2 for Human vs Computer\n 3 for Computer vs Computer
    cin>>opt;

    switch(opt)
    {

    case 1:
        play( Player1, Player2);
        break;
    case 2:
        play( Player1, Comp1);
        break;
    case 3:
        play(Comp1, Comp2);
        break;
    default:
        cout<<" Invalid entry!!!\n Please enter 1,2 or 3!!!\n"; 
        goto start;
    }
}

**right window**
Select Options
 1 for Human vs Human
 2 for Human vs Computer
 3 for Computer vs Computer
h

If the user enters a char instead if an int I do not know how to handle this in the switch.

Chuchoter
  • 148
  • 2
  • 11
  • 7
    Please post code as text. No one will retype it out to compile it. – Paul Rooney May 21 '17 at 12:34
  • i don't now how to post code,i'm new here....can u help? – Adeel Rafaqat May 21 '17 at 12:41
  • Try [this](http://stackoverflow.com/questions/16388510/evaluate-a-string-with-a-switch-in-c/16388610#16388610). Of course using C++11. – Gleb Ignatev May 21 '17 at 12:44
  • @AdeelRafaqat see [this](https://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks) post or failing that just paste the code in and someone will format it. I think we have the thrust of your question now so posting the code might not be necessary. – Paul Rooney May 21 '17 at 12:47
  • @AdeelRafaqat I think your question is about something other than the switch? Its about the user entering the wrong data type and how to handle this? – Paul Rooney May 21 '17 at 12:57
  • @PaulRooney yeah u r right....exactly – Adeel Rafaqat May 21 '17 at 13:02

3 Answers3

1

A switch statement operates on integers (and types implicitly convertible to integers). If you have, say, a string or some other type, then don't use switch.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
0

C++ only deals with switching integers

condition - any expression of integral or enumeration type, or of a class type >contextually implicitly convertible to an integral or enumeration type, or a >declaration of a single non-array variable of such type with a brace-or-equals >initializer.

so your data must either be an int type or cast into an int type. For chars you can use single quotes to get its numerical value

switch(opt)
{
case 'A' : // do something for A
         break;      
case 'B' : // do something for B
         break;
default : break; //neither A or B were assigned to userInput
...etc
}
kp122
  • 41
  • 6
0

I think this question is not about switch statements at all but about handling bad input from cin. If the user enters a value that is not an int, then you don't want to try to handle it in the switch. You want to detect it and perhaps issue an error message and have them try again or exit.

Try this.

#include <iostream>
using namespace std;

int main()
{
    int i = 0;
    cout << "enter a number: ";

    /* Test the truthiness of cin after the text entry
       if the user didnt enter an int value the test
       will return false.
    */
    if(! (cin >> i) )
    {
        cout << "didnt enter a number\n";
        return 0;
    }

    /* now use knowing value must be int */

    switch(i)
    {
        case 1:
            cout << "one\n";
            break;

        // more number cases

        default:
            cout << "some other num\n";
            break;
    }
}

Another relevant answer here.

Community
  • 1
  • 1
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61