returning lowest score from a function

Discussion in 'Software' started by someguy85, Mar 31, 2009.

  1. someguy85

    someguy85 Private E-2

    I'm suppose to write a program that uses functions to do the work. I have it all written out and everything's working except the float findLowest function. I ran debugger and found that it is always returning the first number (score1). I can't figure out what the problem is, and we aren't starting arrays for another week or two so I don't know any other way to do this function. Any help is greatly appreciated. The code is below:

    Code:
    #include <iostream>
    using namespace std;
    
    void getScore(float &);
    float calcAverage(float, float, float, float, float);
    float findLowest(float, float, float, float, float);
    // float value1, value2, value3, value4, value5;
    
    int main()
    {
       float value1, value2, value3, value4, value5;
       cout << "This program will calculate the average of a group of test scores,\n";
       cout << "with the lowest score dropped. \n\n";
    
          
       for (int numScores = 1; numScores <= 5; numScores++)
       {
          float value = 0.0;
          getScore(value);         
    
          if (numScores == 1)
             value1 = value;
          if (numScores == 2)
             value2 = value;
          if (numScores == 3)
             value3 = value;
          if (numScores == 4)
             value4 = value;
          if (numScores == 5)
             value5 = value;
       }
       
       
       float avg = calcAverage(value1, value2, value3, value4, value5);
    
       cout << "The average test score is " << avg << endl;
    
    
    
       return 0;
    }
    
    
    
    
    
    
    void getScore(float &score)
       {
          cout << "Please enter a test score. ";
          cin >> score;
    
          while (score < 0.0 || score > 100.0)
          {
             cout << "Score must be between 0 and 100. Please enter a score again. ";
             cin >> score;
          }
          
       }
    
    
    float calcAverage(float score1, float score2, float score3, float score4, float score5)
    {
    
    
       float lowest = findLowest(score1, score2, score3, score4, score5);
    
       float average = (score1 + score2 + score3 + score4 + score5 - lowest) / 4;
       
       return average;
    
    
    }
    
    
    
    float findLowest(float score1, float score2, float score3, float score4, float score5)
    {
       float lowest = score1;
    
       if (score2 < lowest)
          score2 = lowest;
       if (score3 < lowest)
          score3 = lowest;
       if (score4 < lowest)
          score4 = lowest;
       if (score5 < lowest)
          score5 = lowest;
       return lowest;
    }
    
    Thanks.
     
  2. MutD

    MutD Specialist

    Switch assignemnts around.

    Code:
    float findLowest(float score1, float score2, float score3, float score4, float score5)
    {
       float lowest = score1;
    
       if (score2 < lowest)
          lowest = score2;
       if (score3 < lowest)
          lowest = score3;
       if (score4 < lowest)
          lowest = score4;
       if (score5 < lowest)
          lowest = score5;
       return lowest;
    }
     
  3. someguy85

    someguy85 Private E-2

    Thanks. I can't believe I didn't catch that when debugging.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds