Function not returning average

I cannot find reason why function is not returning average correctly?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>

using namespace std;

double calcAvgScore(int score1, int score2, int score3, int score4, int score5)
{
    float avg = (score1+score2+score3+score4+score5)/5;
    return avg;

}


int main()
{
    string name;
    int score1, score2, score3, score4, score5, avg = 0, quit;
    while (quit != 1)
{
    quit = 0;
         cout << " Enter student name: ";
         cin >> name;

    for (int x=0; x<1; x++)
    {
    cout << " Enter score 1: ";
    cin >> score1;
            if (score1 < 1 || score1 > 10)
         {
             cout << " Re-enter score (between 1 - 10)\n\n"; x -= 1; continue;
         }
    cout << " Enter score 2: ";
    cin >> score2;
            if (score2 < 1 || score2 > 10)
         {
             cout << " Re-enter score (between 1 - 10)\n\n"; x -= 1; continue;
         }
    cout << " Enter score 3: ";
    cin >> score3;
            if (score3 < 1 || score3 > 10)
         {
             cout << " Re-enter score (between 1 - 10)\n\n"; x -= 1; continue;
         }
    cout << " Enter score 4: ";
    cin >> score4;
            if (score4 < 1 || score4 > 10)
         {
             cout << " Re-enter score (between 1 - 10)\n\n"; x -= 1; continue;
         }
    cout << " Enter score 5: ";
    cin >> score5;
            if (score5 < 1 || score5 > 10)
         {
             cout << " Re-enter score (between 1 - 10)\n\n"; x -= 1; continue;
         }

             cout << " Enter 1 to quit or 0 to enter another: ";
             cin >> quit;
}
    cout << " Your avg score is: " << calcAvgScore << endl;
}
   return 0;
}
Line 59: You're not making a function call to calcAvgScore (). You're going to print the address of the function.

Line 23: Why the for loop since the loop will execute only once?

Lines 27,33,39,45,51: You check if the score is valid, and output a message if it's not valid, then proceed on to the next score as if the score was valid.

You have a lot of redundant code that could easily be eliminated with a function call.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>
using namespace std;

double calcAvgScore(int scores[], int num_scores)
{   float sum = 0.0;
    
    for (int i=0; i<num_scores; i++)
        sum += scores[i];        
    return sum / num_scores;

}

//  Does not exit until a valid score is entered
void get_score (int n, int & score)
{   cout << " Enter score " << n << ": ";
    do
    {   cin >> score;
        if (score < 1 || score > 10)
        {   cout << " Re-enter score (between 1 - 10)"; 
        }
    } while (score < 1 || score > 10);
}          

int main()
{   string name;
    int quit = 0;
    int scores[5];    // Use an array
    while (quit != 1)
    {   cout << " Enter student name: ";
        cin >> name;

        for (int i=0; i<5; i++)
            get_score (i, scores[i]);
        cout << " Your avg score is: " << calcAvgScore (scores, 5) << endl;
        cout << " Enter 1 to quit or 0 to enter another: ";
        cin >> quit;
    }             
    return 0;
}
Last edited on
Line 59: This is printing the address of the calcAvgScore function instead of calling it and printing the result. That's one of the big pitfalls of C++ for beginners: it's easy to enter legal syntax that doesn't something completely different from what you expect. The correct syntax is:
cout << " Your avg score is: " << calcAvgScore(score1, score2, score3, score4, score5) << endl;

Line 7. This won't quite give you the right answer. Although you've correctly defined avg as a float instead of an int, the expression on the right side of the '=' is an integer expression. So the program will add up the 5 scores, divide by 5, truncate the result to an integer, convert the integer to a float and assign that to avg.

The easiest way to fix this is to change the 5 to a floating point value. That will force the division to done in floating point, with a floating point result:

float avg = (score1+score2+score3+score4+score5)/5.0;
You need to use double or float instead of int - see example below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <fstream>
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <iostream>

using namespace std;

double calcAvgScore (int score1, int score2, int score3, int score4, int score5)
{
  float avg = (score1 + score2 + score3 + score4 + score5) / 5;
  return avg;

}

double calcAvgScore2 (double score1, double score2, double score3, double score4, double score5)
{
  double avg = (score1 + score2 + score3 + score4 + score5) / 5;
  
  return avg;
}

int main ()
{
  double avg = calcAvgScore (3, 2, 1, 2, 3);
  cout << "\nAvg: " << avg; // display 2

  double avg2 = calcAvgScore2 (3, 2, 1, 2, 3);
  cout << "\nAvg: " << avg2; // display 2.2


  system ("pause");
  return 0;
   
}
ok i have undefined contestants to answer the first question about the loop. I kinda get where you say I am not calling calcAvgScore but that is confusing the way you output it, could you write it more readable for the n00b? this is actually just the start I have to do this with highest and lowest got any pointers on this?
3. Use function calcAvgScore that has the contestant’s 5 scores as input parameters
a. returns the average score for that contestant.
b. Calls two functions, findLowest and findHighest which both accept the 5 scores as input parameters and return the lowest and highest scores, respectively.
Ok[i] is a pointer I was trying to figure that out earlier I was trying array lol. wow
I am in over my head
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/194077/2/

Multiple posting of the same problem is bad practice. Choose one and close the other.
Topic archived. No new replies allowed.