Thread: Strings and Switch Statement

  1. #1
    Registered User Ajsan's Avatar
    Join Date
    Dec 2003
    Posts
    55

    Unhappy Strings and Switch Statement

    i'm having a problem with a switchstatement that uses strings as the argument and it looks something like this

    Code:
    void Game(Game Info,ifstream &fin)
    {
    	int b,c;
    	string a;
    	OpenRoom(Info,fin);
    	cin>>a;
    	while (a!="quit" || a!="exit")
    	{
    		switch (a)
    		{
    		case "remove":
    			Remove(Info,fin);
    			break;
    		case "epuip":
    			Equip(Info,fin);
    			break;
    		case "quit":
    			break;
    		case "northwest":
    			Go(Info,fin,5);
    			break;
    		case "southwest":
    			Go(Info,fin,7);
    			break;
    		case "nw":
    			Go(Info,fin,5);
    			break;
    		case "sw":
    			Go(Info,fin,7);
    			break;
    		case "northeast":
    			Go(Info,fin,4);
    			break;
    		case "southeast":
    			Go(Info,fin,6);
    			break;
    		case "ne":
    			Go(Info,fin,4);
    			break;
    		case "se":
    			Go(Info,fin,6);
    			break;
    		case "w":
    			Go(Info,fin,3);
    			break;
    		case "e":
    			Go(Info,fin,2);
    			break;
    		case "s":
    			Go(Info,fin,1);
    			break;
    		case "n":
    			Go(Info,fin,0);
    			break;
    		case "north":
    			Go(Info,fin,0);
    			break;
    		case "east":
    			Go(Info,fin,2);
    			break;
    		case "west":
    			Go(Info,fin,3);
    			break;
    		case "south":
    			Go(Info,fin,1);
    			break;
    		case "pick":
    			cin>>a;
    			switch (a)
    			{
    			case "up":
    				Get(Info,fin);
    				break;
    			case "nose":
    				cout<<"Thats really sick.\n";
    				break;
    			}
    			break;
    		case "get":
    			Get(Info,fin);
    			break;
    		case "help":
    			cout<<"While this function of the game is not yet complete, it does have a few tips.\n";
    			break;
    		case "drop":
    			cin>>a;
    			Drop(Info,fin);
    			break;
    		case "save":
    			SaveGame(Info,false);
    			break;
    		case "Loki":
    			Loki(Info);
    			break;
    		case "Dogma":
    			Dogma(Info);
    			break;
    		case "buy":
    			Buy(Info,fin);
    			break;
    		default:
    			Error(24);
    		}
    	}
    }
    all of the functions are defined it just gives me an error for every line about a constant i'm using microsoft visual studio .net 2003 and visual c++ pro 6.0. if any one has any thoughts they would be apreciated.

    Code Tags added by Kermi3
    Style is overrated.

    - —₽‚¢‰Î -

  2. #2
    Registered User Ajsan's Avatar
    Join Date
    Dec 2003
    Posts
    55

    Error

    the actual error is

    c:\documents and settings\alli-poo\my documents\visual studio projects\c++\ffec.cpp(471) : error C2051: case expression not constant
    Style is overrated.

    - —₽‚¢‰Î -

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    You can start by using code tags when you post code. It makes it much easier to read, thanks.

    Your problem is that you can't use strings for switch statements. The only allowable constants are numbers (encompasses lots of things) and single characters. What you'll need to do is convert all of that into an if-elseif-else chain:
    Code:
    if (a == "remove") {
       Remove(Info,fin);
    } else if (a == "equip") {
       Equip(Info,fin);
    } else ...

  4. #4
    Registered User Ajsan's Avatar
    Join Date
    Dec 2003
    Posts
    55

    Thx

    Sry bout the Code tags i forgot and thx for the input about the switch statement i didn't know that....
    Style is overrated.

    - —₽‚¢‰Î -

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Just a refinment of Speedy5's answer, in a switch you can only have integer values. That ecompasses a lot a lot (char, enums, ints, short ints, etc) but doesn't allow floats, doubles, etc.

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    127
    Quote Originally Posted by Thantos
    Just a refinment of Speedy5's answer, in a switch you can only have integer values. That ecompasses a lot a lot (char, enums, ints, short ints, etc) but doesn't allow floats, doubles, etc.
    Another slight refinement. Cases can only be constant integral expressions. So any use is limited to literals or const variables.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You may want something like this:
    Code:
    std::string Command;
    if(Command == "North")
    {
       ...
    }
    else if(Command == "South")
    {
       ...
    }
    else if ...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some Trouble - Switching with strings.
    By mintsmike in forum C++ Programming
    Replies: 5
    Last Post: 05-02-2009, 09:04 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. using switch with strings
    By nextus in forum C++ Programming
    Replies: 4
    Last Post: 11-08-2002, 08:13 PM
  4. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM
  5. Switch statements for strings
    By cxs00u in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2002, 03:38 PM