Help with counter

How would I add studentCounter to count while 1 and 2 are input?

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
//examgrade.cpp

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


           
   int main()
           
   
   
   {
   
      int passed = 0; //number of passes
      int failures = 0; //number of failures
      int studentCounter = 0; //student counter
      int result; //one exam result (1=pass, 2=fail)
   
      while (result != -1)
      {
      //prompt user for input & obtain val.
         cout << "Enter result (1=pass, 2=fail, -1=done): ";
         cin >> result; //input result
         
      
      //counters
         if (result == 1)
            passed = passed + 1;
         else if (result == 2)
            failures = failures + 1;
      }
   
      cout << "The number of students that passed were: " << passed << "\nThe number of students that failed were: " << failures << endl;
      cout << "There was a total of: " << studentCounter << " students that took the test." << endl;
   
      if (passed > 8)
         cout << "Raise tuition " << endl;
   }
Last edited on
You can increment student counter in the same block where you increment passes and failures. Just use curly braces so that the compiler knows you want to perform multiple statements.

1
2
3
4
5
6
7
8
9
10
11
	//counters
	if (result == 1)
	{
		passed = passed + 1;
		studentcounter++;
	}
	else if (result == 2)
	{
		failures = failures + 1;
		studentcounter++;
	}
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
           
   int main()
           
   
   
   {
   
      int passed = 0; //number of passes
      int failures = 0; //number of failures
      int studentCounter = 0; //number of students
      int result; //one exam result (1=pass, 2=fail)
   
      while (result != -1)
      {
      //prompt user for input & obtain val.
         cout << "Enter result (1=pass, 2=fail, -1=done): ";
         cin >> result; //input result
      
      //counters
         if (result == 1)
         {
            passed = passed + 1;
            studentCounter++;
         }
         else if (result == 2)
         {
            failures = failures + 1;
            studentCounter++;
         }


I'm getting a Syntax (parse) error at or before end of file. Where else am I suppose to enter a brace?

EDIT: I only ask because putting a brace at the end of the while will just end the statement, won't it?
Last edited on
Also this should be avoided:
1
2
3
      int result; //one exam result (1=pass, 2=fail)
   
      while (result != -1)


Result is undefined therefore it could have any value the first time.
Result is undefined therefore it could have any value the first time.


Thanks. How should I re-write it?
You'll still need a closing brace for the while statement.
@Huppa give it an initial value of anything besides -1. so maybe int result = 0;
You'll still need a closing brace for the while statement.


When I enter the braces you suggested, I get all 0's for the outputs.
Last edited on
@Huppa give it an initial value of anything besides -1. so maybe int result = 0;


Thanks for the help.
Topic archived. No new replies allowed.