0

I have this program that i took it out from: https://intcpp.tech-academy.co.uk/input-validation/ and it works fine, i did some changes because i need the program to keep asking the user to enter a valid input, so that why it has the while in there however it only asks 4 times after that 4th time the input will be valid it does not matter if it right or not, Does any one know how i can fix this. Thank you

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main () {

    cout << "Please enter name:" << endl;
    string userName;
    getline(cin, userName);

    bool rejected = false;

    while (rejected == false)
    {
        for (unsigned int i = 0; i < userName.length() && !rejected; i++)
        {

            if (isalpha(userName[i]))
                continue;

            else if (userName[i] == ' ')
                continue;

            else
            {
                cout << "Error, Please enter Patient's name again, First Name: ";
                getline(cin, userName);
                rejected = false;
            }

        }
        rejected = true;
    }

    system("pause");
    return 0;
}
  • May I suggest you do some [rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging) on your code? Or use an actual debugger to step through the code line by line? That should help you figure out the error pretty quickly. – Some programmer dude Nov 15 '18 at 02:11
  • i think i found the program, break works fine too and the way you show me too but rejected = true; where should be that on at? i tried in 3 different places and in one seems to do nothing until you add a letter in the 2 to places seems to run only one time. Thank you – Rosario Monroy Nov 15 '18 at 03:07

2 Answers2

1

Personally I would do something like

bool is_valid_username(std::string const& username)
{
    // First trim the string of all leading and trailing white-space
    trim(username);

    if (username.length() == 0)
        return false;  // Input was empty or all spaces

    return std::all_of(begin(username), end(username), [](char const ch)
    {
        return std::isalpha(ch) || ch == ' '; // Only letters and spaces are allowed
    });
}

std::string get_username()
{
    std::string username;

    do
    {
        std::cout << "Please enter username: ";
        std::getline(std::cin, username);
    } while (!is_valid_username(username));

    return username;
}

[For the trim function please see this old answer]

The get_username function will continue to ask for a username forever if the input is either empty, all spaces, or contains non-letters or not a space.

Here's a reference for std::all_of.

Here's a reference about lambda expressions.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0
    if (isalpha(userName[i]) || (userName[i] == ' '))
        continue;
    else
    {
        cout << "Error, Please enter Patient's name again, First Name: ";
        getline(cin, userName);
        i = -1; //Reset check name
    }

Try it! Change unsigned int to int

  • Resetting `i` to `0` won't work, because it will be incremented in the next loop so it will check from `1`, so if the first character was an invalid one this would still pass. – Tas Nov 15 '18 at 03:10
  • You definitely don't want to `-1` because it's an `unsigned int`, so it will break immediately from the `for` loop – Tas Nov 15 '18 at 03:37