I'm attempting to code a text-based game in C++ for a school project. Sparing unimportant details, I have a character creator that has the user input name and stats, as well as a 'reset character' option that allows the player to start back from the beginning. Here are the important parts of the code so far.
#include <iostream>
using namespace std;
int charCreator();
string cmd, cmd2, name = "name";
int main()
{
cin >> cmd;
if(cmd == "reset"){
while(cmd == "reset"){
cout << "Enter your character's name to reset.\n";
cin >> cmd2;
if(cmd2 == name){
cout << name << " is sent screaming into the dark abyss. A new challenger approaches.\n";
charCreator();
break;
}
}
}
return 0;
}
//function for character creation
int charCreator()
{
cout << "Name: ";
getline(cin, name);
cout << "\nWelcome, " << name << "!\n\n";
return 0;
}
What should happen is that the player 'resets' their character, turning all values to their default values, including name
, but when you reset it skips the name input and leaves it blank. What I want to know is why this is happening and how I can fix it.
EDIT: Changing getline(cin, name)
to cin >> name
seems to have fixed the problem. New question: Is there any way to have getline()
in charCreator()
so that it doesn't produce this problem?