Question about program to accept homework scores into an array

Hello all,

I've been working on a problem where I'm supposed to enter 5 types of scores for 10 students into 5 arrays. I think I'm making it way more complicated and messy than it needs to be and confusing myself in the process. I've started out with the code below but after entering the numbers, it starts over asking the same thing. Also, it won't quit when I enter -1, no matter where I put the code to tell it to stop when -1 is entered. Thoughts? 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
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
120


    const int hw1Size = 10;                 // size of arrays
    const int hw2Size = 10;
    const int hw3Size = 10;
    const int mtSize = 10;
    const int finalSize = 10;

    int hw1Array [hw1Size] = {0};      //arrays for scores
    int hw2Array [hw2Size] = {0};
    int hw3Array [hw3Size] = {0};
    int mtArray [mtSize] = {0};
    int finalArray [finalSize] = {0};

    void student1Function();               //function prototype for 1st student

 int main()
{

    while (1)
    {

    student1Function();                  // call to function

            if (hw1Array[0]== -1)
            {
               cout << "If you want to exit, enter -1 on more time or " << endl;
                cout << "Enter Homework #1 for Student #1 one more time:  ";
                cin >> hw1Array[0];

                if (hw1Array[0]== -1)
                {
                exitFunction();                     // call to exit function
                }
            }


return 0;
}

void student1Function()
{

            cout << "Homework #1 score for Student # 1 (or -1 to exit):  ";
            cin >> hw1Array[0];

            if (hw1Array[0]== -1)
            {
                cout << "If you want to exit, enter -1 on more time or " << endl;
                cout << "Enter Homework #1 for Student #1 one more time:  ";
                cin >> hw1Array[0];

                if (hw1Array [0] = -1)
                {
                exitFunction();
                }


            if (hw1Array[0] <= 0 && hw1Array[0] >= 100)
            {
                cout << "Invalid score.  Must be in [0 - 100]";
                cout << "Enter Homework #1 for Student #1 one more time:  ";
                cin >> hw1Array [0];
            }
        
            }

                cout << "Homework #2 score for Student # 1 (or -1 to exit):  ";
                cin >> hw2Array[0];

            if (hw2Array[0] <= 0 && hw2Array[0] >= 100)
            {
                cout << "Invalid score.  Must be in [0 - 100]";
                cout << "Enter Homework #2 for Student #1 one more time:  ";
                cin >> hw2Array [0];
            }
          


                cout << "Homework #3 score for Student # 1 (or -1 to exit):  ";
                cin >> hw3Array[0];

            if (hw3Array[0] <= 0 && hw3Array[0] >= 100)
            {
                cout << "Invalid score.  Must be in [0 - 100]";
                cout << "Enter Homework #3 for Student #1 one more time:  ";
                cin >> hw3Array [0];
            }
          


                cout << "Midterm score for Student # 1 (or -1 to exit):  ";
                cin >> mtArray[0];

            if (mtArray[0] <= 0 && mtArray[0] >= 100)
            {
                cout << "Invalid score.  Must be in [0 - 100]";
                cout << "Enter Midterm score for Student #1 one more time:  ";
                cin >> mtArray [0];
            }
           

             cout << "Final score for Student # 1 (or -1 to exit):  ";
                cin >> finalArray[0];

            if (finalArray[0] <= 0 && finalArray[0] >= 100)
            {
                cout << "Invalid score.  Must be in [0 - 100]";
                cout << "Enter Final score for Student #1 one more time:  ";
                cin >> finalArray [0];
            }
         
   }


void exitFunction()
{
    return;
}
Last edited on
There's a lot of different ways you can do this, but the above is not one I would choose. If it were ME, i would fill each array first. Think like you're the teacher and you're grading 5 different assignments, each of which had 10 students submit grades. I would use a for loop for each, and validate your input inside the loop.

for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17



for (int count  = 0; count < hw1Array[hw1Size]; count++)
{
      int score;
      
      do
     {
       cout << "Homework #1 score for Student " << count +1 << " (or -1 to exit):  ";
       cin >> score;
       if(score < 0 && score > 100){cout << "invalid score\n";}   // remember you can get a 0 or a 100 on a test 

hw1Array[count] = score;
      }
 while (score !=-1);


That may be a little off, after all I'm still a noob too, but I think it would work better.
That's what I first thought would work but if you did it the way you show, you would have to fill in all of the hw1 scores for each student then go to the hw2 then hw3, etc. The expected output is showing it asking for all the different scores for student 1 first then student 2 then student3. etc. so I couldn't figure out how to do it the way you show. That's where I started to get confused as to how to get into an array.
Topic archived. No new replies allowed.