1

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?

  • 1
    If you have to prepend your question with "Sorry if this is vague", then you can almost be certain that you are about to ask a bad question. Without even looking at it, I can already tell it's much too long, has far too much code and likely asks far more than one question in the body. I **highly** recommend you to read the [ask] page and then change your question to include a [mcve]. – MechMK1 Dec 01 '17 at 10:49
  • 1
    Okay, thank you. I will attempt to shorten the question. – Brannon Penrod Dec 01 '17 at 10:51
  • I'd suggest you to think about one specific problem you have and then write a minimal example that shows your problem. From the title alone, I can see you have problems getting user input again after some event, yet your code includes cruft like `levelUp()`, character creation and what not. All of this is completely irrelevant to the question at hand. Just to give you an idea of formatting, here is [a question I have written](https://stackoverflow.com/questions/39443342/setconsoleactivescreenbuffer-does-not-display-screen-buffer) with similar scope of problems. – MechMK1 Dec 01 '17 at 10:55
  • 1
    Also, "reducing your code to a minimal format" does not mean "just cut some lines away." I mean create a [mcve], with the emphasis on **complete**. – MechMK1 Dec 01 '17 at 11:01

0 Answers0