helpp meee

help me with my assignment.

The renting car company asked you to develop a program that will calculates the total charges of the rental. The charges based on car type and duration. A discount will be given if the duration exceed 18 hours. Below is the table shows the charges:
Car Type
Sedan (S) , RM20/hour , 10% if >18 hours
Luxury (L) , RM35/hours , 15% if >18 hours
MPV (M) , RM45/hours , 20% if >18 hours

You are required to the following:
a. Write a function named displayCar () that will displays the table above.
b. Write a function named carRate() that will receive car type and return the car rate
per hour.
c. Write a function named totalCharge() that will receive the car rate and duration. The
function will return the total.
d. Write a function named displayNetCharge() that will receive the total charge and duration.

The function will display the net charge after calculate the discount (if applied) as shown below:
Customer Name : JIMNI
License Number : 900909091234
Car Type : S
Duration : 20
Total Charge : RM 400.00
% Discount (if applied) : 10%
Net Charge : RM 360.00

e. The main() will prompt user to input :
i. Customer name
ii. Customer ic number (format 800112081234)
iii. The car type
iv. The duration (in hours)
f. The program will stop if the user enters value N or value n.

Once the program stop, program will produced the daily sales report as shown below:
Car Type
Sedan , quantity = 2 , sales =RM200
Luxury, quantity = 1 , sales =RM140
MPV ,quantity = 1 , sales =RM90
Last edited on
What have you tried so far?
so I've tried this so far;

#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

int carRate(char);
int totalCharge(int,int);
void displayCar()
{
cout<<"--------------------------------------------------------------------------"<<endl;
cout<<" Car Type "<<setw(30)<<"Rate per hour(RM/h)"<<setw(30)<<"Discount(>18 hours)"<<endl;
cout<<"--------------------------------------------------------------------------"<<endl;
cout<<" Sedan (S)"<<setw(20)<<"20"<<setw(30)<<"10%"<<endl;
cout<<" Luxury (L)"<<setw(19)<<"35"<<setw(30)<<"15%"<<endl;
cout<<" MVP (M)"<<setw(22)<<"45"<<setw(30)<<"20%"<<endl;
cout<<"--------------------------------------------------------------------------"<<endl;
}

int carRate(char type)
{

int rate;
if(type=='S')
{
rate=1*20;
return rate;
}
else if (type=='L')
{
rate=1*35;
return rate;
}
else if(type=='M')
{
rate=1*45;
return rate;
}
}

int totalCharge(int rate,int duration)
{
double total;
if(rate=20)
{
total=rate*duration;
return total;
}
else if(rate=35)
{
total=rate*duration;
return total;
}
else if(rate=45)
{
total=rate*duration;
return total;
}
}

int displayNetCharge()


int main()
{
char name[50],type[2],choice;
int id,duration,rate;
double total,netcharge;
cout<<"\t\t***WELCOME TO COVID RENTING CAR COMPANY***"<<endl;
cout<<"\n\n";
displayCar();
cout<<"\n\n";
cout<<"Customer Name :";
cin.get(name,50);
cout<<"License Number :";
cin>>id;
cout<<"Car Type :";
cin>>type;
cout<<"Duration (in hours):";
cin>>duration;
cout<<"\n\n";
carRate(*type);
totalCharge(rate,duration);

cout<<"Press enter to continue and press N or n to exit"<<endl;
cin>>choice;
}


Please edit your post and add code tags for formatting.
[code]
    { your code here }
[/code]

And what specifically is the issue currently with your program?

A few things:
(1) your carRate and totalCharge functions don't always return a value. I suggest having a "return 0;" at the ends of them in case invalid control flow reaches that point.
(2) displayNetCharges should "receive the total charge and duration", similar to how totalCharge receives parameters.
(3) The = sign is assignment. == checks for equality. Look at your if statements and amend them.
(4) Notice that in, for example, totalCharge, you're doing the same exact thing in every if statement body. This could just be simplfied to:
1
2
3
4
int totalCharge(int rate, int duration)
{
    return rate * duration;
}

(5) Your carRate function returns a value, but you don't use that value in main.
(6) Your totalCharge function returns a value, but you don't use that value in main. You need to put that return value in a variable in main, and then pass it to the displayNetCharge function which you will call from main once you write it.
(7) You declare double total,netcharge; which makes them doubles, but your corresponding functions return ints. If a value can be a "decimal" (for example, 3.5) then that value needs to be stored in a double.

For example, if your duration is 1 hour, and your type is Luxury (L), the rate per hour is 35%, which means 1 * 0.35. This value has to be stored as a double, not an int, or else it will truncate to 0.
https://stackoverflow.com/questions/23555019/difference-between-int-and-double
Last edited on
Hello lexyyy98,
In addition to what Ganado has said.


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



The line #include <math.h> should be "cmath", but there is nothing in the program tat would require "cmath". For the operators (*, /, +, -) "cmath" or even "math.h" is not required.

What you could make use of is #include <string> .

In "main" your variables would work better as:
1
2
3
4
char type{};  // <--- Since you only need 1 letter.
double duration{}, rate{}, total{}, netcharge{};  // <--- Best to initialize your variables.
std::string id;  // <--- Empty when defined.
std::string name;


"main is good down tocout << "Car Type :";. Does the program come with source code so that the user can figure out what to enter? If you want the user to enter "S", "L" or "M" then say so or you may get more than you need. Also storing this in a 2 element character array if the user enters "Sedan". The "Se" will be stored in the array, but there will be no "\0" to mark the end.

As a "char" the "S" will be stored in the variable and you will need to clear the input buffer of anything that is left before the next input.

A good prompt with an example or better instructions is a great help.

Another option would be to define a "std::sting" for input and set type = str[0];. Or type = std::toupper(str[0]); to change the character to upper case if needed. The "std::toupper()" would work in other forms. This requires the header file "cctype".

Unless you need to do some math on "id" this would work better as a "std::string" for just input and output. Otherwise a number like "900909091234" would need a " long long" as a 4 byte "int" will store " 2,147,483,647" and this may be implementation dependent. Trying to store "900909091234" in an "int" is likely to end up with a very large negative number that makes no sense.

If you define type as a "char" or a "string" you will not need to de-reference the variable to send it to the function.

In the function "carRate" you could replace the if/else if statements with a switch and you could use only 1 return statement. Or as Ganado suggested have the function return 0 if none of the if/else if statements would be bypassed.

When I tried to compile your code I received 10 errors and 3 warnings. You should compile your code before posting and ask about any errors that you do not understand. It is also best to post the complete error message and not what you think it means.

Andy
Hello lexyyy98,

Now that I have had the chance to run the program most of those 10 errors were my fault. Changes I made, but did not finish. Sorry about that.

The warnings are still valid.

Andy
Topic archived. No new replies allowed.