I am doing an extra assignment for my CSCI class. The assignment asks to create a program that calculates hours for customers in a parking garage. i wrote my program out and now i am trying to break it down into custom functions. i was doing fine until i got to te function to clculate total. It's not returning total when i run the program it just prints zero. can anyone help here is my code.

#include <stdio.h>




void PrintHeading(void);
void printvalues (int,int,float);
float calctotal (float);


int main (void)
{
	int cnt=0;//counter
	int hours;//hours
	float total=0;//Total amount to pay
	char choice;//user choice for continuing
	int array1[10][2] = {0}; // holding customer number and hours
	float array2[10]={0};// holding customer total
	int row = 0, column = 0;
	//array indices
	


	do{
	//Assigning customer number                                   
	cnt++;
	//accepting # of hours
	printf("Please enter number of hours:");
	scanf("%d", &hours);

	
                calctotal(total);
				PrintHeading();
				printf("\n");
					//print heading
	//storing customer info into array
	array1[row][column++] = cnt;
	array1[row][column] = hours;
	array2[row] = total;
	row++;
	column = 0;

	printvalues(cnt,hours,total);
	printf("\n\n");

	printf("\nWould you like to continue? y for yes n for no.\n");
	scanf("%*c%c", &choice);

	printf("\n");


	}while(choice == 'y'|| choice == 'Y');

		return 0;
}

void PrintHeading(void)
{
	printf("\n\n%s%10s%15s", "Customer Number", "Hours", "Total Charge");


}

void printvalues (int cnt,int hours,float total)
{
	printf("%d%21d%10c%-10.2f", cnt, hours,'$', total);
}

float calctotal(float total)
{
	
	int hours=0;
	
	

	if (hours < 3 && hours > 0)
		total = 2;
	else
		if (hours >= 19 && hours <= 24)
			total = 10;
		else
			if (hours>3 && hours < 19)
				total = (hours-3) * .5 + 2;
			else
				if (hours > 24){
				printf("invalid input......try again!\n");
				scanf("%d",&hours);
				
	}
				return total;
				
}

Recommended Answers

All 4 Replies

Put your return statement in your function, not outside. Or what is that curly brace doing before it?

Its just the end brace for the last if statement. i had to put it there so the if statement would read the printf and the scanf. i tried what you said earlier but it didnt work maybe I did it wrong ill check.

Sorry, I thought the last brace was the end of your main function.
Did you not interchange the vars hours and total?
Should
scanf("%d", &hours);
calctotal(total);

not be more something like
scanf("%d", &hours);
total=calctotal(hours);
?

That would be the correct way to call it. The function would still not work as planned, as you set hours to zero inside calctotal. The function itself should be something like:

float calctotal(float hours)
{
	
	// got rid of: int hours=0;
	
	

	if (hours < 3 && hours > 0)
		total = 2;
	else
		if (hours >= 19 && hours <= 24)
			total = 10;
		else
			if (hours>3 && hours < 19)
				total = (hours-3) * .5 + 2;
			else
				if (hours > 24){
				printf("invalid input......try again!\n");
				scanf("%d",&hours);
				
	}
				return total;
				
}
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.