简体   繁体   English

从第二个字母打印的字符串

[英]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.我的意思是,如果插入的名称包含Invalid_Name内的字符之一,则它无效。

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;如果我插入名称 "edward kenway" 它会打印出"Dward Kenway" ,即使我写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()".我解决了这个问题,只是将“cin.sync()”放在“getline()”之前。 Hope that can helps someone.希望可以帮助某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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