Need help troubleshooting

What am I doing wrong that when my studentCounter & totalPassedCounter outputs, there's always one extra? Is that because I initalized number to = 1 to start? How could I go about fixing 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
//examgrade.cpp

#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

           
   int main()
           
   
   {
   
      int number, studentCounter, totalPassedCounter, totalFailed;
   
      number=1;
      studentCounter = 0;
      totalPassedCounter = 0;
      totalFailed = 0;
   
   
      while (number==1)
      {
         studentCounter++;
         totalPassedCounter++;
         cout << "Enter result (1 = pass, 2 = fail, -1 = done): ";
         cin >> number;
         cout << endl;
      }
      while (number==2)
      {
         studentCounter++;
         totalFailed++;
         cout << "Enter result (1 = pass, 2 = fail, -1 = done): ";
         cin >> number;
         cout << endl;
      }
      if (number==-1)
      {
         cout << "There was a total of " << studentCounter << " students that took the test.\n";
         cout << "The number of students that passed was: \n" << totalPassedCounter;
         cout << "The number of students that failed was: \n" << totalFailed << endl << endl;
      }
      if (totalPassedCounter > 8){
         cout << "You should raise tuition." << endl;
      
      }
   
      return (0);
   
   }
Last edited on
Why are you incrementing studentCounter and totalPassedCounter before you've ever asked the user for the first result?

Yes, you started out with 1 that didn't correspond to a student passing. I think I'd initialize the variable to 0 and restructure how the code loops.

For example, what are you expecting to happen if someone enters a 1 in this loop? i.e. they enter a passing grade after a failing grade? It's not going to jump back to the previous while.

1
2
3
4
5
6
7
8
while (number==2)
      {
         studentCounter++;
         totalFailed++;
         cout << "Enter result (1 = pass, 2 = fail, -1 = done): ";
         cin >> number;
         cout << endl;
      }



maybe something like this...

- ask for entry

While sentinel value not entered
- increment student counter
- if pass, increment pass counter
- if fail, increment fail counter
- ask for entry
Last edited on
Why are you incrementing studentCounter and totalPassedCounter before you've ever asked the user for the first result?


Because I'm new to C, and trying to learn. It's the same reason I asked for help in the first place. Forgive me for being under the "Beginner" section of the site.

Yes, you started out with 1 that didn't correspond to a student passing. I think I'd initialize the variable to 0 and restructure how the code loops.


Thanks, I'll try what you recommended.
Because I'm new to C, and trying to learn. It's the same reason I asked for help in the first place. Forgive me for being under the "Beginner" section of the site.

Um, I meant that as a genuine question. For all I know, you had a particular reason for wanting to do it that way. There's no need to be defensive.
Um, I meant that as a genuine question. For all I know, you had a particular reason for wanting to do it that way. There's no need to be defensive.


I apologize. It seemed as though it was a sarcastic question.
Topic archived. No new replies allowed.