Please help me to solve this problem .(C++ Calculation)

Question:

A parking garage charges RM 0.50 per minute fee. Write a program that prompts user to input the number of hours and minutes parked. The program will then calculate and display the total minutes parked and the charges to be paid by customer based on the basic fee given. If the charges is greater than RM80.00, discount 10% is given to the customer.

This is my coding, please help me to solve this prob.TQ guys.


#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

double calculateCharge(double a, double b);
int main()

double num=0;
double num1=0;
double total=0;
cout<<"\t\t********************************************************"<<endl;
cout<<"\t\t\t Welcome to parking garage system"<<endl;
cout<<"\t\t********************************************************"<<endl<<endl<<endl;



cout<<"\t\tPlease enter first customer's hours parked:";
cin>>num;
cout<<"\t\tPlease enter first customer's minutes parked:";
cin>>num1;
cout<<endl<<endl;
cout<<"\t*****************************************************************"<<endl<<endl<<endl;
cout<<setw(5) << "Car" << setw(12) << "Hours" <<setw(12) "Minutes" << setw(16) << "Charge"
<< endl;

cout << setw(5) << num << setw(14) << num1 <<setw(16) << fixed << setprecision(2)
<< calculateCharge (num, num1) << endl;

total = ( calculateCharge (num, num1) );
cout<<setw(5) << "TOTAL" << setw(14) << num << setw(16) << fixed <<
setprecision(2) << total
<< endl;

system("pause");
return 0;
}//end main




double calculateCharge(double x, double y)
{
double charge;

if ( (x <= 2) && (y >= 40) )
charge = ( (x * 30) + (y * 0.5) );

else if ( (x > 2) && (y > 40) )
charge = ((x * 30) + (y * 0.5) );

return charge;

}

your main has no opening bracket "{"
oh ok thanks..how about the full coding.. i can't run this prog by visual basic c++ 2008
I will not write the whole code for you.
oh ok..
Can you edit your main post and add the code tags? You just need to select all the code and while it's selected press the <> button on the right (It should be the topleft button)
ok i'll do it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream> 
#include <iomanip>
#include <math.h>

using namespace std;

double calculateCharge(double a, double b);
int main()
{
double num=0;
double num1=0;
double total=0;
cout<<"\t\t********************************************************"<<endl;
cout<<"\t\t\t Welcome to parking garage system"<<endl;
cout<<"\t\t********************************************************"<<endl<<endl<<endl;



cout<<"\t\tPlease enter first customer's hours parked:";
cin>>num;
cout<<"\t\tPlease enter first customer's minutes parked:";
cin>>num1;
cout<<endl<<endl;
cout<<"\t*****************************************************************"<<endl<<endl<<endl;
cout<<setw(5) << "Car" << setw(12) << "Hours" <<setw(12) "Minutes" << setw(16) << "Charge"
<< endl;

cout << setw(5) << num << setw(14) << num1 <<setw(16) << fixed << setprecision(2) 
<< calculateCharge (num, num1) << endl;

total = ( calculateCharge (num, num1) );
cout<<setw(5) << "TOTAL" << setw(14) << num << setw(16) << fixed << 
setprecision(2) << total
<< endl;

system("pause");
return 0;
}//end main 
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
double calculateCharge(double x, double y)
{
double charge;

if ( (x <= 2) && (y >= 40) )
charge = ( (x * 30) + (y * 0.5) );

else if ( (x > 2) && (y > 40) )
charge = ((x * 30) + (y * 0.5) );

return charge;

}


x = hours
y = minutes
Ok, but, what is the problem? What's not working? (I'm going to test this program right now huh) The only thing I can tell you right now that can help you is this:

1
2
3
4
5
double calculateCharge(double x, double y)
{
// double charge; ---> this variable is not initialized. Under certain circumstances
// you may end up with an unknown return value. So initialize this to 0.0:
double charge = 0.0;


Also here you have a mistake on line 25:
 
...<<setw(12) "Minutes" ... // you are missing a "<<" here between setw and "Minutes". 


Also, did you write the following things because your teachers did teach you?

I mean these:
1
2
3
4
5
// beginning:
#include <math.h>

// ending of main()
system("pause");


If your teacher teached you these two things, you are probably having a bad teacher.
in C++ you should do such things differently:

1
2
3
4
5
6
7
// beginning: remove "#include <math.h>" and add:
#include <cmath>

// ending of main(): remove system("pause") and add:
cout<<"\nPress a Key to Exit.";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.get();


The first thing, about including cmath, isn't really important because of something, but, as you are on C++, you shouldn't use C headers.

math.h is C, cmath is C++.
the system() thing is a REALLY important thing. It is REALLY risky and you should NEVER, NEVER use it. It also "flags" your executable as a virus.
See here for more informations: http://www.cplusplus.com/forum/articles/11153/
Last edited on
TQ so much!

I'm done :)
Topic archived. No new replies allowed.