Switch structure not working
Jun 1, 2009 at 12:46pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
case 2:
int roll = Random(1, 4);
if (roll == 1)
{
cout << "You run away!" << endl;
return true ;
}
else
{
cout << "You could not escape." << endl;
return false ;
}
default :
cout << "Wrong selection." << endl;
return false ;
Error: error C2361: initialization of 'roll' is skipped by 'default' label
I have checked the {} and they seem to be okay. What is wrong with this?
Thanks
Jun 1, 2009 at 1:00pm UTC
Try putting a break ;
before your default :
label
Jun 1, 2009 at 1:02pm UTC
Hey,
I've tried that already but it didn't work :(
Jun 1, 2009 at 1:06pm UTC
Enclose the scope of 'roll' into braces:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
case 2:
{//
int roll = Random(1, 4);
if (roll == 1)
{
cout << "You run away!" << endl;
return true ;
}
else
{
cout << "You could not escape." << endl;
return false ;
}
}// now roll is out of scope
default :
cout << "Wrong selection." << endl;
return false ;
Jun 1, 2009 at 1:11pm UTC
That works fine, thank you Bazzy :)
Topic archived. No new replies allowed.