Why is the code breaking?

I am trying to combine my code and my group partner's code to turn in, but when I type anything that's not a integer it breaks. I think this code is what breaks it:

1
2
3
4
	cout << "Press <enter> to continue ";
	cin.ignore();
	cin.get();
	system("cls");


Can someone help me understand why this causes the code to break? Here is the full code:

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
using namespace std;

int main() {
	int grade_score;
	
	cout << "*****************************************\n" << "Letter Grade Converter" << endl << endl;
	cout << "Enter numerical grade (0-100 as Interger Value): ";	
	
	/* Determines if the user input is valid 
	   a then calculates the letter grade    */
	if (cin >> grade_score) {
		if (grade_score < 0) {
			cout << "Numerical grade can't be negative.";
		}
		else if (grade_score > 100) {
			cout << "Numerical grade is too large.";
		}
		
		else if (grade_score >= 90) {
			cout << "Letter grade: A";
		}	  
		else if (grade_score >= 80) {
			cout << "Letter grade: B";
		}
		else if (grade_score >= 70) {
			cout << "Letter grade: C";
		}
		else if (grade_score >= 60) {
			cout << "Letter grade: D";
		}
		else {
			cout << "Letter grade: F";
		}
	}
	else {
		cout << "Invalid Entry.";
	}
	
	cout << "\n*****************************************\n";

	//Allow the user to continue, clear the cin buffer, capture the next keystroke and clear the screen
	cout << "Press <enter> to continue ";
	cin.ignore();
	cin.get();
	system("cls");
	
	//assign input variables for month and year
	int month;
	int year;

	//assign and itialize boolean to test user input for month and year
	bool validation = false;

	//assign empty text string to hold month value
	string month_name = "";

	//assign and itialize boolean to false to check for leap year
	bool leap_year = false;

	//assign variable to hold the number of days, initialize to 0
	int days = 0;

	while (!validation) {
		//create program header
		cout << "*****************************************\nMonth Selector" << endl;

		//get input from user for month and year
		cout << "Enter year (> 0): ";
		cin >> year;
		cout << "Enter month (1 - 12): ";
		cin >> month;

		//check user input to make sure both month and year entries are valid
		if (year < 0 || month <= 0 || month > 12) {
			cout << "Enter a valid year and month" << endl;
		}
		else {
			validation = true;
		}

	}

	//check for possible leap year using user's input year
	if (year % 4 == 0 and (year % 400 == 0 or year % 100 != 0)) {
		leap_year = true;
	}

	//assign the month entered by the user to a proper month name
	switch (month) {
	case 1:
		month_name = "January";
		days = 31;
		break;
	case 2:
		month_name = "February";
		if (leap_year) {
			days = 29;
		}
		else {
			days = 28;
		}
		break;
	case 3:
		month_name = "March";
		days = 31;
		break;
	case 4:
		month_name = "April";
		days = 30;
		break;
	case 5:
		month_name = "May";
		days = 31;
		break;
	case 6:
		month_name = "June";
		days = 30;
		break;
	case 7:
		month_name = "July";
		days = 31;
		break;
	case 8:
		month_name = "August";
		days = 31;
		break;
	case 9:
		month_name = "September";
		days = 30;
		break;
	case 10:
		month_name = "October";
		days = 31;
		break;
	case 11:
		month_name = "November";
		days = 30;
		break;
	case 12:
		month_name = "December";
		days = 31;
		break;
	}

	cout << "Month name: " << month_name << endl;
	cout << "Days in Month: " << days << 
		endl << "*****************************************";
		
	return 0;
}
cin.ignore() will wait for input if there's nothing to ignore. Also the stream has to be in a good state. If L37 is executed then the input stream is in a fail state. In which case the stream has to put into a good state by .clear().

Try this:

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
if (cin >> grade_score) {
	cin.ignore();
	if (grade_score < 0) {
		cout << "Numerical grade can't be negative.";
	} else if (grade_score > 100) {
		cout << "Numerical grade is too large.";
	}

	else if (grade_score >= 90) {
		cout << "Letter grade: A";
	} else if (grade_score >= 80) {
		cout << "Letter grade: B";
	} else if (grade_score >= 70) {
		cout << "Letter grade: C";
	} else if (grade_score >= 60) {
		cout << "Letter grade: D";
	} else {
		cout << "Letter grade: F";
	}
} else {
	cout << "Invalid Entry.";
}

cout << "\n*****************************************\n";

//Allow the user to continue, clear the cin buffer, capture the next keystroke and clear the screen
cout << "Press <enter> to continue ";
//cin.ignore();
cin.clear();
cin.get();

Topic archived. No new replies allowed.