Thread: function error

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    23

    function error

    This is the program

    A parking garage charges $2 for minimum fee to park up to 3 hours and then a $.50 per hour charge after the 3rd hour. write a program that calculates and prints the parking charge for each of # customer. Result should be printed in tabular format. the program should use the function calculateCharges to determine the charge for each customer.

    here is my code but i get some compile errors and i do not see where there is a mistake

    Code:
    #include <stdio.h>
    
    double calculateCharges (double hours, double charge);
    
    int main()
    {
    	
    	int cars;
    	double totalHours = 0;
    	double totalCharge = 0;
    
    
    	printf ("Please enter the number of cars parked for the day:\n");
    	scanf  ("%d", &cars);
    
    	if (cars >= 1){		
    
    	printf ("Car\tHours\tCharge\n");
    	printf ("%d\t%.1f\t%.2f\n", cars, calculateCharges(hours, charge));
    	totalHours = totalHours + hours;
    	totalCharge = totalCharge + charge;
    	cars--;
    	}
    
    	if (cars == 0){
    
    		printf ("Total\t%.1f\t%.2f\n", totalHours, totalCharge);
    	}
    
    	
    	
    
    	return (0);
    }
    
    double calculateCharges (double hours, double charge)
    {
    	double minRate = 2.00;
    	double rate = 0.50;
    
    	printf ("please enter cars total hours:");
    	scanf  ("%.2f", &hours);
    
    	if (hours < 3) {
    		charge = minRate;
    	}
    
    	else {
    		charge = minRate + (rate * (hours -3));
    	}
    
    	return;
    }
    here is my compiler errors
    Code:
    -------------------Configuration: parking - Win32 Debug--------------------
    Compiling...
    parking.c
    C:\C Files\Chapter_5\parking\parking.c(19) : error C2065: 'hours' : undeclared identifier
    C:\C Files\Chapter_5\parking\parking.c(19) : error C2065: 'charge' : undeclared identifier
    C:\C Files\Chapter_5\parking\parking.c(52) : warning C4033: 'calculateCharges' must return a value
    Error executing cl.exe.
    
    parking.obj - 2 error(s), 1 warning(s)
    thanks,
    joe

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    23
    ok i've played around with my code and here is the new code without any errors
    Code:
    #include <stdio.h>
    
    double calculateCharges (double charge);
    
    int main()
    {
    	
    	int cars;
    	double charge = 0;
    	double hours = 0;
    	double totalHours = 0;
    	double totalCharge = 0;
    
    
    	printf ("Please enter the number of cars parked for the day:\n");
    	scanf  ("%d", &cars);
    
    	if (cars >= 1){	
    	
    		
    
    	printf ("Car\tHours\tCharge\n");
    	printf ("%d\t%.1f\t%.2f\n", cars, calculateCharges(hours, charge));
    	totalHours = totalHours + hours;
    	totalCharge = totalCharge + charge;
    	cars--;
    	}
    
    	if (cars == 0){
    
    		printf ("Total\t%.1f\t%.2f\n", totalHours, totalCharge);
    	}
    
    	
    	
    
    	return (0);
    }
    
    double calculateCharges (double charge)
    {
    	double minRate = 2.00;
    	double rate = 0.50;
    	double hours;
    
    	printf ("please enter cars total hours:");
    		scanf  ("%.2f", &hours);
    
    	
    
    	if (hours < 3) {
    		charge = minRate;
    	}
    
    	else {
    		charge = minRate + (rate * (hours -3));
    	}
    
    	return charge;
    }
    now i'm susposed to have a tabulated foramt display something like this if say i had only 2 cars park for the day.

    cars hours charge
    1 2 2.00
    2 5.5 4.50
    total 7.5 6.50

    all i get is this

    Please enter the number of cars parked for the day:
    2
    Car Hours Charge
    please enter cars total hours:2
    2 2.0 0.00
    Press any key to continue

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    totalHours = totalHours + hours;
    	totalCharge = totalCharge + charge;
    Variables undeclared. you got declare that some where in your main. By looking a your code. I guess u wanted to do some value by reference things??


    Code:
    return charge;
    your calculateCharges function should return a value. In your case it should return charge.

    ssharish2005

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    calculateCharges(hours, charge));
    what's this. Dint you get a compiler error. U got to send two arguments to your function.

    and for reading more than one car details, u got use a loop and array to achive that. Array is to hold more than one car details.

    ssharish2005
    Last edited by ssharish2005; 12-09-2006 at 05:23 PM.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    23
    ssharish2005
    amazingly I didn't get one

    i think i understand what your saying about an array but how would one write this in order to keep all the values i need to print the correct tabulated format after all pertian information is entered?????

    thanks,
    joe

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    #include<stdio.h>
    
    int main()
    {
        printf("%-10d%-10d%-10d\n",10,10000,10000);
        printf("%-10d%-10d%-10d",100000,10,1000);
        
        getchar();
        return 0;
    }
    
    /*my output
    10        10000     10000
    100000    10        1000
    */
    hope this helps you

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM