Hi I manage to create some code to where it will read in a set of exam scores and display their category as well as if they enter an invalid number it repeats the code until the data is valid or until they enter the SENTINEL value but when I do it crashes after reading in 1 number and it does not repeat the loop if they enter in a invalid number.

const int SENTINEL = -999;

int getData(int score, string category);
string getCategory (int score, int amount, int & numOutstanding, int & numSatisfactory, int & numUnsatisfactory);

int main()
{
    int score = 0;
    int score2;
    int amount = 0;
    int numOutstanding = 0, numSatisfactory = 0, numUnsatisfactory = 0;
    string category;
    category = getCategory (score, amount, numOutstanding, numSatisfactory, numUnsatisfactory);
    score2 = getData(score, category);
    cout << score2 << endl;
    return 0;
}
int getData(int score, string category)
{
    do
    {
        cout << "Enter in exam scores from 0-100, -999 stops the program!" << endl;
        cin >> score;
        cout << "Your score is " << score << "Category: " << category << endl;
    }
    while(score < 0 || score > 100 && score < -999 || score > -999);
    return 0;
}
string getCategory (int score, int amount, int & numOutstanding, int & numSatisfactory, int & numUnsatisfactory)
{
    if(score >= 90 && score <= 100)
    {
        cout << "Outstanding!" << endl;
        numOutstanding++;
    }
    else if(score >= 60 && score <= 89)
    {
        cout << "Satisfactory!" << endl;
        numSatisfactory++;
    }
    else if(score >= 0 && score <= 59)
    {
        cout << "Unsatisfactory!" << endl;
        numUnsatisfactory++;
    }
    amount++;
    cout << "The number of Outstanding scores is: " << numOutstanding << "." << endl;
    cout << "The number of Statisfactory scores is: " << numSatisfactory << "." << endl;
    cout << "The number of Unsatisfactory scores is: " << numUnsatisfactory << "." << endl;
    cout << "The total amount of scores entered is: " << amount << "." << endl;
    return 0;
}

Recommended Answers

All 6 Replies

try making this small adjustment to your 'while' loop condition:

while((score < 0 || score > 100) && (score != -999));

try making this small adjustment to your 'while' loop condition:

while((score < 0 || score > 100) && (score != -999));

thx for your help but I figured it out now I am trying to calculate the average of the scores but it is calculating the SENTINEL value with it how do I make it to where it won't calculate the SENTINEL value?

const int SENTINEL = -999;

void getData(int score_data);

int main()
{
int outstanding_score = 0;
int satisfactory_score = 0;
int unsatisfactory_score = 0;
int score_data = 0;
int average;
int sum = 0;

while(score_data != SENTINEL)
{
	cout << "Enter first score (0-100, -999 stops): " << endl;
	cin >> score_data;
if(score_data >= 90 && score_data <= 100) 
{ 
	cout << "Outstanding!" << endl;
	outstanding_score++; 
}
else if(score_data >= 60 && score_data <= 89)
{ 
	cout << "Satisfactory!" << endl;
	satisfactory_score++;
}
else if(score_data >= 0 && score_data <= 59)
{
	cout << "Unsatisfactory!" << endl;
	unsatisfactory_score++;
}
}
sum = sum + score_data;
average = outstanding_score + satisfactory_score + unsatisfactory_score;
average = sum / average;
cout << "The number of outstanding scores is " << outstanding_score << "." << endl;
cout << "The number of satisfactory scores is " << satisfactory_score << "." << endl;
cout << "The number of unsatisfactory score is " << unsatisfactory_score << "." << endl;
cout << "The average score is " << average << "." << endl;

return 0;
}

void getData(int score_data)
{
	do
	{
			cout << "Enter exam score (1-100, -999 stops): " << endl; 
			cin >> score_data;
	}
		while(score_data < 0 && score_data > SENTINEL || score_data < SENTINEL || score_data > 100);
}

What do you want to happen when the sentinal value is entered?

This is what I would do, but I'm not sure if this is what you want:

if(score == -999)
{
     exit(0);
}

What do you want to happen when the sentinal value is entered?

This is what I would do, but I'm not sure if this is what you want:

if(score == -999)
{
     exit(0);
}

It didn't work what i'm trying to do is when I calculate the average it is just supposed to calculate average, but when I enter -999 in the program it is supposed to end the program and show the results. But when I enter -999 it averages in that -999 in problem so say that I enter 5 for 3 scores ok that's 15 after summing it up then I divide that 15 by 3 it will gimme 5 as the average, but what the program is doing is adding the 5 + 5 + 5 + -999 I want to make it to where the -999 is not calculated at all in the program all I want that -999 to do is end the program and that's it.

Based on your while loop condition, you can assume that score will equal -999 after the loop exits.. at which time you can make the appropriate adjustments.

Based on your while loop condition, you can assume that score will equal -999 after the loop exits.. at which time you can make the appropriate adjustments.

Alright looks like I fixed thanks for your help pretty much all I did was move

sum = sum + score_data;

to be first line in my while statement.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.