Grading Program (Also using arrays)

Now this one is due along with my other topic "Numbers in a file". Me and a classmate have been working on this one together for going on 3 straight hours and we can not figure out what we are doing wrong.


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
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{     
      string name;
      double score[20];
      string student[20];
      int number = 0;
      double grade;
      int count = 0;
      double total = 0;
      string best, worst;
     
      while (grade != -1)
      {
        cout <<"Enter name and grade (or DONE -1 to quit): ";
          student[count] = name;
          score[count] = grade;
          cin >> name >> grade;       
         
         
          if (grade > -1){
              count++;
              total += grade;
          }
      }
      cout << "There were " << count << " grades." << endl;
      cout << "The total was " << total << endl;
      double ave = total / count;
      cout << "The average was " << ave << endl;
     
      cout << "The higest grade was ";
      int big = 0;
      for (int i = 0; i <= count; i++)
      {
          if (big >= grade)
          {
              grade = big;
              cout << big;
             
          }
      }
 
      cout << " and was made by ";
      cout << name <<endl;
     
     
      //cout << "The lowest grade was " << big << " and was made by " << student[4] << endl;
     
      cout << "The following students made above average:";
     for (int i = 0; i <= count; i++)
      {
          if(score[i] > ave)
          {   
            cout << " " << student[i] << " " << score[i];
          } 
      }
     
 }


This code is supposed to output:
Enter name and grade (or DONE -1 to quit): Bob 91.5
Enter name and grade (or DONE -1 to quit): Bill 88
Enter name and grade (or DONE -1 to quit): Tim 75
Enter name and grade (or DONE -1 to quit): Kevin 99
Enter name and grade (or DONE -1 to quit): Sue 50
Enter name and grade (or DONE -1 to quit): Greg 10
Enter name and grade (or DONE -1 to quit): Mark 93
Enter name and grade (or DONE -1 to quit): DONE -1

There were 7 grades
The total was 506.5
The average was 72.3571
The highest grade was 99 and was made by Kevin
The lowest grade was 10 and was made by Greg

The following students made above average: Bob Bill Tim Kevin Mark Mike

The following students made below average: Sue Greg
break

We have been working on one section of code at a time but it just does not want to work for us. If anyone can provide any assistance in this matter, as these assignments are due tonight, it would be greatly appreciated and welcomed. (The break is not part of the output.)
Last edited on
A couple of things I see, might be more.

line 19 - you haven't initialized grade before you first try to compare it here.

line 22/23 - the arrays are getting updated before you get the input for them on line 24


Edit: The for loop at line 41 - you're meaning to search the Score array to find the largest value? Right now, grade is just going to hold the last value entered (-1), big is initialized to 0, so 0 is > -1, so you'll assign 0 to grade, and output 0. And the code will repeat this with big and grade = 0.

Line 52 - name is just going to have the last value entered, which would be DONE.

Line 58 - if an array has COUNT number of elements with data, the index values with data will run from 0 to COUNT-1. Not = COUNT.
Last edited on
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
      int big = 0;                                                  
      for (int i = 0; i <= count; i++)
      {
          if (score[i] > big)
          {   
              big = score[i];
              best = student[i];
          }
      }
     
      cout << "The highest grade was " << big << " and was made by " << best << endl;
     
      int low;                                                  
      for (int i = 0; i < count; i++)
      {
          if (score[i] < low)
          {   
              low = score[i];
              worst = student[i];
          }
      }
      cout << "The lowest grade was " << low << " and was made by " << worst << endl;
     
      cout << "The following students made above average:";
     for (int i = 0; i <= count; i++)
      {
          if(score[i] >= ave)
          {   
            cout << student[i] << " ";
          } 
      }
     
      cout << endl;
      cout << "The following students made below average: ";
     for (int i = 0; i < count; i++)
      {
          if(score[i] <= ave)
          {   
            cout << student[i] << " ";
          } 
      }
     
 }


So we've updated the entire thing and now we have all but two parts correct. They are the "highest" and "lowest" grades. They need to have decimal values and using anything other than (int) results in an incredibly whacky number like 23089e-19349893 or something of the sort. Any ideas on how to change it so that it will output correctly?

(e.g. Enter name and grade : Mike 99.99 - should output this - The highest grade was 99 and was made by Mike.)
You can format the output - check into fixed and setprecision(). setprecision() requires #include <iomanip>

http://www.cplusplus.com/reference/ios/fixed/
http://www.cplusplus.com/reference/iomanip/setprecision/
Last edited on
Formatting the output does not work for some reason. We tried
1
2
3
4
5
cout << fixed << setprecision(4) << big;

and

cout << setprecision(4) << big;


Neither worked to input decimals into the output of the code. It just stayed the same.
int big - you won't see decimal places for an integer.
Low needs to be initialized - that might account for one whacky number.
Last edited on
We fixed everything. There was something wrong with the program we were using to compile. Thank you everyone for your help!
Topic archived. No new replies allowed.