Thread: What's wrong with my switch case?

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    38

    Unhappy What's wrong with my switch case?

    Please be easy on me, I'm still a new programmer.

    Code:
    using namespace std;
    void Trailer();
    void Cookies();
    void TurtlePower();
    int main()
    {
      int x;
       switch (x){
    
       case 1:
       cout<<"Trailer"; // This is where I want it to display the word Trailer...
       break;
    
       case 2:
       cout<<"Cookes"; // This is where I want it to display the word cookies....
      break;
    
       case 3:
     cout<<"Turtle Power"; // This is where I want it to display the words Turtle Power...
       break;
    
       }
       cin.get();
    }
    //The idea is that the program displays the words after cout whenever the case is selected. I hope you understand this, i'm not very good at posting tags.

  2. #2
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    You never say what x is, which could be dangerous. Have something like int x = 2;.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    also, add a default case to catch any other values than the ones you specify.

    Code:
    using namespace std;
    void Trailer();
    void Cookies();
    void TurtlePower();
    int main()
    {
      int x;
      switch (x)
      {
       case 1:
         cout<<"Trailer"; // This is where I want it to display the word Trailer...
         break;
    
       case 2:
         cout<<"Cookes"; // This is where I want it to display the word cookies....
         break;
    
       case 3:
         cout<<"Turtle Power"; // This is where I want it to display the words Turtle Power...
         break;
    
       default:
         cout << "an invalid value was encountered" << endl;
         return 0;
      }
      cin.get();
    }
    I always have a default case in my switches, unless the variable being switched is of an enum type. then the compiler checks to see what the value is, and throws warnings or errors if it is outside the valid range for the enum.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function switch help
    By etbjr182 in forum C Programming
    Replies: 9
    Last Post: 02-20-2009, 03:51 PM
  2. switch case statement
    By stanlvw in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 05:06 AM
  3. Can someone please clean up my code
    By ki113r in forum C Programming
    Replies: 10
    Last Post: 09-12-2007, 10:03 AM
  4. Keypress reading
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 06-16-2004, 12:16 PM
  5. case statement wrong?
    By Swaine777 in forum C++ Programming
    Replies: 4
    Last Post: 06-29-2003, 06:46 PM

Tags for this Thread