CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2006
    Posts
    329

    switch statement

    Hi all, can someone tell me why I get errors with this code?

    Code:
    #include <iostream>
    using namespace std;
    
    int main(){
     char buf;
     bool finished=false;
     cout << "Enter something " << endl; 
     cin >> buf;
     while(!finished){
     switch(buf){
      case "sloppyjoe":
      cout << "Some good stuff! " << endl;
      finished=true;
      break;
      
      case "cheeseburger":
      cout << "Got french fries? " << endl;
      finished=true;
      break;
      
      default:
      cout << "That isn't on the list " << endl;
      finished=true;
      break;}}
      return 0;
    }
    When I set case to something like this ...

    Code:
    case 'A':
    It compiles and works fine, except when I make it a string. What's the problem? Here's the error I get..

    case label does not reduce to an integer constant
    Thanx.

  2. #2
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: switch statement

    case "sloppyjoe":

    what is this ?.. you are putting char array as case label. In your code you should use if-else with strcmp with char array instead of char only.

    And why do you posted a simply C/C++ query in VC++ forum ?. There is separate forum for this.

  3. #3
    Join Date
    Nov 2006
    Posts
    329

    Re: switch statement

    I've not had a lot of experience with the switch statement so I suppose that case "sloppyjoe" is weird. But I need to learn how to implement a switch statement that allows this type of behavior. Is there any example you can give me while I continue my research to point me in the right direction? Thanx.

  4. #4
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: switch statement

    you cant use switch statement to match through array of character. use if-else with char array as I suggested earlier.

  5. #5
    Join Date
    Apr 2008
    Posts
    725

    Re: switch statement

    Quote Originally Posted by dellthinker View Post
    Hi all, can someone tell me why I get errors with this code?

    Code:
    #include <iostream>
    using namespace std;
    
    int main(){
     char buf;
     bool finished=false;
     cout << "Enter something " << endl; 
     cin >> buf;
     while(!finished){
     switch(buf){
      case "sloppyjoe":
      cout << "Some good stuff! " << endl;
      finished=true;
      break;
      
      case "cheeseburger":
      cout << "Got french fries? " << endl;
      finished=true;
      break;
      
      default:
      cout << "That isn't on the list " << endl;
      finished=true;
      break;}}
      return 0;
    }
    When I set case to something like this ...

    Code:
    case 'A':
    It compiles and works fine, except when I make it a string. What's the problem? Here's the error I get..



    Thanx.

    why are you only taking one char of the user input? And then in your switch you compre that single char to a char array. Why?

    change 'char buff;' to 'std::string buf;'. you will need to #include <string>. then you may use syntax like

    Code:
    std::string a("aaa"), b("bbb");
    
    if (a == b)
    {
      // do something
    }
    else
    {
      // do something else
    }
    which is easier to read than a whole load of strcmps

  6. #6
    Join Date
    Oct 1999
    Location
    Belgium
    Posts
    440

    Re: switch statement

    switch in C++ is only allowed for integral types (numeric values without decimal part) ! So what you put behind switch and case should be of integral type or convertible without question to integral type. As 'A' is actually 65 (ascii code) the compiler allows this. However, "sloppyjoe" is an array of characters and therefor not convertible to an integral value. The way you should go, as already mentioned here, is checking the user input string with an if/else/else/else ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured