1823384 Members
2529 Online
109654 Solutions
New Discussion юеВ

C Program, im stuck

 
SOLVED
Go to solution
Mikel Howel
Occasional Advisor

C Program, im stuck

im doing a Grading program but i cant get the quiz scores to average, and the test to average as well. can anyone help me, its a C program
i think i can make a temp variable, but how?

#include

int main(int argc,char*argv[])




{
int ch;
int c;
int iJunk;

c = 0;

ch = ' ';


printf("\nEnter Grade For Quiz 1:\n");
scanf("%d",&c);
ch = getc(stdin);

printf("\nEnter Grade For Quiz 2:\n");
scanf("%d",&c);
ch = getc(stdin);

printf("\nEnter Grade For Quiz 3:\n");
scanf("%d",&c);
ch = getc(stdin);

printf("\nEnter Grade For Test 1:\n");
scanf("%d",&c);
ch = getc(stdin);

printf("\nEnter Grade For Test 2:\n");
scanf("%d",&c);
ch = getc(stdin);


if(c>=90)
printf("You get an A\n");
else if((c>=80)&&(c<89))
printf("You get a B\n");
else if((c>=70)&&(c<79))
printf("You get a C\n");
else if((c>=60)&&(c<69))
printf("You get a D\n");
else
printf("Failed the class\n");

ch = getc(stdin);



return 0;


}
18 REPLIES 18
harry d brown jr
Honored Contributor
Solution

Re: C Program, im stuck


Where are you magically adding up scores, counting the number of "scores", and doing division?

live free or die
harry
Live Free or Die
Mikel Howel
Occasional Advisor

Re: C Program, im stuck

Thats what im having trouble with, i dont know how to make it so it will add up the scores for the quiz, and the test sepertly. it only gives a grade for the last data entry. this is as far as i could go, but im stuck the rest of the way...
harry d brown jr
Honored Contributor

Re: C Program, im stuck


Use an array for the scores or

declare another int

int tot;

then after each getc(stdin) line do this:

tot += c;

then before the IF tests do this:

c = tot / 5;


live free or die
harry
Live Free or Die
Mikel Howel
Occasional Advisor

Re: C Program, im stuck

like this? i feel like a dummy asking all these questions. if this is not right could you copy and paste and insert? ive been working on this all day and im getting frustrated. thanks

#include

int main(int argc,char*argv[])




{
int ch;
int c;
int iJunk;
int tot;

c = 0;

ch = ' ';


printf("\nEnter Grade For Quiz 1:\n");
scanf("%d",&c);
ch = getc(stdin);
tot += c;

printf("\nEnter Grade For Quiz 2:\n");
scanf("%d",&c);
ch = getc(stdin);
tot += c;

printf("\nEnter Grade For Quiz 3:\n");
scanf("%d",&c);
ch = getc(stdin);
tot += c;

printf("\nEnter Grade For Test 1:\n");
scanf("%d",&c);
ch = getc(stdin);
tot += c;

printf("\nEnter Grade For Test 2:\n");
scanf("%d",&c);
ch = getc(stdin);
tot += c;

c = tot / 5;
if(c>=90)
printf("You get an A\n");
else if((c>=80)&&(c<89))
printf("You get a B\n");
else if((c>=70)&&(c<79))
printf("You get a C\n");
else if((c>=60)&&(c<69))
printf("You get a D\n");
else
printf("Failed the class\n");

ch = getc(stdin);



return 0;


}
Rajeev  Shukla
Honored Contributor

Re: C Program, im stuck

Hi Try out this simple one where you pass the number of quize as argument and then supply the marks for each quiz and finally it prints the Grade and the average.
Let me know if you need more help in modifying it.

#include
#include
#define MAX 10
main(argc,argv)
int argc;
char *argv[];
{
int i,j, buff[MAX], k, avr;
k=0;
if(argc != 2){
printf("Usage: %s \n", argv[0]);
exit(1);
}
i = atof(argv[1]);
for(j=0;j printf("Enter the Marks for Quiz %d:", (j+1));
scanf("%d", &buff[j]);
}
for(j=0;j k=k+buff[j];
avr = k/i;
if(avr>80)
printf("You have Got Grade A\n");
if((avr>60) && (avr<80))
printf("You have Got Grade B\n");
if((avr>40) && (avr<60))
printf("You have Got Grade C\n");
if(avr<40)
printf("You Have failed\n");
printf("Average=%d\n", avr);
}

Cheers
Rajeev
Mikel Howel
Occasional Advisor

Re: C Program, im stuck

I can see what your geting at, but when i run it, it says you have 33 errors.
Mikel Howel
Occasional Advisor

Re: C Program, im stuck

But that is what i want to do, enter number of quizes, and also do same for test. get a average for quiz, and average for test. then get a group average of everyone i entered. this is whatis throwing me for a loop
Yogeeraj_1
Honored Contributor

Re: C Program, im stuck

hi,
the code posted by Rajeev works fine!

Did you compile the program?
==============================
L1000: home/yogeeraj>cc test2.c
/usr/ccs/bin/ld: (Warning) At least one PA 2.0 object file (test2.o) was detected. The linked output may not run on a PA 1.x system.
L1000: home/deg>a.out
Usage: a.out
L1000: home/yogeeraj>a.out 11
Enter the Marks for Quiz 1:12
Enter the Marks for Quiz 2:12
Enter the Marks for Quiz 3:34
Enter the Marks for Quiz 4:45
Enter the Marks for Quiz 5:67
Enter the Marks for Quiz 6:34
Enter the Marks for Quiz 7:67
Enter the Marks for Quiz 8:34
Enter the Marks for Quiz 9:67
Enter the Marks for Quiz 10:345
Enter the Marks for Quiz 11:345
You have Got Grade A
Average=193
L1000: home/yogeeraj>
==============================

Why don't describe your problem with an example?

regards
Yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Yogeeraj_1
Honored Contributor

Re: C Program, im stuck

hi,
the code posted by Rajeev works fine!

Did you compile the program?
==============================
L1000: home/yogeeraj>cc test2.c
/usr/ccs/bin/ld: (Warning) At least one PA 2.0 object file (test2.o) was detected. The linked output may not run on a PA 1.x system.
L1000: home/deg>a.out
Usage: a.out
L1000: home/yogeeraj>a.out 11
Enter the Marks for Quiz 1:12
Enter the Marks for Quiz 2:12
Enter the Marks for Quiz 3:34
Enter the Marks for Quiz 4:45
Enter the Marks for Quiz 5:67
Enter the Marks for Quiz 6:34
Enter the Marks for Quiz 7:67
Enter the Marks for Quiz 8:34
Enter the Marks for Quiz 9:67
Enter the Marks for Quiz 10:345
Enter the Marks for Quiz 11:345
You have Got Grade A
Average=193
L1000: home/yogeeraj>
==============================

Why don't describe your problem with an example?

regards
Yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Mikel Howel
Occasional Advisor

Re: C Program, im stuck

when i compiled the program using bloodshed it came back with 33 errors. to me it looks right. but why does it come back with these errors?
Mikel Howel
Occasional Advisor

Re: C Program, im stuck

do you recomend a different c compiler? ill try the rest tomorrow because its late. thanks for the help. ill be on tomorrow probably asking more questions. thanks again
Yogeeraj_1
Honored Contributor

Re: C Program, im stuck

hi,

can you post the error messages you are getting?

regards
Yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Mikel Howel
Occasional Advisor

Re: C Program, im stuck

this is what it came up with:


Executing g++.exe...
g++.exe "C:\My Documents\Untitled3.cpp" -o "C:\My Documents\Untitled3.exe" -g3 -I"C:\DEV-CPP\include" -I"C:\DEV-CPP\include\g++-3" -I"C:\DEV-CPP\include" -L"C:\DEV-CPP\lib"
C:\My Documents\Untitled3.cpp:4: `argc' was not declared in this scope
C:\My Documents\Untitled3.cpp:4: `argv' was not declared in this scope

C:\My Documents\Untitled3.cpp:5: ANSI C++ forbids declaration `main' with no type
C:\My Documents\Untitled3.cpp:5: initializer list being treated as compound expression
C:\My Documents\Untitled3.cpp:5: syntax error before `int'
C:\My Documents\Untitled3.cpp:7: parse error before `{'

C:\My Documents\Untitled3.cpp:9: ANSI C++ forbids declaration `k' with no type

C:\My Documents\Untitled3.cpp:10: parse error before `if'
C:\My Documents\Untitled3.cpp:12: ANSI C++ forbids declaration `exit' with no type
C:\My Documents\Untitled3.cpp:12: `int exit' redeclared as different kind of symbol
C:/DEV-CPP/include/stdlib.h:325: previous declaration of `void exit(int)'
C:\My Documents\Untitled3.cpp:13: parse error before `}'
C:\My Documents\Untitled3.cpp:14: ANSI C++ forbids declaration `i' with no type
C:\My Documents\Untitled3.cpp:14: warning: initialization to `int' from `double'
C:\My Documents\Untitled3.cpp:15: parse error before `for'
C:\My Documents\Untitled3.cpp:15: parse error before `;'
C:\My Documents\Untitled3.cpp:15: syntax error before `++'
C:\My Documents\Untitled3.cpp:17: `buff' was not declared in this scope
C:\My Documents\Untitled3.cpp:17: `j' was not declared in this scope
C:\My Documents\Untitled3.cpp:17: ANSI C++ forbids declaration `scanf' with no type
C:\My Documents\Untitled3.cpp:17: `int scanf' redeclared as different kind of symbol
C:/DEV-CPP/include/stdio.h:231: previous declaration of `int scanf(const char *, ...)'

C:\My Documents\Untitled3.cpp:17: initializer list being treated as compound expression
C:\My Documents\Untitled3.cpp:18: parse error before `}'
C:\My Documents\Untitled3.cpp:19: parse error before `;'
C:\My Documents\Untitled3.cpp:19: syntax error before `++'
C:\My Documents\Untitled3.cpp:21: ANSI C++ forbids declaration `avr' with no type
C:\My Documents\Untitled3.cpp:22: parse error before `if'
C:\My Documents\Untitled3.cpp:30: ANSI C++ forbids declaration `printf' with no type
C:\My Documents\Untitled3.cpp:30: `int printf' redeclared as different kind of symbol
C:/DEV-CPP/include/stdio.h:211: previous declaration of `int printf(const char *, ...)'
C:\My Documents\Untitled3.cpp:30: initializer list being treated as compound expression
C:\My Documents\Untitled3.cpp:31: parse error before `}'

C:\My Documents\Untitled3.cpp:6: storage size of `argv' isn't known

Execution terminated
Adam J Markiewicz
Trusted Contributor

Re: C Program, im stuck

My two words:

First, you didn't initialize 'tot' to 0 at the beginning, so it can have any value at the beginning. So change
int tot;
to
int tot = 0;

Second syntax of your main() in the second try is quite old and looks like your compiler do not accept it. So instead of:

main(argc argv)
int argc;
char *argv[];
{

try:

int main( int argc, char *argv[] )
{


Good luck

Adam
I do everything perfectly, except from my mistakes
Mikel Howel
Occasional Advisor

Re: C Program, im stuck

i got it down to i bug, but when i run it it works, but only allows you to enter number of quiz then shuts off.

here is the bug

-I"C:\DEV-CPP\include" -L"C:\DEV-CPP\lib"
C:\My Documents\Untitled2.cpp: In function `int main(int, char **)':
C:\My Documents\Untitled2.cpp:18: warning: assignment to `int' from `double'

Execution terminated
Compilation successful

can anyone help me with this?

#include
#include
#define MAX 10

int main(int argc,char*argv[])

{
int i,j, buff[MAX], k, avr;
int iJunk;
k=0;
if(argc != 2){
printf("Usage: %s \n", argv[0]);
iJunk = getchar();
exit(1);

}

i = atof(argv[1]);
for(j=0;jprintf("Enter the Marks for Quiz %d:", (j+1));
scanf("%d", &buff[j]);
}

for(j=0;jk=k+buff[j];
avr = k/i;
if(avr>80)
printf("You have Got Grade A\n");
if((avr>60) && (avr<80))
printf("You have Got Grade B\n");
if((avr>40) && (avr<60))
printf("You have Got Grade C\n");
if(avr<40)
printf("You Have failed\n");
printf("Average=%d\n", avr);
}
harry d brown jr
Honored Contributor

Re: C Program, im stuck


Mikel,

on line 18 you are using atof() which is converting a string to a double-float, so assigning it to a "int" isn't cutting it.

I think you want the "atoi" function.


live free or die
harry
Live Free or Die
Mikel Howel_1
Advisor

Re: C Program, im stuck

sorry i forgot my log in so i had to do a new one, if this doesnt let me assign points then i will post a new message with this. this is what i have so far. it works fine, except i dont know how to add this:

printf("You get an A\n");
else if((c>=80)&&(c<89))
printf("You get a B\n");
else if((c>=70)&&(c<79))
printf("You get a C\n");
else if((c>=60)&&(c<69))
printf("You get a D\n");
else
printf("Failed the class\n");

ch = getc(stdin);

because i need the quizes to average out seperatly, and the same for the tests. and then add it to the program below, someone please help. me head hurts!!! lol



#include

struct student_info{
char fname[26];
char lname[31];
int quiza[4];
int quizb[4];
int quizc[4];
int testa[4];
int testb[4];

};

typedef struct student_info SI;

int main()
{

SI student[5];
int iJunk;
int iCount;

for (iCount = 0; iCount < 3; iCount++){
printf("Students First Name:");
scanf("%s", student[iCount].fname );
iJunk = getchar();


printf("Students Last Name:");
scanf("%s", student[iCount].lname );
iJunk = getchar();

printf("Score for Quiz 1:");
scanf("%d", &student[iCount].quiza );
iJunk = getchar();

printf("Score for Quiz 2:");
scanf("%d", &student[iCount].quizb );
iJunk = getchar();

printf("Score For Quiz 3:");
scanf("%d", &student[iCount].quizc );
iJunk = getchar();

printf("Score For Test 1:");
scanf("%d", &student[iCount].testa );
iJunk = getchar();

printf("Score For Test 2:");
scanf("%d", &student[iCount].testb );
iJunk = getchar();
}
printf("\n");

printf("Press a key to continue...\n");
iJunk = getchar();

return 0;
}
Mikel Howel_1
Advisor

Re: C Program, im stuck

ill do a new message since its not going to let me give point, so look for the new message thanks pals