I have been writing my program and everything works like it should unless the user inserts a character then the whole thing blows up... how can I fix this.
Is there something I can add to block characters from being added? any help would be appreciated!
here is my code:
#include <iostream>
using namespace std;
int main()
{
// Declaring Variables
int numscores = 0;
double average = 0.0;
double total = 0.0;
errorcheck1:
// Ask for user input: How many tests to average
cout << "Please enter how many test scores you would like to average: ";
cin >> numscores;
// Check to make sure number of scores is greater then 0 but less then 101
if (numscores <= 0 || numscores > 100)
{
cout << "You must enter a number greater then 0 but less then 101!" << endl;
cout << endl;
goto errorcheck1;
}
// Getting the values for the number of tests
// Start of loop
double scores[100] = {0};
for (int x = 1; x <= numscores; x++)
{
cout << "Enter test score #" << x << ": ";
cin >> scores[x];
if (scores[x] < 0 || scores[x] > 100)
{
cout << "==============================================" << endl;
cout << "Error: Please enter a value between 0 and 100. " << endl;
cout << "==============================================" << endl;
x = x - 1;
}
} // End of loop
// This is the output section
cout << "=======================================" <<endl;
cout << "Total number of test scores: " << numscores << endl;
// Add all of the scores and call it total
for (int y = 0; y <= numscores; y++)
{
total = total + scores[y];
}
// End loop
// Below I will average the scores then display them
average = total / numscores;
cout << "Average of all test scores: " << average << endl;
//End
// Needed for some compilers like mine
return 0;
}