Shaik
Shaik

Reputation: 33

Why does my code jump to else immediately?

#include <stdio.h>
int printMenu(int);
int studentglobal;
float getCarryMark(float);
float cm1;


main()
{
    printf("-----------------------------------------------------\n");
    printf("\t\tTotal Score calculator\n");
    printf("-----------------------------------------------------\n");
    int counter, x, studentcount = 1,sum = 0 ;

    x = printMenu(studentglobal);

    for (counter = 0; counter < x; counter++)

    {
        studentcount = studentcount + counter;
        printf("Student : %d \n", studentcount);
        getCarryMark(cm1);

        if (cm1 >= 0 && cm1 <= 50)
        {
            printf("right range!!\n");
        }
        else
        {
            printf("INVALID RANGE!!!\n");
        }
        printf("%.2f\n", cm1);
    }

}

int printMenu(int nstudent)
{
    printf("Enter no of student: ");
    scanf("%d", &nstudent);
    return(nstudent);
}

float getCarryMark(float carrymark)
{
    printf("Enter your carrymarks: ");
    scanf("%f", &carrymark);
    return(carrymark);
}

So actually when I enter 200, it shows INVALID RANGE!!!, but when I enter 20 it still shows INVALID RANGE!!!. It somehow skipped the if statement. Please don't bother the other part, if I have any mistake tell me please. ert gf dfg dgd dg dfgd gd dg dg dgdfg

Upvotes: 0

Views: 105

Answers (4)

Mete Cantimur
Mete Cantimur

Reputation: 1401

getCarryMark function takes a parameter by value, modifies the value and returns it back, however, the returned value is never used. Modifying the parameter's value does not reflect this change to the outside since it has been passed by value.

I have partially updated the code so that it could execute the if statement. Please try the following code.

#include <stdio.h>
int printMenu(int);
int studentglobal;
float getCarryMark(float);
float cm1;


main()
{
    printf("-----------------------------------------------------\n");
    printf("\t\tTotal Score calculator\n");
    printf("-----------------------------------------------------\n");
    int counter, x, studentcount = 1,sum = 0 ;

    x = printMenu(studentglobal);

    for (counter = 0; counter < x; counter++)

    {
        studentcount = studentcount + counter;
        printf("Student : %d \n", studentcount);
        cm1 = getCarryMark();

        if (cm1 >= 0 && cm1 <= 50)
        {
            printf("right range!!\n");
        }
        else
        {
            printf("INVALID RANGE!!!\n");
        }
        printf("%.2f\n", cm1);
    }

}

int printMenu(int nstudent)
{
    printf("Enter no of student: ");
    scanf("%d", &nstudent);
    return(nstudent);
}

float getCarryMark()
{
    float carrymark = 0.0;

    printf("Enter your carrymarks: ");
    scanf("%f", &carrymark);
    return(carrymark);
}

Upvotes: 2

J.M Smith
J.M Smith

Reputation: 391

You need to return carrymark from getCarryMark:

float getCarryMark(float carrymark)
{
    printf("Enter your carrymarks: ");
    scanf("%f", &carrymark);
    return(carrymark);
}

Upvotes: 4

Alessandro
Alessandro

Reputation: 4472

You missed the return statement in getCarryMark

Upvotes: 2

DiegoS
DiegoS

Reputation: 826

You are missing a return statement in getCarryMarks method !

Upvotes: 3

Related Questions