I made it 'compiling'
#include <stdio.h>
#include <stdbool.h>
int main()
{
printf("Welcome to the grade Calculator!\n");
printf("This program will input your homework,quizzes,tests,and final exams and calculate your average.\n");
char input;
int i,n,g;
bool t=true;
int sum=0,sum1=0,sum2=0,sum3=0;
int average;
while (t==true)
{
scanf("%c",&input);
if(input=='h')
{
printf("How many homework grades are to be entered?:");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
printf("Grade %d: ", g);
scanf("%d",&g);
sum+=g;
}
}
if(input=='q')
{
printf("How many quizzes grades are to be entered?:");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
printf("Grade %d:", i);
scanf("%d",&g);
sum1+=g;
}
}
if(input=='t')
{
printf("How many test grades are to be entered?:");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
printf("Grade %d:", i);
scanf("%d",&g);
sum2+=g;
}
}
if(input=='f')
{
printf("How many final grades are to be entered?:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Grade %d:",i);
scanf("%d", &g);
sum3+=g;
}
}
else
{
t = false;
}
if ( t )
{
char c;
printf("Do you want to input any other grade:");
while ((c = getchar()) !='\n' && c != EOF) {}
}
}
Then, I made it better (use data structures and functions, Luke!)
#include <stdio.h>
#include <stdbool.h>
struct Grade
{
char kind;
const char * desc;
bool valid;
double average;
};
double input_grade(const char * desc);
int main()
{
struct Grade grade[] =
{
{ 'h', "homework", false, 0.0 },
{ 'q', "quizzes", false, 0.0 },
{ 't', "tests", false, 0.0 },
{ 'f', "final exams", false, 0.0 },
};
const int GRADES = sizeof(grade)/sizeof(grade[0]);
printf("Welcome to the grade Calculator!\n");
printf("This program will input your homework,quizzes,tests,and final exams and calculate your average.\n");
while (true)
{
char input;
printf("please choose a grade: ");
input = getchar();
int g;
for (g=0; g < GRADES; ++g)
{
if ( input == grade[g].kind)
break;
}
if ( g == GRADES)
break;
grade[g].average = input_grade(grade[g].desc);
grade[g].valid = true;
while ((input = getchar()) != '\n' && input != EOF) {}
}
for (int g=0; g < GRADES; ++g)
{
if (grade[g].valid )
printf("your %s average is %g\n", grade[g].desc, grade[g].average);
}
return 0;
}
double input_grade(const char * desc)
{
int items;
double average = 0.0;
printf("how many %s grades ? ", desc);
scanf("%d", &items);
for (int i = 0; i < items; ++i)
{
printf("grade %d: ", i);
int g;
scanf("%d", &g);
average += g;
}
if ( items )
average /= items;
return average;