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;
				
}

Dani AI

Generated

The concrete problems in the posted program are already spotted by and : the total-calculation function is being called with the wrong value, the helper resets or ignores the hours value, and a result variable is left effectively uninitialized. Those fixes are necessary but not sufficient; the following checklist closes the gaps you’ll hit next.

Make the calculation function a pure calculator: accept the number of hours (not the running total), declare and initialize a local total variable, compute the fee from the passed hours, then return that total. Do input validation in main (loop until hours is in the allowed range) instead of trying to read inside the calculator. Be explicit about the billing boundaries: treat 0 or negative and >24 as invalid; treat up-to-3 hours as the flat fee; treat 4–18 as incremental at the stated per-hour rate; cap at the maximum for the 19–24 range.

Practical tidy-ups that avoid future bugs: assign the function’s return to main’s total variable before storing it in your array; check row < capacity before writing; simplify array writes (assign fixed column indices instead of incrementing a column counter); print the heading once before the input loop unless every-row headings are intentional. Read the continue choice with a format or method that skips leftover newlines (or use fgets+parse), and compile with warnings enabled (-Wall -Wextra) to catch uninitialized variables and prototype mismatches.

Debug tips: add a temporary trace print that shows the hours passed into the calculator and the returned total, then remove it once correct. Converting the parallel arrays into a small struct array for each customer will also make the code clearer and reduce indexing errors.

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.