-
December 3rd, 2009, 04:02 PM
#1
Empty Strings
I have a do-while for while a string is not empty. The problem is that whenever I enter just a new line (to have an empty string), nothing happens. A new line is put into the DOS window but it doesn't...do anything. It just goes to the next line and I'm still in the loop.
Code:
vector<Character>& makeCharacters() {
vector<Character> theParty;
bool moreChars, classCorrect;
do {
string name, clas;
cout<<"Enter the name of a character"<<endl;
cin>>name;
if(name.empty()) //SHOULD SET BOOL TO FALSE AND LEAVE DO-WHILE
moreChars = false;
else {
moreChars = true;
do {
cout<<"Enter the class of the character"<<endl;
cin>>clas;
if(clas.compare("fighter") != 0 && clas.compare("Fighter") != 0
&& clas.compare("mage") != 0 && clas.compare("Mage") != 0
&& clas.compare("healer") != 0 && clas.compare("Healer") != 0)
{
cout<<"Sorry, that is an invalid class choice."<<endl;
cout<<"The choices are Fighter, Mage, and Healer."<<endl;
classCorrect = false;
}
else
classCorrect = true;
} while (!classCorrect); //END DO WHILE FOR CHAR CLASS
Character one(name, clas); //MAKE NEW CHARACTER
theParty.push_back(one);
} //END ELSE
} while(moreChars); //END DO WHILE FOR MORE CHAR
return theParty;
} //END MAKE CHARACTERS
The if statement if(name.empty()) is where the problem is. I want it to set the bool to false if it is empty. What am I missing here?
-
December 3rd, 2009, 04:13 PM
#2
Re: Empty Strings
Well instead of
I changed to
Code:
getline(cin, name);
While this gives me a response, it is not what I want. The program aborts after I enter a blank line. Could that be due to my main?
Code:
int main() {
vector<Character> party = makeCharacters();
for(unsigned int i=0;i<party.size();i++)
cout<<party.at(i).toString()<<endl;
}
-
December 3rd, 2009, 04:44 PM
#3
Re: Empty Strings
Well apparently it doesn't go through main's for loop because party's size is empty.
Code:
cout<<"THEPARTYS SIZE: "<<theParty.size()<<endl;
return theParty;
} //END MAKE CHARACTERS
int main() {
vector<Character> party;
party = makeCharacters();
cout<<"PARTYS SIZE: "<<party.size()<<endl;
for(unsigned int i=0;i<party.size();i++)
cout<<party.at(i).toString()<<endl;
}
At the end of makeCharacters(), I return the vector theParty. So when I declare
Code:
party = makeCharacters();
shouldn't that make party equal to the vector returned from makeCharacters()? When I dispay theParty's size, it will be more than zero. But party's size is always zero.
-
December 3rd, 2009, 05:35 PM
#4
Re: Empty Strings
Nope, you're returning the vector by reference. Once you return from the function, the vector is destroyed, and the reference is invalid.
Viggy
-
December 3rd, 2009, 05:36 PM
#5
Re: Empty Strings
Seems to me you made the cardinal mistake of returning a reference to a local object. The object is destroyed by the time the function returns. Return by value instead.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|