Switch structure not working
Bv202 (172) Mar 20, 2010 at 7:32am 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
fauntleroy42 (25) Mar 20, 2010 at 7:32am UTC
Try putting a break ; before your default : label
Bv202 (172) Mar 20, 2010 at 7:32am UTC
Hey,
I've tried that already but it didn't work :(
Bazzy (4111) Mar 20, 2010 at 7:32am 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 ;
Bv202 (172) Mar 20, 2010 at 7:32am UTC
That works fine, thank you Bazzy :)
This topic is archived - New replies not allowed.