-1

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.

1201ProgramAlarm
  • 30,320
  • 7
  • 40
  • 49
UnoaCaso
  • 47
  • 6
  • Why did you add `cin.ignore()`? If you remove it, first letter wouldn't be ignored. – fas Apr 28 '20 at 22:27
  • I added it because getine() not worked without it. – UnoaCaso Apr 28 '20 at 22:34
  • @UnoaCaso then you are not using it correctly – Remy Lebeau Apr 28 '20 at 22:43
  • 1
    Do you know why you sometimes need `cin.ignore()` before using `getline`? See https://stackoverflow.com/q/21567291/9254539. Don't write code that you don't understand. That's [cargo cult programming](https://en.wikipedia.org/wiki/Cargo_cult_programming) and will get you nowhere. Instead, understand the problem. – BessieTheCookie Apr 28 '20 at 22:51
  • 1
    If you've solved your problem, you can post an answer yourself. Don't edit the solution into the question. – 1201ProgramAlarm Apr 28 '20 at 23:28
  • Why are you not simply using [std::basic_string::find_first_of](https://en.cppreference.com/w/cpp/string/basic_string/find_first_of) to check for invalid characters? – David C. Rankin Apr 28 '20 at 23:44
  • @1201ProgramAlarm Thanks, I don't knew that. – UnoaCaso Apr 28 '20 at 23:55
  • @David C. Rankin simply because i've never used that, in fact I'm going to learn about it rn. – UnoaCaso Apr 28 '20 at 23:57
  • Have a look at [get valid name example](https://paste.opensuse.org/38588731) which shows the use of `.find_first_of()` and `.find_first_not_of()`. (expires: Tue May 26 19:42:48 CDT 2020) – David C. Rankin Apr 29 '20 at 00:44

1 Answers1

0

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

UnoaCaso
  • 47
  • 6