This is the code I have written so far.
Code:
#include <stdio.h>#include<math.h>
#define FINAL_EXPECTED 40.0
#define TOTAL_EXPECTED 100.0
struct result{
double final;
double midterm;
double assignment1;
double project;
double quiz;
double labtest;
double total;
};
int main(void)
{
struct result student;
int option;
FILE * fp;
fp = fopen ("markingscheme.txt","w");
do
{
printf("\n\tWeightage for Final : ");
scanf("%lf", &student.final);
if (fabs(student.final - FINAL_EXPECTED)<0.01)
{
printf("\n\tDone");
}
else
{
printf("\n\tError");
}
printf("\n\n\tWeightage for Mid-Term Exam : ");
scanf("%lf", &student.midterm);
/*if (fabs(student.midterm - 20)<0.01) || fabs(student.midterm -30)<0.01)
{
printf("\n\tDone");
}
else
{
printf("\n\tError");
}*/
printf("\n\n\t1. Weightage for Lab Test\n");
printf("\n\n\t2. Weightage for Quiz\n");
printf("\n\n\t3. Weightage for Lab Test & Quiz\n");
printf("\n\n\t4. None\n");
scanf("\n\n\t%d", &option);
if (option==1)
{
printf("\n\n\tWeightage for Lab Test: ");
scanf("%lf", &student.labtest);
}
else if (option==2)
{
printf("\n\n\tWeightage for Quiz: ");
scanf("%lf", &student.quiz);
}
else if (option==3)
{
printf("\n\n\tWeightage for Lab Test & Quiz: ");
scanf("%lf%lf", &student.labtest, &student.quiz);
}
else
{
printf("None\n");
}
if (fabs((student.labtest + student.quiz) - 5.0)<5.01)
{
printf("\n\tDone");
}
else
{
printf("\n\tError");
}
printf("\n\n\t1. Weightage for Assignment 1\n");
printf("\n\n\t2. Weightage for Project\n");
printf("\n\n\t3. Weightage for Assignment 1 & Project\n");
printf("\n\n\t4. None\n");
scanf("\n\n\t%d", &option);
if (option==1)
{
printf("\n\n\tWeightage for Assignment 1 : ");
scanf("%lf", &student.assignment1);
}
else if (option==2)
{
printf("\n\n\tWeightage for Project : ");
scanf("%lf", &student.project);
}
else if (option==3)
{
printf("\n\n\tWeightage for Assignment 1 & Project : ");
scanf("%lf%lf", &student.assignment1, &student.project);
}
else
{
printf("None\n");
}
student.total = student.final + student.midterm + student.labtest + student.quiz + student.assignment1 + student.project;
}while(fabs(student.total - TOTAL_EXPECTED)<0.01);
fclose(fp);
return 0;
}
There appears to be something wrong with this line.
Code:
(fabs(student.midterm - 20)<0.01) || fabs(student.midterm -30)<0.01)
I'm thinking of using the switch statement here. Which is better if..else or switch statement?
Code:
if (option==1)
{
printf("\n\n\tWeightage for Lab Test: ");
scanf("%lf", &student.labtest);
}
else if (option==2)
{
printf("\n\n\tWeightage for Quiz: ");
scanf("%lf", &student.quiz);
}
else if (option==3)
{
printf("\n\n\tWeightage for Lab Test & Quiz: ");
scanf("%lf%lf", &student.labtest, &student.quiz);
}
else
{
printf("None\n");
}
If error was displayed, how can I make the user key in the final marks again?
Code:
if (fabs((student.labtest + student.quiz) - 5.0)<5.01)
{
printf("\n\tDone");
}
else
{
printf("\n\tError");
}
If I use this code, does it mean I should change all printf and scanf statements into fprintf and fscanf statements?
Code:
FILE * fp;
fp = fopen ("markingscheme.txt","w");