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;
}
2
  • May I suggest you do some 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. Commented Nov 15, 2018 at 2: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 Commented Nov 15, 2018 at 3:07

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

thank you, this one is seems to be working fine but the part i posted it only a part of my program, i dont know how i can join this way to the other part of my program. Thank you so much.
@RosarioMonroy All the code to read and verify the name in your question could be removed and replaced with a single line string userName = get_username();.
@RosarioMonroy Also note that while the use of trim(username) is good to have, it's optional.
How do i call it from main?
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

2 Comments

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.
You definitely don't want to -1 because it's an unsigned int, so it will break immediately from the for loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.