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