-
New To C. Need Help
Hello,
I am a college student taking a C course and my program isn't doing what it is supposed to do.
it isn't displaying (maybe not recording) values that it is supposed to.
I am supposed to take letter grades of ten students, turn them into numerical GPA using else/if function.
Any help please. Thank you - Robert
Not looking for anyone to do it for me... just advice. thanks.
Code:
#include<stdio.h>
#include<math.h>
double average(char z);
int main(void)
{
double g1;
double g2;
double g3;
double g4;
double g5;
double g6;
double g7;
double g8;
double g9;
double g10;
double AA;
double AB;
double AC;
double AD;
double AE;
double AF;
double AG;
double AH;
double AI;
double AJ;
double grade;
printf("Enter grade for students 1-10\n");
scanf("%c", &g1);
scanf("%c", &g2);
scanf("%c", &g3);
scanf("%c", &g4);
scanf("%c", &g5);
scanf("%c", &g6);
scanf("%c", &g7);
scanf("%c", &g8);
scanf("%c", &g9);
scanf("%c", &g10);
AA = (g1);
AB = (g2);
AC = (g3);
AD = (g4);
AE = (g5);
AF = (g6);
AG = (g7);
AH = (g8);
AI = (g9);
AJ = (g10);
return (0)
}
double average(char z)
{
char a, b, c, d, f;
double grade, final;
double AA, AB, AC, AD, AE, AF, AG, AH, AI, AJ;
if (z=='a')
{
grade = 4.0;
}
else if (z=='b')
grade = 3.0;
else if (z=='c')
grade = 2.0;
else if (z=='d')
grade = 1.0;
else if (z=='f')
grade = 0.0;
;
return(grade);
}
-
Hum... looks like there's a good amout of errors in your code...
Code:
scanf("%c", &g1);
You declared g1 as a double. You need to use the %lf specifier for double, not the %c one (%c is for char). But in your case, you should declare g1..g10 as char, not double...
Note that using scanf may cause you some trouble, since reading charaters one by one need some "special" treatement... since when you press enter, a '\n' char is sent to the input buffer, and your program willl eventually read this character etc etc..
By the way, you should use arrays instead of declaring lists of variable and writing multiple lines of code who's doing the same thing...
Huh. Maybe you mean Note that your average function prototype takes a char for the first argument, not a double.
Code:
char a, b, c, d, f;
double grade, final;
double AA, AB, AC, AD, AE, AF, AG, AH, AI, AJ;
Duh. You are only using one variable (grade)... why declaring so many useless variables ?
Well... this is really one bad looking piece of code for anyone having a good experience in C... maybe you should consider reading/listening more in your class, or i don't know what...
Oh and by the way, no need to include <math.h> in your case.
[Edit] Also, it's not printing anything to the screen because there's no instruction to do such.. consider adding some "printf("%f\n", AA);" to your code if you want something to appear to your screen.
-
thank you very much...
we havent learned about arrays and we were taught to use scanf in that manner...
I am a math major required to take this course... Although it is very interesting, I struggle a little with the language...
our book isn't very good either and has little to no examples.
I will try your advice though, thank you once again.
-
Our web site and forum are searchable, and most of the time you can find help with your question. I think these will help:
http://www.google.com/custom?domains...ments+tutorial