Write a C program in to calculate the parking fare for customers who park
their cars in a parking lot when the following information
is given:
A. A character showing the type of vehicle : C for car, B for bus,
T for truck.
B. An integer between 0 and 24 showing the hour the vehicle entered
the lot.
C. An integer between 0 and 60 showing the minute the vehicle entered
the lot.
D. An integer between 0 and 24 showing the hour the vehicle entered
the lot.
E. An integer between 0 and 60 showing the minute the vehicle left
the lot.

Different rates for each type of vechicle, as shown:
Car : $0.00/hr first 3 hr $1.50/hr after 3 hr
Truck : $1.00/hr first 2 hr $2.30/hr after 2 hr
BUS : $2.00/hr first hr $3.70/hr after 1 hr

No vehicle is allowed to stay in the parking lot later than midnight;
it will be towed away.

Then print the parking lot charge.

PLS HELP ME OUT HERE


When I run it I get long negative numbers

/

/ Project3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>

void getData(int* ehour, int* emin, int* exhour, int* exmin);
void rate(int exhour, int exmin, int ehour, int emin, int* thour, int* tmin, int* round);
int charge(char* vehic, float* rate1, float* rate2, int ehour);
void result(int exhour, int exmin, int ehour, int emin, int thour, int round, float total);

int main(void)
{
	char vehic;
	int ehour;
	int emin;
	int exhour;
	int exmin;
	int thour;
	int tmin;
	int round;

	float rate1;
	float rate2;
	float total;

	getData(&ehour, &emin, &exhour, &exmin);
	rate(exhour, exmin, ehour, emin, &thour, &tmin, &round);
	charge(&vehic, &rate1, &rate2, ehour);
	total= rate1 + rate2;
	result( exhour, exmin, ehour, emin, thour, round, total);

	return 0;
}

void getData(int* ehour, int* emin, int* exhour, int* exmin)
	{
		char v;

		printf("Enter C for car, B for bus, T for truck: ");
		scanf("%c", &v);
		printf("\nHour vehicle entered 0-24: ");
		scanf("%d", &ehour);
		printf("\nMinute vehicle entered 0-60: ");
		scanf("%d", &emin);
		printf("\nHour vehicle exited 0-24: ");
		scanf("%d", &exhour);
		printf("\nMinute vehicle exited 0-60: ");
		scanf("%d", &exmin);
		return;
	}
	void rate(int exhour, int exmin, int ehour, int emin, int* thour, int* tmin, int* round)
	{
		if(emin < exmin)
		{
			emin= emin + 60;
			exhour= exhour - 1;
		}
		*thour = ehour - exhour;
		*tmin = emin - exmin;
		if ((*tmin > 0 && *tmin <= 60))
		{
			*thour = *thour + 1;
			*round = *tmin * 0;
		}
		return;
	}
int charge(char* vehic, float* rate1, float* rate2, int ehour)
{
	switch (*vehic)
	{
	case 'c': if (ehour <= 3)
			  {
				  *rate1 = 0.00;
			  if (ehour > 3)
				  *rate2 = 1.50 * (ehour - 3);
			  }
			  break;

	case 't': if (ehour <= 2)
			  {
				  *rate1 = 1.00 * ehour;
			  if (ehour > 2)
				  *rate2 = 2.30 * (ehour - 2);
			  }
			  break;
	case 'b': if (ehour <= 1)
			  {
				  *rate1 = 2.00 * ehour;
			  if (ehour > 1)
				  *rate2 = 3.70 * (ehour - 1);
			  }
			  break;
	}
	return 0;
}
void result(int exhour, int exmin, int ehour, int emin, int thour, int round, float total)
{
	printf("\n\t\t PARKING LOT CHARGE \t\t\n");
	printf("\nType of vehicle: Car or Bus or Truck");
	printf("\nTIME-IN\t\t %d:%d", ehour, emin);
	printf("\nTIME-OUT\t\t %d:%d", exhour, exmin);
	printf("\n\t\t\t --------");
	printf("\nPARKING TIME\t\t %d:%d", thour, round);
	printf("\n\t\t\t --------");
	printf("\nTOTAL CHARGE\t\t %4.2f", total);

	return;
}

Print out your values in main() between your function calls. See where the values become bad.

This is my updated code

// Project3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>

void getData(char* vehic, int* ehour, int* emin, int* exhour, int* exmin);
void rate(int exhour, int exmin, int ehour, int emin, int* thour, int* tmin, int* round);
void charge(char vehic, float* rate1, float* rate2, int ehour);
void result(int exhour, int exmin, int ehour, int emin, int thour, float rate1, float rate2, int round, float total);

int main(void)
{
	char vehic;
	int ehour;
	int emin;
	int exhour;
	int exmin;
	int thour;
	int tmin;
	int round;

	float rate1;
	float rate2;
	float total;

	getData(&vehic, &ehour, &emin, &exhour, &exmin);
	rate(exhour, exmin, ehour, emin, &thour, &tmin, &round);
	charge(vehic, &rate1, &rate2, ehour);
	total= rate1 + rate2;
	result( exhour, exmin, ehour, emin, thour, rate1, rate2, round, total);

	return 0;
}

void getData(char* vehic, int* ehour, int* emin, int* exhour, int* exmin)
	{
		printf("Enter C for car, B for bus, T for truck: ");
		scanf("%c", &*vehic);
		printf("\nHour vehicle entered 0-23: ");
		scanf("%d", &*ehour);
		printf("\nMinute vehicle entered 0-59: ");
		scanf("%d", &*emin);
		printf("\nHour vehicle exited 0-23: ");
		scanf("%d", &*exhour);
		printf("\nMinute vehicle exited 0-59: ");
		scanf("%d", &*exmin);
		return;
	}
	void rate(int exhour, int exmin, int ehour, int emin, int* thour, int* tmin, int* round)
	{
		if(emin < exmin)
		{
			emin= emin + 60;
			exhour= exhour - 1;
		}
		*thour = ehour - exhour;
		*tmin = emin - exmin;
		if ((*tmin > 0 && *tmin <= 60))
		{
			*thour = *thour + 1;
			*round = *tmin * 0;
		}
		return;
	}
void charge(char vehic, float* rate1, float* rate2, int ehour)
{
	switch (vehic)
	{
	case 'C': if (ehour <= 3)
			  {
				  *rate1 = 0.00 * ehour;
			  }
			  else
			  {
				  *rate2 = 1.50 * ehour;
			  }

	case 'T': if (ehour <= 2)
			  {
				  *rate1 = 1.00 * ehour;
			  }
			  else
			  {
				  *rate2 = 2.30 * ehour;
			  }
	case 'B': if (ehour <= 1)
			  {
				  *rate1 = 2.00 * ehour;
			  }
			  else
			  {
				  *rate2 = 3.70 * ehour;
			  }
	}
	return;
}
void result(int exhour, int exmin, int ehour, int emin, int thour, float rate1, float rate2, int round, float total)
{
	printf("\n\t\t PARKING LOT CHARGE \t\t\n");
	printf("\nType of vehicle: Car or Bus or Truck");
	printf("\nTIME-IN\t\t\t %2d:%2d", ehour, emin);
	printf("\nTIME-OUT\t\t %2d:%2d", exhour, exmin);
	printf("\n\t\t\t --------");
	printf("\nPARKING TIME\t\t %2d:%2d", thour, round);
	printf("\n\t\t\t --------");
	printf("\nTOTAL CHARGE\t\t %4.2f\n\n", total);

	return;
}

This is my updated code

Good. Problem fixed I take it?

In your scanf statements, you are dereferencing a pointer and then referencing that, which basically does nothing. You pass pointers into getDate, so you don't need to dereference of reference anything, you can just write it like

scanf("%c", vehic);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.