need a little help figuring out what's wrong

I have an assignment where I'm supposed to figure out grade information for multiple sections. I haven't even gotten through most of it, because I keep getting stuck on the score. For some reason I keep getting weird numbers when I enter the scores. The first number entered is supposed to be how many scores are in the section, and then followed by the scores. The only thing that is coming up correctly is the low score. The grade counts, and the highest score are getting really weird numbers. Can anyone tell me why this might be?

Thanks!

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

int main ()
{
int score, highscore, lowscore, sectionsum;
int sectionNum = 1;
int aCount, bCount, cCount, dCount, fCount;
int sectionAverage;
int scoresInSection;
int count = 0;

cin >> scoresInSection;

while (cin)
{
cin >> score;

while (count < scoresInSection)
{
if (score >= 90)
 aCount++;
else if (score >= 80)
 bCount++;
else if (score >= 70)
 cCount++;
else if (score >= 60)
 dCount++;
else
 fCount++;

if (score > highscore)
 highscore = score;
if (score < lowscore)
 lowscore = score;

count++;

sectionsum = sectionsum + score;
}

cout << "A's:  " << aCount << endl;
cout << "B's:  " << bCount << endl;
cout << "C's:  " << cCount << endl;
cout << "D's:  " << dCount << endl;
cout << "F's:  " << fCount << endl;

sectionNum++;

cin >> scoresInSection;
}

cout << "Lowest Score:  " << lowscore << endl;
cout << "Highest Score:  " << highscore << endl;

return 0;
}
You need more indentation in your code to make it easier to read. Anyway, the problem is that highscore is not initialized before you use it for comparison. Also, you don't set sectionsum or any of the counters to zero before you start incrementing them.
What is the starting value os scores/high/low scores etc?
You need to initialize your variables. If you increment an uninitialized variable you will get values you didn't expect. Same goes for highscore.
Thank you all for the quick replies!

And I apologize about the indentation. I will work on that in the future.

I just changed the following:

1
2
int highscore = 0;
int count;


But the only thing that changed in my output is that highscore is now showing the same thing as low score. For instance, when I type 2 80 90. I get the following output:
A's: 0
B's: 4196466
C's: 0
D's: 1810270640
F's: 32767

And then when I hit Ctrl D, the same output occurs (I would like this to stop happening, and only show the scores once.. also with the correct numbers).

And I get:
Lowest Score: 80
Highest Score: 80

Any other suggestions/hints as to why this occurring?

Same thing.
aCount++; What is the value of aCount before that line is executed first time?
Thanks, MiiNiPaa!

That fixed the Lowest Score and Highest Score problem.

The program still isn't correctly incrementing the xCounts though.

For instance, if I input: 3 50 70 90

I get the output:

A's: 0
B's: 0
C's: 0
D's: 0
F's: 3
A's: 67
B's: 0
C's: 0
D's: 0
F's: 3

It seems like it is skipping all the else if statements, and going directly to the else of fCount++

I have no idea how it's getting that number in the second aCount, or why a second aCount through fCount keeps appearing.

Any thoughts on this?
It seems like it is skipping all the else if statements, and going directly to the else of fCount++
Score of 50 is f judging from your code so first iteration is correct. About second: what is the value of count after first set of data is finished calculating? And what is it when second set is just began?

Actually there is no need for loop and count. Just add scoresInSection to *Count

Edit: Sligth rework of your program using C++ facilities:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <algorithm>
#include <iostream>
#include <map>

int main ()
{
    int highscore = 0;
    int lowscore  = 1000;
    std::map<char, int> grades;
    int scoresInSection, score;
    while (std::cin >> scoresInSection >> score) {
             if (score >= 90) grades['A'] += scoresInSection;
        else if (score >= 80) grades['B'] += scoresInSection;
        else if (score >= 70) grades['C'] += scoresInSection;
        else if (score >= 60) grades['D'] += scoresInSection;
        else                  grades['F'] += scoresInSection;
        highscore = std::max(score, highscore);
        lowscore  = std::min(score, lowscore );
        for(const auto& p: grades)
            std::cout << p.first << "'s: " << p.second << '\n';
    }
    std::cout << "Lowest Score:  " << lowscore  << '\n';
    std::cout << "Highest Score: " << highscore << '\n';
}
1 6
F's: 1
4 60
D's: 4
F's: 1
2 88
B's: 2
D's: 4
F's: 1
1 111
A's: 1
B's: 2
D's: 4
F's: 1
7 73
A's: 1
B's: 2
C's: 7
D's: 4
F's: 1
^Z
Lowest Score:  6
Highest Score: 111
Last edited on
The count is correctly displaying after the first set, but is showing a value of "80" after the second set.

For instance, an input of 3 90 80 70
is showing an output of
count: 3
count: 80

Isn't the loop necessary if I want to do multiple sections?

Also, how do I tell the program to stop incrementing the xCount of whatever the first value is? Should I do separate if statements, instead of the else if statements?
And 80 - 3 is how much? I bet you saw this in C's.

how do I tell the program to stop incrementing the xCount of whatever the first value is?
I do not quite follow. In your previous example value of F's is not increased on second iteration at all.
This is a lot to digest. I need some time to go through all of this.

Thanks for your help.

To clarify what I meant, with my code it increments the xCount of whatever the first value is.

For instance, 3 40 80 90 will show F's: 3
3 40 80 90 will show F's: 3
There are two passes here. On first one count starts from zero and increments until is is 3. On each iteration fCount is incremented. In the end fCount is incremented 3 - 0 = 3 times.

On second pass count starts from 3 (left from previous pass) and increments untill it will become 80. On each iteration bCount is incremented. In the end bCount is incremented 80 - 3 = 77 times.

So your program will report 77 A's and 3 F's after those two passes.

You can allso try to switch your two sets of variables around and see what happens.
Forgive me, but I'm still very confused. I'm a complete beginner.

What do you mean by changing the two sets of variables around? Why are there two passes?

I've been trying to study your code, but it is a little over my head, and not exactly what I'm trying to accomplish.

The first input has to be how many grades will be in that section, the scoresInSection variable. Then the input of that amount of grades before showing the letter grades.

I see now what my loop is doing in looping that amount of times on one letter grade, but still don't understand the second loop, or how to stop doing that without a while(cin) loop.
I think I figured it out.

I put the cin >> score; in the wrong loop.

And then I needed to "0" out all of the scores.

I'll post the code I came up with in a minute, as I don't know how to copy and paste, so I have to manually enter it.
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
#include <iostream>

using namespace std;

int main()
{
  int highscore = 0;
  int lowscore = 100;
  int scoresInSection, score, count;
  int sectionNum = 0;
  int aCount = 0;
  int bCount = 0;
  int cCount = 0;
  int dCount = 0;
  int fCount = 0;
  int sectionSum = 0;
  int sectionAverage = 0;

  cin >> scoresInSection;

while (cin)
{

  while (count < scoresInSection)
  {
    cin >> score;

    if (score >= 90)
      aCount++;
    else if (score >= 80)
      bCount++;
    else if (score >= 70)
      cCount++;
    else if (score >= 60)
      dCount++;
    else
      fCount++;

    if (score > highscore)
      highscore = score;
    if (score < lowscore)
      lowscore = score;

    count++;

    sectionSum = sectionSum + score;
  }

  sectionAverage = sectionSum / scoresInSection;

  sectionNum++

  cout << "Scores for section " << sectionNum << endl;

  cout << "A's:  " << aCount << endl;
  cout << "B's:  " << bCount << endl;
  cout << "C's:  " << cCount << endl;
  cout << "D's:  " << dCount << endl;
  cout << "F's:  " << fCount << endl;

  cout << "Lowest Score:  " << lowscore << endl;
  cout << "Highest Score:  " << highscore << endl;
  cout << "Average Score:  " << sectionAverage << endl;

  aCount = 0;
  bCount = 0;
  cCount = 0;
  dCount = 0;
  fCount = 0;
  count = 0;
  scoresInSection = 0;
  lowscore = 100;
  highscore = 0;
  sectionAverage = 0;
  sectionSum = 0;

  cin >> scoresInSection;
}

return 0;
}
Topic archived. No new replies allowed.