0

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.

Oka
  • 23,367
  • 6
  • 42
  • 53
  • 1
    You delcare local variable but you should pass the variables as arguments to the functions instead. – Ted Lyngmo Nov 15 '21 at 07:22
  • 1
    The variable `Ave` in `calcGrade` is uninitialized along with the other unused variables. Its value is indeterminate. Just because you use a variable with the same name in another function doesn't affect anything else with that name unless it is a global variable. Yours aren't. `return Ave, Div;` doesn't do what you might think it does. You can't return two values from a function. – Retired Ninja Nov 15 '21 at 07:23

2 Answers2

2

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();
}
Oka
  • 23,367
  • 6
  • 42
  • 53
  • Thank you very much! This is very informative. I'm new in programming, this helped so much – kanseidurifto09 Nov 15 '21 at 07:50
  • Another question, how come you don't have & in scanf but it still works? – kanseidurifto09 Nov 15 '21 at 07:51
  • @kanseidurifto09 It's important to remember that the `&` operator gives you the memory address of a particular variable. These memory addresses are just values, and can be passed around like any other value. We get the memory addresses of our local score variables in `calcGrade`, and pass those *pointer values* to the `getScores` function. `scanf` expects *pointer values* as it needs to know where in memory to store the data it reads, and we already have those memory addresses. – Oka Nov 15 '21 at 08:02
  • I'm sorry, can you explain that in a much more simpler terms? It's just that i'm new and i have a hard time understanding all of these terms. – kanseidurifto09 Nov 15 '21 at 08:06
  • @kanseidurifto09 It would be hard to simplify that further, and still have it be technical and/or *succinct*. Basically put, because we already used the `&` operator here: `getScores(&score1, &score2, &score3);` we don't need it in our calls to `scanf` because `one`, `two`, and `three` are already of the correct *type* (pointer-to-int). Pointers are very often a sore spot for beginners and I would encourage you to find a good [book](https://stackoverflow.com/a/562377/2505965) that covers them well as they are very important in C. – Oka Nov 15 '21 at 08:37
  • 1
    This is awesome! Thanks for directing me to the lists of books! – kanseidurifto09 Nov 15 '21 at 08:43
0

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.

Joe. K
  • 9
  • 2