wont calculate # scores above average

ok. im pretty much done with this small program my teacher asked us to write.

my problem is:
it wont give me how many scores there are above average.
it keeps giving me 0. i dont get it.

maybe im overlooking something?
i think something is wrong in my [color=#33FF33]// count#of scores > average[/color] area. if someone can take a look at it and kinda point it out to me, itd be awesome. thank u!

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
#include <iostream>
using namespace std;

struct Score
{
  int value;
  Score* next;
};


int main()
{    
  
  // create an empty linked list
  Score* head = 0;
  
  // prompt for how many students
  int nScores;
  cout << "How many scores would you like to view? ";
  cin >> nScores;
  cin.ignore(1000, 10);
  cout << " " << endl;
  
  for (int count = 0; count < nScores; count++)
  {
  //create a node 
  Score* aScore = new Score;
  cout << "Enter score [-1 to quit]: ";
  cin >> aScore->value;
  cin.ignore(1000, 10);
  
  // check for sentinel value
  if (aScore->value == -1) break;
  
  // add node to list (stack model)
  aScore->next = head;
  head = aScore;
  }
  
  // traverse list
  int sum = 0;
  int count = 0;
  Score* p;
  for (p = head; p; p = p->next)
  {
     sum += p->value;
     count++;  
  }
  
  // print results
  if (count > 0)
  { 
    float average = float(sum) / count;
    cout << "The average of " << count << " scores is " << average << endl;
  }
  else 
    cout << "No values were entered." << endl;
  
  
  // count#of scores > average
  int nGreater = 0;
  float average;
  for (count = 0; count > 0; count++)
  {
    if (nScores > average) nGreater++;
  }
  cout << nGreater << " scores are greater than average." << endl;
  
  cin.ignore();
  cin.get();
  return 0;
}
you're initialising count to zero then saying "while count is greater than zero", which it never is because you're starting at zero...
Plus, you declare average as a float and never use it.

Have you tried compiling with warnings on? It would be able to point that out for you.
Last edited on
Actually, you're declaring average twice... (lines 53 and 62)

For your count#of scores > average part, wouldn't you be looking for something along the lines of

1
2
3
for ( int i=0; i < count; i++){
  if ( whateverTheValueIs > average ) nGreater++;
}
Topic archived. No new replies allowed.