Thread: Help me with switch case!

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    10

    Help me with switch case!

    I have gotten stuck on switch cases, I've looked everywhere, the forums and other c++ websites and I can't get a simple switch case to work for me

    I tried to find a working switch case example on the internet so i can toy around with it myself in a compiler and learn how it works that way but every single example doesn't work!!!!

    Will someone post a simple switch case or link to a website that provide a working switch case for me?

    thanks

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Strange but here you are
    Control Structures
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    10
    It doesn't work with codeblocks compiler for me
    unless I'm doing something wrong

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    What did you try and how does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    10
    I tried the second code from the bottom of the website -
    Code:
    switch (x) {
      case 1:
        cout << "x is 1";
        break;
      case 2:
        cout << "x is 2";
        break;
      default:
        cout << "value of x unknown";
      }
    And added this myself, it doesn't work

    Code:
    #include <iostream>
    
    using namespace std;
    
    int x();
    
    int main ()
    {
    
    switch (x) {
      case 1:
        cout << "x is 1";
        break;
      case 2:
        cout << "x is 2";
        break;
      default:
        cout << "value of x unknown";
      }
      cin.get();
    }
    it tells me

    s File\project9\main.cpp||In function 'int main()':|
    s File\project9\main.cpp|10|error: switch quantity not an integer|
    ||=== Build finished: 1 errors, 0 warnings ===|

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    This declares a function named x that takes no arguments and returns an int:
    Code:
    int x();
    Rather, declare a local int variable named x:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int x = 0;
    
        switch (x) {
        case 1:
            cout << "x is 1";
            break;
        case 2:
            cout << "x is 2";
            break;
        default:
            cout << "value of x is neither 1 nor 2";
        }
    
        cin.get();
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    10
    Sweet thank you!
    But i don't understand if int x is bellow int main(), why in cprogramming's tutorial it puts void playgame, void loadgame, and void multiplayer above int main()
    (that probably doesn't make sense so i highlighted it)

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int x = 0;
    
        switch (x) {
        case 1:
            cout << "x is 1";
            break;
        case 2:
            cout << "x is 2";
            break;
        default:
            cout << "value of x is neither 1 nor 2";
        }
    
        cin.get();
    }



    Code:
    #include <iostream>
    
    using namespace std;
    
    void playgame();
    void loadgame();
    void playmultiplayer();
    	
    int main()
    {
      int input;
      
      cout<<"1. Play game\n";
      cout<<"2. Load game\n";
      cout<<"3. Play multiplayer\n";
      cout<<"4. Exit\n";
      cout<<"Selection: ";
      cin>> input;
      switch ( input ) {
      case 1:            // Note the colon, not a semicolon
        playgame();
        break;
      case 2:            // Note the colon, not a semicolon
        loadgame();
        break;
      case 3:            // Note the colon, not a semicolon
        playmultiplayer();
        break;
      case 4:            // Note the colon, not a semicolon
        cout<<"Thank you for playing!\n";
        break;
      default:            // Note the colon, not a semicolon
        cout<<"Error, bad input, quitting\n";
        break;
      }
      cin.get();
    }

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Read up on declarations and definitions, and the difference between functions and variables.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The compiler starts reading your source from the top and goes down to the bottom. If the prototypes for those 3 function were not there, then when the compiler reached the place in the source code where they were called, it could not validate whether or not they were being called correctly. The end result is that the compiler is going to issue an error if it does not yet know the definition for any function call it comes across. It doesn't matter that you may define it later in the code, the compiler must know the definition for the function by the point when it reaches that statement so it can do its job.

    Those prototypes serve to notify the compiler that there are three functions with those names and that take those arguments so that when it later comes across them in the source being called, it can check and state that "yes these are being called properly" and can then move on to the next line of code.

    The main function itself is special, it only ever comes in certain forms so the compiler will know if it's not in one of those already predefined and accepted forms. You don't need to prototype main.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  3. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM
  4. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 02:07 AM