Switch?

This is a discussion on Switch? within the C++ Programming forums, part of the General Programming Boards category; can u put a switch statement inside a switch using the same numbers for cases? Example: Code: switch (menu) { ...

  1. #1
    lv.42 Berserker Drake's Avatar
    Join Date
    Jun 2005
    Posts
    67

    Question Switch?

    can u put a switch statement inside a switch using the same numbers for cases?

    Example:
    Code:
    switch (menu)
        {
        case 1:
            int difficulty;
            std::cout<<"1.Easy"<<std::endl;
            std::cout<<"2.Normal"<<std::endl;
            std::cout<<"3.Heroic"<<std::endl;
            std::cout<<"4.GODLIKE"<<std::endl;
            std::cout<<"Choose a difficulty level:"<<std::endl;
            std::cin>>difficulty;
                    switch(difficulty)
                    {
                    case 1:
                    std::cout<<"You chose Easy"<<std::endl;
                    break;
                    
                    case 2:
                    std::cout<<"You chose Normal"<<std::endl;
                    break;
                    
                    case 3:
                    std::cout<<"You chose Heroic"<<std::endl;
                    break;
                    
                    case 4:
                    std::cout<<"You chose GODLIKE"<<std::endl;
                    break;
                    
                    default:
                   	std::cout<< "You made an illegal choice.\n"<<std::endl;
                   	break;
                    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    yes but don't forget the break statement following you're second switch statement at the end of case 1.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Drake why do you use the std::cout command instead of just doing this...?
    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    
    int main()
    {
    //Program code
    //the using std::cout; allow you to use cout without having to put the std:: in front of it everytime.
    just a suggestion

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    An even more succinct solution is to just put
    Code:
    using namespace std;
    right there, but it's almost always merely a matter of personal style. Drake's method is more common than you'd think.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    What you can't do is have duplicate cases:
    Code:
    switch(x) {
    case 0:
    case 0:
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 05:40 PM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21