Advice needed

Just started learning C++ using Allain's "Jumping into C++". And I'm currently stuck on Chapter 4 Practice problem 1. I've actually done the thing and what is needed of me there, but I was just wondering about 1 thing. In the below code, then I build and run it, if I enter letters at the "person's age" prompt, the program just runs the rest of the code printing every 'cout' on the way. Is there any way to stop this behaviour?

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>

int main()
{
    std::string name1 = "<unknown>";
    std::string name2 = "<unknown>";
    int age1;
    int age2;
// entering data
    std::cout << "What's the name of the first person?" << "\n";
    std::getline(std::cin, name1, '\n');

    std::cout << "What's the name of the second person?" << "\n";
    std::getline(std::cin, name2, '\n');

    std::cout << "Please enter " << name1 << "'s age:" << "\n";
    std::cin >> age1;

    std::cout << "Please enter " << name2 << "'s age:" << "\n";
    std::cin >> age2;


// checking if age != 0
    if (age1 == 0 ) {
        std::cout << "Age nas to be numeric and greater than 0. Please re-enter " << name1 << "'s age:"<< "\n";
        std::cin >> age1;
    }
    if (age2 == 0) {
        std::cout << "Age nas to be numeric and greater than 0. Please re-enter " << name2 << "'s age:"<< "\n";
        std::cin >> age2;
    }
// main task body
//checking if both are above 100
    if (age1 > 100 && age2 > 100) {
        std::cout << "Have they gone senile yet?";
    }
//checking if person 1 is older than 100
    else if (age1 > 100) {
        std::cout << name1 << " is one old fk!";
    }
//same for person 2
    else if (age2 > 100) {
        std::cout << name2 << " is one old fk!";
    }
// are they the same age?
    else if (age1 == age2) {
        std::cout << name1 << " and " << name2 << " are of the same age" << "\n";
    }
// is there only a year difference?
    else if (age1 - age2 == 1) {
        std::cout << name1 << " is older by a year." << "\n";
    }
    else if (age2 - age1 == 1) {
        std::cout << name2 << " is older by a year." << "\n";
    }
//main comparison
    else if (age1 > age2) {
        std::cout << name1 << " is older by " << age1 - age2 << " years." << "\n";
    }
    else if (age1 < age2) {
        std::cout << name2 << " is older by " << age2 - age1 << " years." << "\n";
    }

}
Last edited on
Stream std::cin can read data only when it is in a good state. If an error occured (you entered a letter instead of a number) the state of the stream is bad and it read nothing until you will clear the state.
Last edited on
So I need to either implement some kind of error check or simply limit the usable input characters to numbers only, right?
You should check the state of the stream after each reading.
Ok, thx. Will keep that in mind.
Topic archived. No new replies allowed.