简体   繁体   中英

String printed from the 2nd letter

The purpose of this code is to validate a name and print each word with the 1st letter capitalize. I mean if the name inserted contains one of the char that are inside Invalid_Name it's not valide.

It works properly, but when I try to print that name, it starts from the 2nd letter. Just look at the code below(sorry for the size):

 void validate_charName()
{
    int i = 0;
    int check = 0;
    string Invalid_Name = "1234567890'!\"£$%&/()=?^*-+<>-.,_:;[]{}°@§";
    bool name_is_valide = true;

    // Validate the character's name
    while(name_is_valide)
    {
        bool invalid_name = false;
        name_is_valide = true;

        cout<< "\nWhat's the name of your character? " << endl;
        cin.ignore();
        getline(cin, char_name);

        for(int i = 0; i < char_name.length(); i++)
        {
            for(int j = 0; j < Invalid_Name.length(); j++)
                {
                    if(char_name[i] == Invalid_Name[j])
                        invalid_name = true;
                }
        }
        if(invalid_name)
            cout<< "The name must contains only letters!" << endl;
        else
            name_is_valide = false;

    }


    while(char_name[i])
    {
        if(check == 0)
        {
            char_name[i] = toupper(char_name[i]);
            check = 1;
        }
        else if(isspace(char_name[i]))
            check = 0;
        i++;
    }
        sleepcp(1000);
        cout << "               ******Your character's name is " + char_name + "******                " << endl;
}

If I insert the name "edward kenway" it will print out "Dward Kenway" , even if I write i = 0; before the loop.

Why?

*************EDIT*************

If i print the name in the right way since the first input, it works. Otherwise, it gives me that problem.

I solved this just putting "cin.sync()" before "getline()". Hope that can helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM