Legal drinking age

My program is crashing when I type in the the character that registers for the else condition. The if, and the else if work perfectly fine. Any ideas?
Appreciate the help.



// LEGAL DRINKING AGE

#include <iostream>
using namespace std;

int main()
{
int age;
bool answer;
answer = true;

while (answer == true)
{
cout << "Please enter your age: ";
cin >> age;

if ((age >= 21) && (age <= 125))
{
cout << "You can drink.\n";
}
else if ((age > 0) && (age < 21))
{
cout << "You cannot drink.\n";
}
else
{
cout << "Are you sure you're human?";
}

cout << "Would you like to try again? (tpye 1 for yes, and 0 for no)";
cin >> answer;
}
return 0;
}
Last edited on
What are you entering to trigger the else condition?
Your code looks fine and works well when you enter a number. Of course it crashes when you input any other characters than digits for age or any other key than 0 or 1 for answer.
One way to solve it is to get the input as a string, check that it is valid and convert it to an int or bool.
The idea is; if the input is anything outside the (decision makers) boundaries for the if and else if, which would be anything that is not the integers 1-125, that it would output the else: "Are you human?".

For instance, if I type in 'a' for the input. I get that crash i mentioned, instead of the else statement.
Oh.. It's crashing because mixed data types. Obvious oversight on my part. Thanks Thomas I see what you mean. Is there any reason to convert it though?
You may use cin.fail() and cin.ignore().
Is there any reason to convert it though?

One reason might be to make it work. Telling if a string represents a number that is above and below a particular value isn't a particularly convenient thing to do without converting the string into an integer value.

Here's a version using a string and conversion (and execeptions, since std::stoi may throw one.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string>

int to_int(const std::string& txt, int base = 10) {    
    std::size_t consumed;
    int value = std::stoi(txt, &consumed, base);
        
    if (consumed != txt.size()) 
        throw std::invalid_argument("Full text not convertible to integer value");

    return value;
}

int main()
{
    bool should_continue;
    do 
    {
        std::string input;

        std::cout << "Please enter your age: ";
        std::cin >> input;

        try {
            int age = to_int(input);
            if (age < 0 || age >= 125)
                throw "You ain't human";

            if (age >= 21)
                std::cout << "You can drink.\n";
            else
                std::cout << "You cannot drink.\n";
        }
        catch (...) { std::cout << "Are you sure you're human?\n"; }


        std::cout << "Would you like to try again? (1 = yes)\n> ";
        std::cin >> input;

        should_continue = input == "1";

    } while (should_continue);
}
Topic archived. No new replies allowed.