How to print out sum for every loop?

Hi, I wanted to sum up the marks of one student, then proceed to the next student, however, the sum of the second student includes the sum of the first student. Here are my codes:
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
#include<stdio.h>
#include<stdlib.h>
#pragma warning (disable:4996)

void main()
{
	int subject, mark, student;
	float total = 0, average = 0;

	for(student=1; student<=3; student++){
		printf("Student No. %d\n", student);

	for (subject = 1; subject <= 3; subject++)
	{
		printf("Marks for Subject %d: ", subject);
		scanf("%d", &mark);
		total += mark;

	}

	average = total / 3;
	printf("Total mark= %.2f\n", total);
	printf("Average mark= %.2f\n", average);
	}
	
	system("pause");
}

The output came out like this:
Student No. 1
Marks for Subject 1: 65
Marks for Subject 2: 58
Marks for Subject 3: 71
Total mark= 194.00
Average mark= 64.67
Student No. 2
Marks for Subject 1: 20
Marks for Subject 2: 40
Marks for Subject 3: 30
Total mark= 284.00
Average mark= 94.67
Student No. 3
Marks for Subject 1:

From the output, you can see that the total mark for student 2 includes the total mark for student 1, so does the average mark. Any help are appreciated.
After L23 and before the flowing closing } you need to set total to 0
thank you @seeplus, I want to ask another question, how to print the highest total mark after finish entering all the students' marks?
Example, the 2nd student has the highest mark, so the program have to print out 2nd student's mark and also print out the number 2 as the top student
this is the output:
THE HIGHEST TOTAL MARK IS --------> 280
THE TOP STUDENT IS STUDENT NO ----> 2
Perhaps:

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
#include <stdio.h>
#include <stdlib.h>

int main()
{
	int mark = 0, total = 0, hitot = 0, hstud = 0;

	for (int student = 1; student <= 3; student++) {
		printf("Student No. %d\n", student);

		for (int subject = 1; subject <= 3; subject++) {
			printf("Marks for Subject %d: ", subject);
			scanf("%d", &mark);
			total += mark;
		}

		printf("Total mark= %d\n", total);
		printf("Average mark= %.2f\n", total / 3.0);

		if (total > hitot) {
			hitot = total;
			hstud = student;
		}

		total = 0;
	}

	printf("THE HIGHEST TOTAL MARK IS %d\n", hitot);
	printf("THE TOP STUDENT IS STUDENT NO %d\n", hstud);

	//system("pause");
}

Declare and initialise total in the innermost scope possible.
1
2
3
4
5
for (int student = 1; student <= 3; student++) {
	printf("Student No. %d\n", student);

	int total = 0;
	for (int subject = 1; subject <= 3; subject++) {


C99 has been available for 20 years, so your compiler really ought to be able to do this now.
Hello AkiraC,

It would be helpful if you mention your operating system and what IDE/compiler you are using. Right now I can only guess that you are using "Windows".

Taking into account everyone's suggestions and some of my own I adjusted your code. Sorry it took so long as I got stuck on a problem I have not been able to resolve yet.

So with changes and tweks I have 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
51
52
53
54
55
56
//#pragma warning (disable:4996)
#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<stdlib.h>

int main()  // <--- Changed.
{
	const int MAXSTUDENTS = 3;

	int highStudent = 0;  // <--- ALWAYS initialize all your variables.
	float highTotalMark = 0.0;

	for (int student = 0; student < MAXSTUDENTS; student++)
	{
		printf("Student No. %d\n", student + 1);  // <--- Changed.

		int scanfResult = 0, mark = 0;  // <--- Added. Moved "mark".
		float totalMarks = 0.0, average = 0.0;  // <--- Moved "average".

		for (int subject = 0; subject < MAXSTUDENTS; subject++)
		{
			printf("Marks for Subject %d: ", subject + 1);  // <--- Changed.

			scanfResult = scanf("%d", &mark);  // <--- Temporary fix. Working on something better.

			//if (!scanfResult && EOF);  // <--- Temporary fix. Working on something better. Not working right.
			//{
				//printf("\n     Error on input! Must be a number.\n     Start over!\n\n");

				//system("pause");

				//return 1;
		        //}

		average = totalMarks / MAXSTUDENTS;  // <--- Changed.

		printf("\nTotal mark = %.2f\n", totalMarks);  // <--- Changed.

		printf("Average mark = %.2f\n\n", average);  // <--- Changed.
		//printf("Average mark = %.2f\n\n", totalMarks / MAXSTUDENTS);

		if (totalMarks > highTotalMark)
		{
			highTotalMark = totalMarks;
			highStudent = student;
		}
	}

	printf("\nTHE HIGHEST TOTAL MARK IS --------> %3.2f\n", highTotalMark);
	printf("THE TOP STUDENT IS STUDENT NO ----> %3d\n\n\n", highStudent + 1);

	system("pause");

	return 0;  // <--- May not be required, but makes a good break point for testing.
}

I have found that line 2 is a better choice than targeting a single error code.

Line 9 is a better choice than a for loop that contains a magic number. It may not seem like much now, but consider a file with over a 1000 lines of code and 30 for loops that need changed. You are very likely to miss 1 somewhere and it may take a long time before it shows up.

Since C and C++ along with other languages are (0)zero based you should get use to starting the loop iterator at (0)zero and using (<) something and leave starting at (1) for the rare occasion. And if you get use to using (<=) in the condition when the time comes to start at (0)zero the (<=) will loop 1 more time than you need. If you are accessing an array this will put you 1 past the end of the array.

As per salem c's suggestion I defined more of the variables in the outer for loop where they are needed and used.

"scanfResult" as the name implies is the return value of the "scanf" function which produced a warning about not using the return value of "scanf". Which is what your code first produced.

While on the subject of "for loops" it is most often best to define the loop iterator's type in the for loop. Save defining the variable outside the for loop for those times when its value is needed after the for loop ends.

A fair example is using a for loop to read a file and using the loop iterator to keep track of how many records were read.

Lines 27 - 34 is an attempt to fix "scanf" when a numeric value is expected an a non numeric value is entered. Seems the when there is a mismatch on what is entered and what is expected that "scanf" fails and becomes unusable the rest of the program. I have been working on fixing the problem, but with little success.

Next is "average". Line 41 can eliminate the need for the variable and the line that gives it a value by doing the calculation in the "printf" function.

Some of the other changes I made are to improve the loop of the output to make it easier to read.

Last point, and backing up, void main() should be replaced with int main(). I am thinking that by C99 standards this has changed.

A test run:
                    // <--- Intentional blank line.
Student No. 1
Marks for Subject 1: 65
Marks for Subject 2: 58
Marks for Subject 3: 71

Total mark = 194.00
Average mark = 64.67

Student No. 2
Marks for Subject 1: 70
Marks for Subject 2: 81
Marks for Subject 3: 90

Total mark = 241.00
Average mark = 80.33

Student No. 3
Marks for Subject 1: 20
Marks for Subject 2: 40
Marks for Subject 3: 30

Total mark = 90.00
Average mark = 30.00


THE HIGHEST TOTAL MARK IS --------> 241.00
THE TOP STUDENT IS STUDENT NO ---->   2


Press any key to continue . . .



Andy
Hello AkiraC,

I finally figured out the problem I was having.

In the above code line 27 ends with a (;) that should not be there. Sometimes it is the little things that are hard to catch.

Removing the (;) allowed me to come up with this solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
for (int subject = 0; subject < MAXSTUDENTS; subject++)
{
	while (printf("Marks for Subject %d: ", subject + 1) &&
			!(scanfResult = scanf("%d", &mark)) || mark < MINMARK || mark > MAXMARK)
	{
		if (!scanfResult)
		{
			printf("\n     Invalid input! Must be a number.\n\n");

			while (getchar() != '\n');
		}
		else if (mark < MINMARK || mark > MAXMARK)
		{
			printf("\n     Value out of range of 0 - 100 inclusive. Try again.\n\n");
		}
	}

	totalMarks += mark;
}

The 2 constant variables "MINMARK" and "MAXMARK" are defined at the top of "main". You could even make use of these variables in the prompt.

The while condition will become true if you enter a non numeric value or a negative number or a number > 100. It should work well for you and the error messages you can change to anything that you like. Although I think that "Invalid input! Must be a number." says it all. I also like indenting error messages to catch your attention.

Andy
Topic archived. No new replies allowed.