-5

This code take input from the user (characters C,T,B) and (int 0-24 and 0-60) to calculate the cost of parking based on the type of vehicle the user inputs.

the last line of code in the program is supossed to print the result of the function "charged" which is determined by the type of vehicle declared by the user inputed char value but when i run it only retruns 0.00 instead of the flaot value any and all help is appreciated :)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>



int total_minute_parked (int minute_in, int minute_left)
{
    int minute_parked;
    if (minute_in > minute_left)
    {
        minute_parked = (minute_left  - minute_in + 60);
    }
    else
    {
        minute_parked = (minute_left - minute_in);
    }
    return minute_parked;
}

// func calc total hours parked

int total_hour_parked (int hour_in, int hour_left)
{
    int hours_parked;
    if (hour_left > hour_in)
    {

        hours_parked = abs((hour_left - 1) - hour_in);
    }
    else
    {
        hours_parked = abs(hour_left - hour_in);
    }

    return hours_parked ;
}

// -------------------funtion to calc charge based off type of vehicle------

float charged (char vehicle_type,int total_hour_parked)
{

    char C;
    char T;
    char B;

    float temp_charged;

    if  (vehicle_type == C) // -------------------------------CAR
    {
        if (total_hour_parked > 3)
        {
            float secondary_hour = total_hour_parked - 3;
            temp_charged = secondary_hour * 1.5;
        }
        else
        {
            temp_charged = 0;
        }
    }

    else if  (vehicle_type == T)   // ------------------------------TRUCK
    {
        if (total_hour_parked > 2)
        {
            float secondary_hour = total_hour_parked - 2;
            temp_charged = (secondary_hour * 2.3) + 1.0;
        }
        else {
            temp_charged = 1;
        }
    }

    else if  (vehicle_type == B) // -----------------------------------BUS
    {
        if (total_hour_parked > 1)
        {
            float secondary_hour = total_hour_parked - 1;
            temp_charged = (secondary_hour * 3.7) + 2.0;
        }
        else {
            temp_charged = 2;
        }
    }
    return temp_charged;
}

//---------------------- end program upon invalid imput -------------------//



// --------------------- main that prints results and takes imput -----------//

int main()

{

    int total_hour_parked (int hour_in,int hour_left);
    float charged (char vehicle_type, int total_hour_parked);
    char vehicle_type;
    int hour_in = 0;
    int minute_in = 0;

    int hour_left = 0;
    int minute_left = 0;

    printf("Please enter the type of Vehicle:");
    scanf("%c",&vehicle_type);

    printf("Please enter the hour entered lot:");
    scanf("%d", &hour_in);

    printf("Please enter the minute entered lot:");
    scanf("%d", &minute_in);

    printf("Please enter the hour left lot:");
    scanf("%d", &hour_left);

    printf("Please enter the minute left lot:");
    scanf("%d", &minute_left);

    printf("------------------------------------\n");

    printf("Parking time: %d:%d\n", total_hour_parked(hour_in,hour_left),total_minute_parked(minute_in,minute_left));

    printf("Cost %f",charged(vehicle_type,total_hour_parked));

    return 0;
}
3
  • 1
    Do you really mean to have vehicle_type == 'C'? Because vehicle_type == C makes no sense as the variable C is uninitialized and will have an indeterminate (and seemingly random) value. Commented Oct 16, 2018 at 6:29
  • 1
    Fyi, there are many warnings in this code, in particular related to usage of indeterminate variables in expressions. Things like if (vehicle_type == C) make no sense, when the variable C has no determinate value. I concur with Some programmer dude. It looks like you want to use character literals in this. The final printf arguments don't make much sense either. int total_hour_parked (int hour_in,int hour_left); declares a function. Passing it as an argument (uncalled, so a function pointer) to charged makes no sense. Commented Oct 16, 2018 at 6:30
  • 1
    you already ask about another problem in this code, maybe its time to use debugger? Commented Oct 16, 2018 at 6:30

2 Answers 2

2

I'm not sure if it's going to solve all the problems in the code, but there's an issue here:

char C;
char T;
char B;

float temp_charged;

if  (vehicle_type == C) // -------------------------------CAR 

What this does is it declares three char and doesn't assign any value (so accessing them is undefined and will result in some rubbish value). Then you are comparing vehicle_type against those chars. The result will most likely be false (or 0). That's not what you intended. Instead, do this:

if  (vehicle_type == 'C')

You probably misunderstood what char C; means. It doesn't mean "make a char with value 'C'", but "make a char named C and don't initialize it". But in this scenario you don't need those three chars anyway bcause you can just compare vehicle_type with a literal.

Sign up to request clarification or add additional context in comments.

Comments

0

Their are several problems in the above code.

1). You are comparing vehicle_type with the char which are not assign so first assign the values to this char variables

char C = 'C';
char T = 'T';
char B = 'B';

Or you can directly compare like

if (vehicle_type == 'T')

2). total_hour_parked is a function not a integer variable so what you can do is store the return value of total_hour_parked in any variable

Eg:-

int m_total_hour_parked = total_hour_parked(hour_in, hour_left);
printf("Parking time: %d:%d\n", m_total_hour_parked, total_minute_parked(minute_in, minute_left));
printf("Cost %f", charged(vehicle_type, m_total_hour_parked));

Hopefully that helps

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.