-1

I am fairly new to C++, as I am currently up to and studying switch statements for this language tool. I am having problems with my code for I lack the understanding required for making relevant configurations that would help solve possible outputting problem. This problem resolves around the problem outputting the default switch choice, as opposed to the desirable case when entering the relevant input character:

#include <iostream>
using namespace std;

int main()
{
    char Poke;
    cout << "Please select your starter Pokemon:" << endl;
    cin >> Poke;

    switch (Poke) {

    case 'Bulbasaur':
        cout << "You have selected " << Poke << endl;
        break;
    case 'Charmander':
        cout << "You have selected " << Poke << endl;
        break;
    case 'Squirtle':
        cout << "You have selected" << Poke << endl;
        break;
    default:
        cout << "Entry Unknown" << endl;
        break;
    }
}

Yes this code is based on the original three starter Pokemon from Gen One. Whenever I select, choose and input 'Charmander' onto the command prompt window box, it would not read my input for some reason and would only output the default which would be "Entry Unknown":

Command Prompt output problem

Sorry I couldn't embedded image. Don't have enough reputation points :/

drescherjm
  • 10,365
  • 5
  • 44
  • 64
sukh_johal95
  • 151
  • 1
  • 1
  • 9
  • 2
    `char` means a single character. You should use strings instead. – M.M Jan 09 '17 at 00:03
  • @M.M Oh okay. What variable type would be best for user input instead? – sukh_johal95 Jan 09 '17 at 00:04
  • You should consult some reference material such as a book or tutorial, it's not feasible to learn C++ by guessing – M.M Jan 09 '17 at 00:05
  • 1
    `std:string` instead of `char`. – Hatted Rooster Jan 09 '17 at 00:07
  • switch work only with trivial types, can't compare strings. The solution here is to create std::list of appropriate strings and try find the input.. if input isn't in list. Oh, and you should think of capitalization, etc. – Swift - Friday Pie Jan 09 '17 at 00:09
  • Possible duplicate of [Why switch statement cannot be applied on strings?](http://stackoverflow.com/questions/650162/why-switch-statement-cannot-be-applied-on-strings) – Soren Jan 09 '17 at 00:44
  • Very similar to http://stackoverflow.com/questions/41469864/switch-statements-using-string-input/41469927#41469927 apart from you are asking the same question in C++ and not in C – Soren Jan 09 '17 at 00:45

3 Answers3

0

'Bulbasaur' is a multicharacter literal (see syntax 6 here). "Bulbasaur" would be a character string (notice the different quote character). You cannot switch with a string or std::string. To achieve you will need to use a series of if or use a container such as std::map or std::unordered_map.

Additionally, in char Poke; cin >> Poke; the type char indicates that Poke is a single charater. You can only store a single character in Poke. Use std::string instead.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
0

You should see some basic Input/Ouput C++ tutorial like this:

http://www.cplusplus.com/doc/tutorial/basic_io/

Also remember that switch can be used only with integer values.

I have made the necessary changes and this works fine:

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

int main()
{
    string Poke;
    cout << "Please select your starter Pokemon:" << endl;
    getline(cin, Poke);    

    if (Poke == "Bulbasaur")
        cout << "You have selected " << Poke << endl;
    else if (Poke == "Charmander")
        cout << "You have selected " << Poke << endl;    
    else if (Poke == "Squirtle")
        cout << "You have selected" << Poke << endl;
    else
        cout << "Entry Unknown" << endl;
}

The necessary changes were:

  • Poke variable type from char to std::string
  • From cin to getline, that takes the entire line.
  • Changes Switch statement to if statements to be able to compare full strings as you were doing.
jgorostegui
  • 1,290
  • 1
  • 8
  • 13
0

I am currently up to and studying switch statements for this language tool.

A switch statement can only be used for integral values, not for values of user-defined type. So even if you try to use C++'s std:string class, it will not work. Since your primary requirement is to use switch-case, try something like the following:

#include <iostream>
using namespace std ;

int main()
{
   char Poke;
   cout<<"Please select your starter Pokemon:"<<endl;
   cout<<"b for Bulbasaur, c for Charmander, s for Squirtle"<<endl;
   poke = getchar(); //Take one char from standard input

   switch(Poke){

      case 'b' : 
         cout<<"You have selected "<<Poke<<endl;
         break;
      case 'c' : 
         cout<<"You have selected "<<Poke<<endl;
         break;
      case 's' : 
         cout<<"You have selected"<<Poke<<endl;
         break;
      default:
         cout<<"Entry Unknown"<<endl;
         break;
   }

}
VHS
  • 9,534
  • 3
  • 19
  • 43