Ok so my assignment is as follows:
Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions:
void getScore() should ask the user for a test score, store it in a refrence parameter variable, and validate it. This function should be called by main once for each of the five scores to be entered.
void calcAverage() should calculate and display the average of the four highest scores. This function should be called just once by main, and should be passed the five scores.
int findLowest() should find and return the lowest of the five scores passed to it. It should be called by calcAverage, which uses the function to determine which of the five scores to drop.
Input Validation: Do not accept test scores lower than 0 or higher than 100.

So below is my program, but when I run it the following message occurs after the lowest score is found:
DeBug Error!
Run-Time check failure #3- The variable "average" is being used without being initialized.

I understand that this means I have an issue with my average line, but I'm unsure as to how to resolve this issue.
Any guidance would be GREAT.

//Lowest Score Drop

#include <iostream>
using namespace std;

void intro();
void getScore(float&);
void calcAverage(float, float, float, float, float);
int findLowest(float, float, float, float, float);

int main()
{
    float score1,
           score2,
           score3,
           score4,
           score5;

    intro();
    getScore(score1);
    getScore(score2);
    getScore(score3);
    getScore(score4);
    getScore(score5);
    calcAverage(score1, score2, score3, score4, score5);

    system("pause");
    return 0;
}

void intro()
{
     cout << "This program calculates the average of a group of test scores\n";
     cout << "where the lowest score in the group is dropped.\n\n";
}

void getScore(float &score)
{    
     score;

              cout << "Please enter a test score. ";
              cin >> score;

     while (score < 0 || score > 100)
         {  
              cout << "Invalid entry! Cannot accept test scores lower than 0 or higher than 100.\n";
              cout << "Please re-enter a test score. ";
              cin >> score;
         }  
}

void calcAverage(float grd1, float grd2, float grd3, float grd4, float grd5)
{    
     float lowest, average;

     lowest = findLowest(grd1, grd2, grd3, grd4, grd5);

     cout << lowest << endl << endl; 

     if (lowest == 1)
     {
          average = (grd2 + grd3 + grd4+ grd5)/4;
          cout << "Four highest test scores: " << fixed << grd2 << ", " << grd3 << ", " << grd4 << ", " << grd5 << endl;
     }
     else if (lowest == 2)
     {
          average = (grd1 + grd3 + grd4+ grd5)/4;
          cout << "Four highest test scores: " << fixed << grd1 << ", " << grd3 << ", " << grd4 << ", " << grd5 << endl;
     }
     else if (lowest == 3)
     {
          average = (grd1 + grd2 + grd4+ grd5)/4;
          cout << "Four highest test scores: " << fixed << grd1 << ", " << grd2 << ", " << grd4 << ", " << grd5 << endl;
     }
     else if (lowest == 4)
     {
          average = (grd1 + grd2 + grd3+ grd5)/4;
          cout << "Four highest test scores: " << fixed << grd1 << ", " << grd2 << ", " << grd3 << ", " << grd5 << endl;
     }
     else if (lowest == 5)
     {
          average = (grd1 + grd2 + grd3+ grd4)/4;
          cout << "Four highest test scores: " << fixed << grd1 << ", " << grd2 << ", " << grd3 << ", " << grd4 << endl;
     }

     cout << "Average: " << fixed << average << endl;
}


int findLowest(float score1, float score2, float score3, float score4, float score5)
{
   float lowest = score1;
   int index = 1;
   cout<<"The lowest of the five scores is:\n"<<endl;

      if (lowest > score2)
      {
           lowest = score2;
           index = score2;

      }
      if (lowest > score3)
      {
           lowest = score3;
           index = score3;
      }
      if (lowest > score4)
      {
           lowest = score4;
           index = score4;
      }
      if (lowest > score5)
      {
           lowest = score5;
           index = score5;
      }

  return index;
}

Recommended Answers

All 4 Replies

Please use code tags when posting code.

As far as the compiler is concerned, it is possible that you get to the cout statement cout << "Average: " << fixed << average << endl; without first setting the 'average' variable. That is, you need to either initialize it when you create it:

double average = 0;

or add an 'else' to your if/else if block.

David

Please use code tags when posting code.

As far as the compiler is concerned, it is possible that you get to the cout statement cout << "Average: " << fixed << average << endl; without first setting the 'average' variable. That is, you need to either initialize it when you create it:

double average = 0;

or add an 'else' to your if/else if block.

David

Thanks so much for your response daviddoria. (Are the code tags visible in this post?)So I did as you suggested and tried the program again, so now the program completes, but now instead of displaying the score of the lowest grade that was dropped, it displays the number where the lowest test grade was entered. For example, if I entered the lowest score the second time out of the five times I was asked to enter a score, it will display the number two.

//Lowest Score Drop

#include <iostream>
using namespace std;

double average;
void intro();
void getScore(float&);
void calcAverage(float, float, float, float, float);
int findLowest(float, float, float, float, float);

int main()
{
    float score1,
           score2,
           score3,
           score4,
           score5;
        
    intro();
    getScore(score1);
    getScore(score2);
    getScore(score3);
    getScore(score4);
    getScore(score5);
    calcAverage(score1, score2, score3, score4, score5);
    
    system("pause");
    return 0;
}

void intro()
{
     cout << "This program calculates the average of a group of test scores\n";
     cout << "where the lowest score in the group is dropped.\n\n";
}

void getScore(float &score)
{    
     score;
     
              cout << "Please enter a test score. ";
              cin >> score;
              
     while (score < 0 || score > 100)
         {  
              cout << "Invalid entry! Cannot accept test scores lower than 0 or higher than 100.\n";
              cout << "Please re-enter a test score. ";
              cin >> score;
         }  
}

void calcAverage(float grd1, float grd2, float grd3, float grd4, float grd5)
{    
     float lowest, average;
     
     lowest = findLowest(grd1, grd2, grd3, grd4, grd5);
     
     cout << lowest << endl << endl; 
     
     if (lowest == 1)
     {
          average = (grd2 + grd3 + grd4+ grd5)/4;
          cout << "Four highest test scores: " << fixed << grd2 << ", " << grd3 << ", " << grd4 << ", " << grd5 << endl;
     }
     else if (lowest == 2)
     {
          average = (grd1 + grd3 + grd4+ grd5)/4;
          cout << "Four highest test scores: " << fixed << grd1 << ", " << grd3 << ", " << grd4 << ", " << grd5 << endl;
     }
     else if (lowest == 3)
     {
          average = (grd1 + grd2 + grd4+ grd5)/4;
          cout << "Four highest test scores: " << fixed << grd1 << ", " << grd2 << ", " << grd4 << ", " << grd5 << endl;
     }
     else if (lowest == 4)
     {
          average = (grd1 + grd2 + grd3+ grd5)/4;
          cout << "Four highest test scores: " << fixed << grd1 << ", " << grd2 << ", " << grd3 << ", " << grd5 << endl;
     }
     else if (lowest == 5)
     {
          average = (grd1 + grd2 + grd3+ grd4)/4;
          cout << "Four highest test scores: " << fixed << grd1 << ", " << grd2 << ", " << grd3 << ", " << grd4 << endl;
     }
     
     cout << "Average: " << fixed << average << endl;
}


int findLowest(float score1, float score2, float score3, float score4, float score5)
{
   float lowest = score1;
   int index = 1;
   cout<<"The lowest of the five scores is:\n"<<endl;
    
      if (lowest > score2)
      {
           lowest = score2;
           index = score2;
		   
      }
      if (lowest > score3)
      {
           lowest = score3;
           index = score3;
      }
      if (lowest > score4)
      {
           lowest = score4;
           index = score4;
      }
      if (lowest > score5)
      {
           lowest = score5;
           index = score5;
      }

  return index;
}

Please use code tags when posting code.

As far as the compiler is concerned, it is possible that you get to the cout statement cout << "Average: " << fixed << average << endl; without first setting the 'average' variable. That is, you need to either initialize it when you create it:

double average = 0;

or add an 'else' to your if/else if block.

David

Sorry for the repost, didn't look as if my previous turned out correctly as far as code tags.

//Lowest Score Drop

#include <iostream>
using namespace std;

double average=0;
void intro();
void getScore(float&);
void calcAverage(float, float, float, float, float);
int findLowest(float, float, float, float, float);

int main()
{
    float score1,
           score2,
           score3,
           score4,
           score5;
        
    intro();
    getScore(score1);
    getScore(score2);
    getScore(score3);
    getScore(score4);
    getScore(score5);
    calcAverage(score1, score2, score3, score4, score5);
    
    system("pause");
    return 0;
}

void intro()
{
     cout << "This program calculates the average of a group of test scores\n";
     cout << "where the lowest score in the group is dropped.\n\n";
}

void getScore(float &score)
{    
     score;
     
              cout << "Please enter a test score. ";
              cin >> score;
              
     while (score < 0 || score > 100)
         {  
              cout << "Invalid entry! Cannot accept test scores lower than 0 or higher than 100.\n";
              cout << "Please re-enter a test score. ";
              cin >> score;
         }  
}

void calcAverage(float grd1, float grd2, float grd3, float grd4, float grd5)
{    
     float lowest, average;
     
     lowest = findLowest(grd1, grd2, grd3, grd4, grd5);
     
     cout << lowest << endl << endl; 
     
     if (lowest == 1)
     {
          average = (grd2 + grd3 + grd4+ grd5)/4;
          cout << "Four highest test scores: " << fixed << grd2 << ", " << grd3 << ", " << grd4 << ", " << grd5 << endl;
     }
     else if (lowest == 2)
     {
          average = (grd1 + grd3 + grd4+ grd5)/4;
          cout << "Four highest test scores: " << fixed << grd1 << ", " << grd3 << ", " << grd4 << ", " << grd5 << endl;
     }
     else if (lowest == 3)
     {
          average = (grd1 + grd2 + grd4+ grd5)/4;
          cout << "Four highest test scores: " << fixed << grd1 << ", " << grd2 << ", " << grd4 << ", " << grd5 << endl;
     }
     else if (lowest == 4)
     {
          average = (grd1 + grd2 + grd3+ grd5)/4;
          cout << "Four highest test scores: " << fixed << grd1 << ", " << grd2 << ", " << grd3 << ", " << grd5 << endl;
     }
     else if (lowest == 5)
     {
          average = (grd1 + grd2 + grd3+ grd4)/4;
          cout << "Four highest test scores: " << fixed << grd1 << ", " << grd2 << ", " << grd3 << ", " << grd4 << endl;
     }
     
     cout << "Average: " << fixed << average << endl;
}


int findLowest(float score1, float score2, float score3, float score4, float score5)
{
   float lowest = score1;
   int index = 1;
   cout<<"The lowest of the five scores is:\n"<<endl;
    
      if (lowest > score2)
      {
           lowest = score2;
           index = score2;
		   
      }
      if (lowest > score3)
      {
           lowest = score3;
           index = score3;
      }
      if (lowest > score4)
      {
           lowest = score4;
           index = score4;
      }
      if (lowest > score5)
      {
           lowest = score5;
           index = score5;
      }

  return index;
}

Your code tags are better than nothing, but since the line numbers didn't show up, I don't think you did it exactly right:

[ code ]
your code here
[ /code ]

(except don't put spaces between the word 'code' and the brackets).

I suggest hard coding some simple data to test your program. You always want to remove the user input so 1) you don't have to type it in each time to test the code, 2) you are sure there are no errors obtaining the data and 3) so WE don't have to type the input to try to reproduce your problem :)

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.