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?
C++ quiz
- hpprinter100
- Fame ! Where are the chicks?!
- Posts: 214
- Joined: 19 Oct 2007, 16:00
- 16
- Contact:
C++ quiz
Last edited by hpprinter100 on 11 Dec 2011, 11:08, edited 1 time in total.
- hpprinter100
- Fame ! Where are the chicks?!
- Posts: 214
- Joined: 19 Oct 2007, 16:00
- 16
- Contact:
Re: C++ quiz
Sorry, Fixed
Re: C++ quiz
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.
Hope this helps.
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
}
¯\_(ツ)_/¯ It works on my machine...
- hpprinter100
- Fame ! Where are the chicks?!
- Posts: 214
- Joined: 19 Oct 2007, 16:00
- 16
- Contact:
Re: C++ quiz
Thanks