This is my main function
#include <stdio.h>
void getScore();
int calcScore(int a, int b, int c);
int calcGrade();
int main(){
calcgrade();
return 0;
}
My second function is to get the input by the user
void getScore()
{
int Ave, score1, score2, score3;
printf("\t\tENTER TEST SCORES <0-100>\n");
printf("Enter test score 1:\t");
scanf("%d", &score1);
printf("Enter test score 2:\t");
scanf("%d", &score2);
printf("Enter test score 3:\t");
scanf("%d", &score3);
Ave = calcScore(score1, score2, score3);
}
My third function is to calculate the score
int calcScore(int a, int b, int c)
{
int Ave, Div;
Ave = a b c;
Div = Ave / 3;
return Ave, Div;
}
And now my fourth function and the main problem is my if else statement
int calcGrade()
{
int score1, score2, score3, Ave;
getScore();
if (Ave>=90)
printf("\t\t Your Final Grade is: A");
else if (Ave>=70 && score3>=90)
printf("\t\t Your Final Grade is: A");
else if (Ave>=70 && score3<=89)
printf("\t\t Your Final Grade is: B");
else if (Ave>=50 && score2>=70 && score3>=70)
printf("\t\t Your Final Grade is: C");
else if (Ave>50 && score2<=69 && score3<=69)
printf("\t\t Your Final Grade is: D");
else(Ave<50);
printf("\t\t Your Final Grade is: F");
}
The problem is that, when i run the program, it doesn't execute the if else statements, and it goes on to print the else statement.
CodePudding user response:
Variables declared in functions are local to that function scope, even if they share the same identifier as variables declared in other functions. Ave
in getScore
and Ave
in calcScore
do not refer to the same piece of memory.
To pass values between functions you can use function arguments and return values, much like you did with calcScore
.
It should be noted that you cannot return multiple values from a function, and
return Ave, Div;
will actually return the last value in the comma-separated list.
To "return" multiple pieces of data from a function you can use pointers as function arguments, to place values in a memory location (just as scanf
does).
Additionally:
else(Ave<50);
printf("\t\t Your Final Grade is: F");
else
does not have a conditional part. This is an else statement whose body is the statement (Ave<50)
, an operation with no side-effect. The printf
call belongs to the same block as the else
.
An example. Note that one
, two
, and three
are already pointers in the getScores
function, and as such can be passed directly to scanf
.
#include <stdio.h>
void getScores(int *one, int *two, int *three) {
printf("\t\tENTER TEST SCORES <0-100>\n");
printf("Enter test score 1:\t");
scanf("%d", one);
printf("Enter test score 2:\t");
scanf("%d", two);
printf("Enter test score 3:\t");
scanf("%d", three);
}
int calcAverage(int a, int b, int c) {
return (a b c) / 3;
}
void calcGrade(void) {
int score1, score2, score3, avg;
getScores(&score1, &score2, &score3);
avg = calcAverage(score1, score2, score3);
if (avg >= 90)
printf("\t\t Your Final Grade is: A\n");
else if (avg >= 70 && score3 >= 90)
printf("\t\t Your Final Grade is: A\n");
else if (avg >= 70 && score3 <= 89)
printf("\t\t Your Final Grade is: B\n");
else if (avg >= 50 && score2 >= 70 && score3 >= 70)
printf("\t\t Your Final Grade is: C\n");
else if (avg > 50 && score2 <= 69 && score3 <= 69)
printf("\t\t Your Final Grade is: D\n");
else if (avg < 50)
printf("\t\t Your Final Grade is: F\n");
}
int main(void) {
calcGrade();
}
CodePudding user response:
The problem here is that you call the getScore()
which will change the Ave instantly, so you need to make the getScore()
as a return function so it can change the value of the Ave. or you need to make the variable Ave as A global variable.
You need to read more about global and local scopes and to read more about returnable functions and you can read too about how to use pointers to change values too.