1
double age = 0;
int empAge;

cout << "Please enter your age in integers: ";
cin >> age;

// Input validation for age. To prevent input with a decimal number eg 5.6, 7.8, 90.9, etc

while(cin.fail() || ((int)age != (double)age)){
    cout << "\nThat is not a valid option, please try again. ";
    cin >> age; 

    if(cin.fail() || (int)age != double(age)){
        cin.clear();
        string not_an_int;
        cin >> not_an_int;
    }
    cout << "That is not a valid option, please try again. ";
    cin >> age;
}
// Assigns the data in variable of double type age to the variable of int type empAge   
age = empAge;

What I need to get done ? Ask user to enter a integer digit only. If not an integer give error "That is not a valid option, please try again" and ask user to enter again.

Attached is also an image showing errors my code gives with the inputs used.

enter image description here

5
  • You probably want to call cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); right after cin.clear() instead of string not_an_int etc. Commented Aug 25, 2018 at 10:05
  • 1
    "Attached is also an image showing errors my code gives with the inputs used." Don't post images of text. Copy-paste text into the question, as text. Commented Aug 25, 2018 at 10:18
  • I don't understand what the question is. Please clarify your question. What is the problem? Commented Aug 25, 2018 at 12:08
  • Reading the comments in your code, I think you are supposed to do something like int tmp_age; cin >> tmp_age; ... // check for input fails ... double age = tmp_age;, which would make the last assignment (or comment) to have sense. Commented Aug 25, 2018 at 14:19
  • @KorelK Apologies for the ambiguity. Basically i need the user to input a integer value, if it is not an integer then give error. Keep repeating until user enters an integer value. Just trying to remove bad input such as alphabets, decimal numbers. Commented Aug 25, 2018 at 17:22

3 Answers 3

1

What I need to get done ? Ask user to enter a integer digit only. If not an integer give error "That is not a valid option, please try again" and ask user to enter again.

I advise you to read this carefully, as it is quite unclear what exactly you are asking.

I've prepared this small snippet for you in case you are asking something like How do I read user input using stdin while validating it is only an integer value?

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

bool is_natural(const std::string s)
{
    return s.find_first_not_of( "0123456789" ) == std::string::npos;
}

int main()
{
    std::string input;
    while (!(std::cin >> input) || !is_natural(input))
    {
        std::cout << "That is not a valid option, please try again.\n";
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }

    int age = std::stoi(input);
    std::cout << "Age (natural integer value) entered: " << age << "\n";
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to read only integers in input, this is a possible solution:

#include <iostream>
#include <string>

using namespace std;

int main() {
    int age;
    unsigned long status;
    string myString;
    do {
        cout << "Please enter your age in integers: ";
        getline(cin, myString);

        /*
         * Return the position of the first character that does not match.
         * Else return "string::npos"
         */
        status = myString.find_first_not_of("0123456789");

        if (status != string::npos)
            cout << "That is not a valid option, please try again." << endl;
    } while (status != string::npos);

    //Convert string to int.
    age = stoi(myString);

    cout << "Age as integer: " << age << endl;
}

You can use stoi() only in c++ 11 or following:

int num = stoi( str )

Old method:

int num = atoi( str.c_str() )

Comments

0
using namespace std;

// The method check if the string only contains digits
bool is_digits(const std::string &str)
{
    return std::all_of(str.begin(), str.end(), ::isdigit);
}

int main()
{
    int age;
    bool status = false;
    string myString;
    while (!status) {
        cout << "Please enter your age in integers: ";
        cin >> myString;

        status = is_digits(myString);
        if (!status)
            cout << "That is not a valid option, please try again." << endl;
    }

    //Convert string to int.
    age = stoi(myString);

    cout << "Age as integer: " << age << endl;
    return 0;
}

Comments

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.