I need to make a function float calculateClaim(vehicleClass, distanceTravel) that accepts class of vehicle and distance traveled, determines the rate and returns the claim amount. The main function reads the class of vehicle and distance traveled, calls function calculateClaim() and calculates the total mileage claim.

However, I cannot display the amount of claim. It only displays 0.00.

The program that I had done is like this :

#include <stdio.h>
float calculateClaim(char, int);
int main(void){
    char Vehicle, choice;
    int Distance, num;
    float Amount, Total;

    printf("...............................................................\n");
    printf("                 Class of Vehicle\n");
    printf("...............................................................\n");
    printf("            A   B   C   D\n");
    printf("Rate (cent/km)      70  60  50  45\n");
    printf("...............................................................\n\n");

    printf("Enter Class of Vehicle      : ");
    scanf("%c", &Vehicle);
    num = 1;
    Total = 0;

    do{ 
        printf("Enter Distance Travelled (km)   : ");
        scanf("%d", &Distance);
        calculateClaim(Vehicle, Distance);
        printf("Amount of Claim %d \t\t: %.2f \n", num, Amount);
        printf("Next Mileage Claim (enter Y/N)\t: ");
        scanf(" %ch", &choice);
        printf("\n");
        num++;  
        Total = Amount + Total;
    }while(choice == 'Y');

    printf("\nTotal Claim Amount \t\t: %.2f", Total);
    return 0;
}   

float calculateClaim(char vehicleClass, int distanceTravel){
    float Amount;
    float calculateClaim;   
    if(vehicleClass=='A')
     Amount = 0.7 * distanceTravel;
    else if(vehicleClass=='a')
        Amount = 0.7 * distanceTravel;
    else if(vehicleClass=='B')
        Amount = 0.6 * distanceTravel;
    else if(vehicleClass=='b')
        Amount = 0.6 * distanceTravel;
    else if(vehicleClass=='C')
        Amount = 0.5 * distanceTravel;
    else if(vehicleClass=='c')
        Amount = 0.5 * distanceTravel;
    else if(vehicleClass=='D')
        Amount = 0.45 * distanceTravel;
    else if(vehicleClass=='d')
        Amount = 0.45 * distanceTravel; 

    return calculateClaim;
}

2 Answers

0
Vatsal On

You did not set the Amount variable to hold the value returned by calculateClaim function. Also your calculateClaim function should return the Amount not the variable calculateClaim(which is never set inside your function).

#include <stdio.h>
float calculateClaim(char, int);
int main(void){
    char Vehicle, choice;
    int Distance, num;
    float Amount, Total;


    printf("...............................................................\n");
    printf("                 Class of Vehicle\n");
    printf("...............................................................\n");
    printf("            A   B   C   D\n");
    printf("Rate (cent/km)      70  60  50  45\n");
    printf("...............................................................\n\n");

    printf("Enter Class of Vehicle      : ");
    scanf("%c", &Vehicle);
    num = 1;
    Total = 0;

    do{

        printf("Enter Distance Travelled (km)   : ");
        scanf("%d", &Distance);
        Amount = calculateClaim(Vehicle, Distance);
        printf("Amount of Claim %d \t\t: %.2f \n", num, Amount);
        printf("Next Mileage Claim (enter Y/N)\t: ");
        scanf(" %ch", &choice);
        printf("\n");
        num++;
        Total = Amount + Total;

    }   while(choice == 'Y');

        printf("\nTotal Claim Amount \t\t: %.2f", Total);
        return 0;
}
    float calculateClaim(char vehicleClass, int distanceTravel){

    float Amount;
    float calculateClaim;
        if(vehicleClass=='A')
         Amount = 0.7 * distanceTravel;
        else if(vehicleClass=='a')
            Amount = 0.7 * distanceTravel;
            else if(vehicleClass=='B')
                Amount = 0.6 * distanceTravel;
                else if(vehicleClass=='b')
                    Amount = 0.6 * distanceTravel;
                    else if(vehicleClass=='C')
                        Amount = 0.5 * distanceTravel;
                        else if(vehicleClass=='c')
                            Amount = 0.5 * distanceTravel;
                            else if(vehicleClass=='D')
                                Amount = 0.45 * distanceTravel;
                                else if(vehicleClass=='d')
                                    Amount = 0.45 * distanceTravel;

    return Amount;
    }
0
J...S On

As has been pointed out, you are not using the value returned by the calculateClaim() function.

In calculateClaim(), you have a redundant float variable calculateClaim as you are actually using just the Amount variable.

Instead of returning the float variable calculateClaim, from the calculateClaim() function, return Amount and remove the usage of the redundant variable.

And it is not a good practice to name a variable with the same name as a function.

Also, you are repeating yourself, to check for case insensitive comparison in

if(vehicleClass=='A')
    Amount = 0.7 * distanceTravel;
else if(vehicleClass=='a')
    Amount = 0.7 * distanceTravel;

Instead, use the logical OR operator (||) and try something like

if(vehicleClass=='A' || vehicleClass=='a' )
    Amount = 0.7 * distanceTravel;