My code dose something really basic. All it dose is that it ask you how many grades you have to enter. Then you enter them grades and it give you the sum and the average. now i put a if statement to make sure the user can not enter in a number lower or equal to 0. but it doesn't work ? The program runs but i enter a number lower then 0 and i just goes into an infinite loop? can any one led me in the right direction. Thanks
Code:#include <iostream> int main ( ) { unsigned short int numberOfGrades; double grades; double sum = 0; std::cout << "Enter number of grades you are entering: "; std::cin >> numberOfGrades; std::cin.ignore(); if ( numberOfGrades <= 0 ) { std::cerr << "ERROR \n\n"; return EXIT_FAILURE; } for ( int i = 0; i < numberOfGrades; i++ ) { std::cout << "> "; std::cin >> grades; std::cin.ignore(); sum = sum + grades; } float Average = sum / numberOfGrades; std::cout << std::endl << std::endl; std::cout << "The sum is : " << sum << std::endl; std::cout << "The average is: " << Average << std::endl; std::cin.get(); return ( 0 ); }