Help Wanted

Help with C++ error message – mellonamin

mellonamin

Member

Posts: 119
From: Maryville, TN, United States
Registered: 11-16-2004
Can someone tell me what is wrong with this code???


void enter_game()
{
int idnumber;
string name;
cout<<"Please input your character identification number: ";
cin>>idnumber;
switch (idnumber)
{
case 1:
name="Mellonamin";
cout<<"\nWelcome, "<<name<<"!";
string password;
cout<<"\nPlease input your password: ";
cin>>password;
if (password=="nanananana")
{
cout<<"Password Verified...Proceeding to The Circle!\n";
Sleep(2000);
game();
}
else
{
cout<<"Alas, that is not the password registered by that player.\n";
Sleep(3000);
enter_game();
}
break;
case 2:
cout<<"Alas, there is no character by that number.\n";
cout<<"Proceed to character creation process (y/n)? ";
string proceedcc;
if (proceedcc=="y" || proceedcc=="Y")
{
create_character_name();
}
else
{
cout<<"Okay then.\n";
Sleep(2000);
main();
}
break;
}
}


These are the error messages I get when compiling it:

In function `void enter_game()':
jump to case label (default)
crosses initialization of 'std::string password'

And then these warnings:

destructor needed for 'std::string password'
where case label appears here (default)
(enclose actions of previous case statements requiring destructors in their own)


I am so confused....

------------------
Vita sine Ieso est mors.
Life without Jesus is death.

Briant

Member

Posts: 742
From: Stony Plain, Alberta, Canada
Registered: 01-20-2001
I'm not sure what compiler you're using, but they will handle the problem differently. The problem is basically that case 1 is in the same "scope" as case 2, and that your variable "password" has its initialization skipped if the case 2 code is executed (even though "password" isn't used there.

There are two simple adjustments you can make to get around this:

1. move your "string password" outside of the switch statement (ie. put it with "string name")

OR

2. use squiggly brackets inside your case statements to restrict scope
ie.:

switch(idnumber)
{
case 1:
{
// code for case 1
}
break;
case 2:
{
// code for case 2
}
break;
}

I hope that helps.

God bless,
Brian

------------------
Brian

mellonamin

Member

Posts: 119
From: Maryville, TN, United States
Registered: 11-16-2004
That helps alot....thanks soooooo much!

------------------
Vita sine Ieso est mors.
Life without Jesus is death.