79080132

Date: 2024-10-12 03:40:30
Score: 0.5
Natty:
Report link

Figured it out thanks to Dave C Rankin.

Here is the code:

#include <iostream>
#include <limits>
#include <string>

std::string firstName, secondName, homeTown;
int age;

int main() {
    do {
        std::cout << "Please enter your full name, hometown and age (e.g. John Doe London 25).\n\n";
        std::cin >> firstName >> secondName >> homeTown >> age;
        if (firstName.find_first_of("0123456789") != std::string::npos || secondName.find_first_of("0123456789") != std::string::npos || homeTown.find_first_of("0123456789") != std::string::npos) {
            std::cout << "\nInvalid input!\n\n";
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
        else {
            if (std::cin.fail()) {
                std::cout << "\nInvalid input!\n\n";
                std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            }
            else {
                break;
            }
        }
    } while (1);
    std::cout << "\nYour Name is: " << firstName << " " << secondName << "\n" << "Your Hometown is: " << homeTown << "\n" << "Your Age is: " << age << "\n";
    return 0;
}

Instead of using a single if statement, I split it between two so that firstName, secondName and homeTown could be filtered seperately from age.

This is what I used to for validating the strings: if (firstName.find_first_of("0123456789") != std::string::npos || secondName.find_first_of("0123456789") != std::string::npos || homeTown.find_first_of("0123456789") != std::string::npos) {

find_first_of() allows the code to search for any numbers being inputted for the strings. If any are detected it returns the error message successfully. I used firstName, secondName and homeTown all in one if statement using the ||/OR operator.

for age, I simply implemented a if (std::cin.fail()) to check automatically for any mis-inputs, as it worked in the past just fine.

I also made some miscellaneous changes to the code such as removing using namespace std; that aren't especially important, however, the rest of the code remains unchanged.

Here are some examples of outputs now:

If the user correctly inputs the values:

Please enter your full name, hometown and age (e.g. John Doe London 25).

John Doe London 25

Your Name is: John Doe
Your Hometown is: London
Your Age is: 25

If the user incorrectly inputs age:

Please enter your full name, hometown and age (e.g. John Doe London 25).

a a a a

Invalid input!

Please enter your full name, hometown and age (e.g. John Doe London 25).

If the user incorrectly inputs firstName, secondName and/or homeTown

Please enter your full name, hometown and age (e.g. John Doe London 25).

1 1 1 1

Invalid input!

Please enter your full name, hometown and age (e.g. John Doe London 25).

If the user inputs none of the variables correctly:

Please enter your full name, hometown and age (e.g. John Doe London 25).

1 1 1 a

Invalid input!

Please enter your full name, hometown and age (e.g. John Doe London 25).

Sweet!

Reasons:
  • Blacklisted phrase (0.5): thanks
  • Whitelisted phrase (-1): it worked
  • Contains signature (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Ren