this is the simple problem I'm working on.

A parking garage charges a $2.00 minimum fee to park for up to three hours. The garage charges an additional $0.50 per hour for each hour or part thereof in excess of three hours. The maximum charge for any given 24-hour period is $10.00. Assume that no car parks for longer than 24 hours at a time. Write a program that will calculate and print the parking charges for each of 3 customers who parked their cars in this garage yesterday. You should enter the hours parked for each customer. Your program should print the results in a neat tabular format. The program should use the function calculateCharges to determine the charge for each customer.

so far this is what I have been able to come up with when i run a syntax check a few errors pop up and I think its just something either I'm not understanding properly or Just don't see it.

these are the errors reported
8 C:\Users\peter\Desktop\petes programs\parking garage\main.cpp `rate' undeclared (first use this function)
#include <iostream>

27 C:\Users\peter\Desktop\petes programs\parking garage\main.cpp expected unqualified-id before '{' token

27 C:\Users\peter\Desktop\petes programs\parking garage\main.cpp expected `,' or `;' before '{' token

#include <iostream>

using namespace std;
double calculatecharges(double);

int main(int argc, char *argv[])
{
double hours; rate;
int x;
x=0;

do
{ cout<<"Enter number of hours parked: ";
cin>> hours;
rate = calculatecharges(double hours);
cout<< " your parking fee is :" << rate << endl;
x++;
} while (x<4) ;


system("PAUSE");
return EXIT_SUCCESS;
}

double calculatecharges(double hours) ;

{
double base_rate = 2;
double long_rate = .50;
double max_rate= 10;
if (hours>3)
rate=base_rate;
else if (hours>3)
rate=((hours-3)*long_rate + base_rate);
else (hours>11)
rate = max_rate;
}

Recommended Answers

All 10 Replies

#include <iostream>
using namespace std;

double calculatecharges(double);

int main(int argc, char *argv[])
{
     //Syntax Error Here
     //double hours; rate;

     double hours, rate;
     int x = 0;

     do 
     { 

          cout<<"Enter number of hours parked: ";
          cin>> hours;

          rate = calculatecharges(double hours);

          cout<< " your parking fee is :" << rate << endl;

          x++;

      }while(x < 4); 

     system("PAUSE");

return EXIT_SUCCESS;
}


//Syntax Error Here
//Only function prototypes get a semicolon
//double calculatecharges(double hours) ;

double calculatecharges(double hours) 
{
     double base_rate = 2;
     double long_rate = .50;
     double max_rate= 10;

     if (hours>3)

          rate=base_rate;

     else if (hours>3)

          rate=((hours-3)*long_rate + base_rate);

     else (hours>11)

          rate = max_rate;
}

Hello Peter, I believe you are simply missing a header file.

Please use code tags

around your code.

double hours; rate;

separate on the same line by a comma: double hours, rate; or if you want do double hours; double rate;

rate = calculatecharges(double hours);

don't need to give the variable type when calling the function
(but you do in the declaration or definition

double calculatecharges(double hours) ;

should not have a semicolon after it

rate=base_rate;
       else if (hours>3)
       rate=((hours-3)*long_rate + base_rate);
       else (hours>11)
       rate = max_rate;
       }

Your function returns a double but you are not returning anything in this case. Also, the rate in your main() is not in scope here (reread that section of your text if you're not sure why). You should calculate it in here and then return it, and you've already got it set up correctly in main.

thanks a bunch that helped a lot now it runs and its looping. theres just an error of shorts in my calculation because no matter what i enter the "rate" spews out letters.

Because if you have no return value for your function you are getting whatever garbage was in rate to begin with

Because if you have no return value for your function you are getting whatever garbage was in rate to begin with

double calculatecharges(double hours) 
    { 
      double rate;                      
      double base_rate = 2;
      double long_rate = .50;
      double max_rate= 10;
       if (hours<3)
       rate=base_rate;
       else if (hours>3)
       rate=((hours-3)*long_rate + base_rate);
       else (hours>11);
       rate = max_rate;
       return rate;
       }

ok so now it returns a value but its always 10 and from what i understand from going back and reading all functions should terminate execution with a return statement that includes an expression indicating the value to be returned. so if im understanding this correctly now the problem is with my calculation ?

You've stuck a ; on the end of your else statement in your function, so it's interpreting the rate = max_rate; in isolation. So that's what your function is returning each time.

double calculatecharges(double hours) 
    { 
      double rate;                      
      double base_rate = 2;
      double long_rate = .50;
      double max_rate= 10;
       if (hours<=3)
       rate=base_rate;
       else if (hours>3)&&(hours>11)
       rate=((hours-3)*long_rate + base_rate);
       else if(hours>=11)
       rate=max_rate;
       return rate;
       
       }

ok so as long as im using my and logic right this should work but the compiler is telling me i need a ; after else if (hours>3)&&(hours>11) but if i did that it would give me the same problem i had before

you can take the parens off of the hours>3 and hours>11 individually and put a set around the whole thing

#include <iostream>

using namespace std;
double calculatecharges(double);

int main(int argc, char *argv[])
{
    double hours, rate;
    int x;
    x=0;
    
    do 
    { cout<<"Enter number of hours parked: ";
    cin>> hours;
    rate = calculatecharges(hours);
    cout<< " your parking fee is :" << rate << endl;
    x++;
    } while (x<3) ;  
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

double calculatecharges(double hours) 
    { 
      double rate;                      
      double base_rate = 2;
      double long_rate = .50;
      double max_rate= 10;
       if (hours<=3)
       rate=base_rate;
       else if (hours>3 && hours<11)
       rate=((hours-3)*long_rate + base_rate);
       else if(hours>11)
       rate=max_rate;
       return rate;
       
       }

thanks for all the help it works now =p

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.