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:
#include <iostream>
usingnamespace 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.";
}
elseif (grade_score > 100) {
cout << "Numerical grade is too large.";
}
elseif (grade_score >= 90) {
cout << "Letter grade: A";
}
elseif (grade_score >= 80) {
cout << "Letter grade: B";
}
elseif (grade_score >= 70) {
cout << "Letter grade: C";
}
elseif (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().