Output shows nan

I have been trying to find a solution for this for hours and I can't find any. The output always shows nan when I calculate the averages. Does anyone know why this is? Am I missing something? Sorry I'm a noob at this.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
  This program get 5 scores and drop the lowest one, then calculate
  and display the average
*/

#include <iomanip>
#include <cstdlib>
#include <iostream>

using namespace std;

//function prototypes
double getScore();   //score has to be 0 - 100
double findLowest(double, double, double, double, double);
double calcAverage(double, double, double, double, double);
void display(double);

int main(int argc, char *argv[])
{
    //variable declaration
    double s1, s2, s3, s4, s5, avg;
    
    //call getScore 5 times
    s1 = getScore();
    s2 = getScore();
    s3 = getScore();
    s4 = getScore();
    s5 = getScore();
    
    //call the calcAverage function
    avg = calcAverage(s1, s2, s3, s4, s5);
    
    //call display function
    display(avg);
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

/*
  getScore function: this function displays a prompt and get a score from the keyboard
  input validation is needed to make sure the score is 0-100. The function returns the score
*/

double getScore()
{
       double score = 0.0;
       cout << "Enter your test score: " << endl;
       cin >> score;
       
       while (score < 0 || score > 100)
{
        cout << "Error: Test score must be in the 0 to 100 range." << endl;
        cin >> score;
}
}

/*
  findLowest: this function returns the lowest score among the five scores
*/
double findLowest(double a, double b, double c, double d, double f)
{
      if (a < c && a < c && a < d && a < f)
		return a;
	else if (b < a && b < c && b < d && b < f)
		return b;
	else if (c < b && c < a && c < d && c < f)
		return c;
	else if (d < b && d < c && d < a && d < f)
		return d;
	else
		return f;	
       
}

/*
  calcAverage: this function calculates and returns the average after dropping the lowest
*/
double calcAverage(double a, double b, double c, double d, double f)
{
    int lowest;
    double avg;
    
       //call the findLowest function to get the lowest
       
       lowest = findLowest(a, b, c, d, f);
       
       
       //calculate the average
       
       if (lowest == a)
		avg = (b + c + d + f)/4;
	else if(lowest == b)
		avg = (a + c + d + f)/4;
	else if(lowest == c)
		avg = (b + a + d + f)/4;
	else if(lowest == d)
		avg = (b + c + a + f)/4;
	else
		avg = (b + c + d + a)/4;
       
       //return the average
       
       return avg;
             
}


void display(double avg)
{
     //display the avrage 
     cout << "The average of the four highest scores is: " << setprecision(2) << fixed << avg << endl;
		 
          
}



Also want to add that when I try that code on

http://cpp.sh/

it worked, but when I tried running it on codeblocks the output displays "nan" when I average the four highest scores.
Last edited on
I have been trying to find a solution for this for hours

Reading the compiler warnings would have saved you hours :-)

double getScore() doesn't return anything.

I didn't receive any compiler warnings. I still don't see what I'm doing wrong. Sorry, I'm a noob, but do I need to add this

return score;

?
but do I need to add this
return score;
?

Exactly!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
double getScore()
{
   double score = 0.0;
   cout << "Enter your test score: " << endl;
   cin >> score;

   while (score < 0 || score > 100)
   {
      cout << "Error: Test score must be in the 0 to 100 range." << endl;
      cin >> score;
   }

   return score;
}


That instruction tells the function to send back the value contained in "score" to the caller, i.e. the lines
1
2
3
4
5
6
//call getScore 5 times
s1 = getScore();
s2 = getScore();
s3 = getScore();
s4 = getScore();
s5 = getScore();

So it can 'enter' in the variables s1, s2, s3...

I'm as noob as you, this is the beginners forum, I was just joking :-)
Every compiler can be instructed to give more or less warnings - they are annoying, but also useful.
For example, gcc accepts parameters like -Wall, -Wextra, -pedantic-errors... That's up to you to turn it on or off, of course, it's a personal preference.
It worked thanks man!
Topic archived. No new replies allowed.