Help when checking for invalid inputs

This is an assignment for class. The problem and code are listed below. I've completed the assignment and it functions properly, except when checking for invalid inputs. For example, when I enter "g" for # of Segments flown, the program displays the following:

Enter the number of miles flown: Please run the program again and enter a valid number of segments that is positive.

Is anyone able to assist me with the proper code for when inputting a letter for the number of segments, one of the following occurs:
1. Immediately prompts the error message above without asking for the number of miles flown
2. Allows the number of miles flown to be entered before providing the error prompt

Assignment
When flying frequently on most airlines, a passenger can join a frequent flyer club. Benefits are based on the number of segments and miles that a passenger flies. A segment is a one-way trip regardless of the number of stops on the route. A round trip is two segments. Members are classified as Platinum, Gold or Silver Elite Status when they attain a threshold of segments or miles. If these thresholds convey different status, a member is awarded the higher one. The table below shows the threshold number of segments or miles that a member must have to attain the named Elite Status.

Elite Status Number of Segments Flown Number of Miles Flown

Platinum 120 or more 75000 or more
Gold 80-119 50000-74999
Silver 40-79 25000-49999
Member 0-39 0-24999

Write a C++ program that inputs the number of segments and miles flown. For each segment/miles pair, based on the table above, print only the Elite status of the person with those segments/miles. Check for valid inputs (negative values). If the input is invalid prompt the user to correct it. The error message and the way you want to handle negative values is up to you. Output should look similar to below.

Sample output:
Enter the number of segments flown: 17
Enter the number of miles flown: 24342

Elite status: Member



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
#include <iostream>

using namespace std;

int main()
{
	int segments=-1, miles=-1;
	
	cout << "Enter the number of segments flown: ";
	cin >> segments;
	cout << "Enter the number of miles flown: ";
	cin >> miles;
	
	if (segments >= 120)
	{
		if (miles >= 0)
			cout << "Elite status: Platinum\n";
		else
			cout << "Please run the program again and enter a valid number of miles that is positive.";
	}
	else if (segments >= 80 && segments <= 119)
	{
		if (miles >= 75000)
			cout << "Elite Status: Platinum\n";
		else if (miles >= 0 && miles <= 74999)
			cout << "Elite status: Gold\n";
		else 
			cout << "Please run the program again and enter a valid number of miles that is positive.";
	}
	else if (segments >= 40 && segments <= 79)
	{
		if (miles >= 75000)
			cout << "Elite Status: Platinum\n";
		else if (miles >= 50000 && miles <= 74999)
			cout << "Elite status: Gold\n";
		else if (miles >= 0 && miles <= 49999)
			cout << "Elite status: Silver\n";
		else
			cout << "Please run the program again and enter a valid number of miles that is positive.";
	}
	else if (segments >= 0 && segments <= 39)
	{
		if (miles >= 75000)
			cout << "Elite Status: Platinum\n";
		else if (miles >= 50000 && miles <= 74999)
			cout << "Elite status: Gold\n";
		else if (miles >= 25000 && miles <= 49999)
			cout << "Elite status: Silver\n";
		else if (miles >= 0 && miles <= 24999)
			cout << "Elite status: Member\n";
		else
			cout << "Please run the program again and enter a valid number of miles that is positive.";
	}
	else
		cout << "Please run the program again and enter a valid number of segments that is positive." << endl;
	return 0;
}
You have some redundant checks (e.g. on line 30, it is guaranteed that segments will be less than 80).

As for your problem, entering "g" and then trying to read it as an integer puts the stream into an error state, and then all subsequent read attempts automatically fail.

You should read all inputs into a string first, then try to extract the data you want and see if it succeeded. Reading a string will always succeed unless you reach the end of the stream, so you don't have to worry about dealing with error states.
1
2
3
4
5
6
7
8
9
std::istream &tryReadInt(std::istream &from, int &to)
{
    std::string line;
    if(std::getline(from, line))
    {
        return std::istringstream(line) >> to;
    }
    return from;
}
1
2
3
4
5
6
7
8
9
int x;
if(tryReadInt(std::cin, x))
{
    //success
}
else
{
    //failure
}
Pro tip: stringstreams are among the most useful streams in C++.
http://www.cplusplus.com/reference/sstream/
Last edited on
Topic archived. No new replies allowed.