Create error output when typing in a non-interger

So I've seen a number of the same question on here, but I've made my own code and having trouble getting an output if anything other than an integer is typed in.

It works completely fine for the most part, but everything I can get working, I keep getting the "Test scores cannot be higher than 50, try again: ".

#include "stdafx.h"
#include <iostream>
#include<iostream>
#include <iomanip>
using namespace std;


int main()
{

//variables
int grade1, grade2, grade3;
int max;
int error;

int ch;
char ch;

//input from the user
cout << "Welcome to the grade calculator. You will input three test scores." << endl;
cout << "The highest of the first two grades and the third grade will be" << endl;
cout << "added together to determine the numeric grade average for the" << endl;
cout << "course. Each test score has a maximum of 50 points" << endl << endl;

//prompt the user for input
//enter first grade
cout << "Please enter test score 1: ";
cin >> grade1;

if (!cin || grade1 < 0)
{
cout << "Test scores cannot be negative, try again: ";
cin.clear(); //reset for a good read
fseek(stdin, 0, SEEK_END); //flush the buffer
cin >> grade1;
}
else if (!cin || grade1 > 50)
{
cout << "Test scores cannot be higher than 50, try again: ";
cin.clear(); //reset for a good read
fseek(stdin, 0, SEEK_END); //flush the buffer
cin >> grade1;
}
else if (!cin || ch 'A' >= && <= 'Z')
{
cout << "Invalid input, try again: ";
cin.clear(); //reset for a good read
fseek(stdin, 0, SEEK_END); //flush the buffer
cin >> grade1;
}


//enter second grade
cout << "Please enter test score 2: ";
cin >> grade2;

if (!cin || grade2 < 0)
{
cout << "Test scores cannot be higher than 50, try again: ";
cin.clear(); //reset for a good read
fseek(stdin, 0, SEEK_END); //flush the buffer
cin >> grade2;
}
else if (!cin || grade2 > 50)
{
cout << "Invalid input, try again: ";
cin.clear(); //reset for a good read
fseek(stdin, 0, SEEK_END); //flush the buffer
cin >> grade2;
}


//enter third grade
cout << "Please enter test score 3: ";
cin >> grade3;

if (!cin || grade3 < 0)
{
cout << "Test scores cannot be negative, try again: ";
cin.clear(); //reset for a good read
fseek(stdin, 0, SEEK_END); //flush the buffer
cin >> grade3;
}
else if (!cin || grade3 > 50)
{
cout << "Test scores cannot be higher than 50, try again: ";
cin.clear(); //reset for a good read
fseek(stdin, 0, SEEK_END); //flush the buffer
cin >> grade3;
}

cout << endl;

//decide which grade is higher
if (grade1 > grade2)
max = grade1;
else
max = grade2;

//average grade
cout << "The average for the course is = " << max + grade3 << endl;

//grade letter
if (max + grade3 >= 90)
cout << "The grade letter = A";
else if (max + grade3 >= 80)
cout << "The grade letter = B";
else if (max + grade3 >= 70)
cout << "The grade letter = C";
else if (max + grade3 >= 60)
cout << "The grade letter = D";
else if (max + grade3 < 60)
cout << "The grade letter = F";

cout << endl << endl;
cout << "Thank you for using this program" << endl;


return 0;
}

Topic archived. No new replies allowed.