C++ quiz

Questions about programming languages and debugging
Post Reply
User avatar
hpprinter100
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 214
Joined: 19 Oct 2007, 16:00
16
Contact:

C++ quiz

Post by hpprinter100 »

I am having some trouble with default in switch statements.

I am making a multiple choice quiz. I have to use the switch statement for the multiple choice part.

The code is http://pastebin.com/rm5y5dGw

and using the header file : http://pastebin.com/tz2gqu3H

The problem is that when I use default to goto the start of the question if an invalid char is entered. it seems like it just runs through again straight to the default, then back to the goto. Not sure why as it works perfectly fine in the toll dice case 0:

Any ideas where i am going wrong?
Last edited by hpprinter100 on 11 Dec 2011, 11:08, edited 1 time in total.

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Re: C++ quiz

Post by Gogeta70 »

Your first link is broken.
¯\_(ツ)_/¯ It works on my machine...

User avatar
hpprinter100
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 214
Joined: 19 Oct 2007, 16:00
16
Contact:

Re: C++ quiz

Post by hpprinter100 »

Sorry, Fixed

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Re: C++ quiz

Post by Gogeta70 »

Well, you have an issue with your syntax.

Any case, including the default case, should be followed with curly brackets { and } if you assign a value to a variable. A good rule of thumb is to just always use the curly brackets, as it makes your code neater as well.

Also, each case except the last case in a switch statement should be followed by a "break;". If you don't do that, the switch will continue executing the next case until it either reaches a break or the end of the switch statement.

Code: Select all

switch(a)
{
    case 0:
    {
        switch(b)
        {
            case 0:
            {
                //some code here
            }
            break;
            
            default:
            {
                //some other code here
            }
            //don't need a break here because this is the last case in this switch
        }
    }
    break;
    
    default:
    {
        //some default code here
    }
    //no break needed
}
Hope this helps.
¯\_(ツ)_/¯ It works on my machine...

User avatar
hpprinter100
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 214
Joined: 19 Oct 2007, 16:00
16
Contact:

Re: C++ quiz

Post by hpprinter100 »

Thanks :)

Post Reply