I need help with my intro to C++ assignment

NOTE: This is a long post.


I have an assignment for my intro to C++ class where I need to calculate hotel room costs. I am having trouble and would like some help. My issue is that I have to calculate the different costs for single and multiple rooms. I am not really sure what to do and could use some help. I probably screwed up the math somewhere but I am not sure.



Here is the question that I am dealing with:



The cost of renting a room at a hotel is, say $100.00 per night. For special occasions, such as a wedding or conference, the hotel offers a special discount as follows.

If the number of rooms booked is:

at least 10, the discount is 10%
at least 20, the discount is 20%
at least 30, the discount is 30%
Also if rooms are booked for at least three days, then there is an additional 5% discount.

Instructions
Write a program that prompts the user to enter:

The cost of renting one room
The number of rooms booked
The number of days the rooms are booked
The sales tax (as a percent).
The program outputs:

The cost of renting one room
The discount on each room as a percent
The number of rooms booked
The number of days the rooms are booked
The total cost of the rooms
The sales tax
The total billing amount.





In my code I wrote I THINK THIS IS THE ERROR where I think the error is since I can't seem to be able to bold my text.\


Here is what I mean:

Here is my first question input:

Enter the cost of renting one room: $340
Enter the number of rooms booked: 3
Enter number of days the rooms are booked: 2
Enter sales tax(%): 9.0

And here is my output:

The cost of renting one room: $340.0
The discount on each room: 0.0%
No. of rooms booked: 3
No. of days the rooms booked: 2

Total cost of the rooms: $2040.0
Sales Tax : $183.6
Total Billing Amount : $2223.6


This works for the first question and it gives the correct answers but not for the other questions.


For the second question and beyond I am getting errors.


Here is my input for the second question.

Enter the cost of renting one room: $278
Enter the number of rooms booked: 12
Enter number of days the rooms are booked: 3
Enter sales tax(%): 8.75


Here is my output:

The cost of renting one room: $236.3 (This one needs to be 278.0, which is the same as the input)

The discount on each room: 0.2% (this one needs to be 15.0)

The rest are correct

No. of rooms booked: 12
No. of days the rooms booked: 3

Total cost of the rooms: $8506.8
Sales Tax : $744.3
Total Billing Amount : $9251.1




I also am having an error for my third question.

Input

Enter the cost of renting one room: $399
Enter the number of rooms booked: 20
Enter number of days the rooms are booked: 1
Enter sales tax(%): 9.8


Output

The cost of renting one room: $319.2 (this needs to be 399.0)
The discount on each room: 0.2% (this needs to be 15.0)

The rest are correct

No. of rooms booked: 20
No. of days the rooms booked: 1

Total cost of the rooms: $6384.0
Sales Tax : $625.6
Total Billing Amount : $7009.6


Here is my input for the fourth question

Enter the cost of renting one room: $560
Enter the number of rooms booked: 47
Enter number of days the rooms are booked: 1
Enter sales tax(%): 7.6


Here is the output that I am getting


The cost of renting one room: $392.0 (this should be 560.0)
The discount on each room: 0.3% (this should be 30.0)
No. of rooms booked: 47
No. of days the rooms booked: 1


Total cost of the rooms: $18424.0
Sales Tax : $1400.2
Total Billing Amount : $19824.2




I think that the issue is in the calculations that I made for calculating the rooms cost.


//Calculating the cost per single room

roomCost = rent - rent*totalDiscount;


In order to get the correct amount of rent for question 2 and beyond, I would need to remove the " - rent*totalDiscount; " but this would cause an error for my first question because my answer would be incorrect for the first question if I alter my code. I would like some help in order to figure out how I am able to fix this issue that I am having. I need to be able to calculate the cost of a discount for the first question but also calculate the cost of a single room for all of the other questions.



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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  #include<iostream>
using namespace std;

//Constants to store discount percent
const float discount_10=0.1;
const float discount_20=0.2;
const float discount_30=0.3;
const float additional_discount=0.05;

int main()
{

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(1);

float rent,salesTax;
int rooms,days;

//Variables to store processing items
float roomCost=0.0f;
float totalDiscount=0;
float totalCost=0;
float tax=0;
float finalBill=0;

//User input
cout<<"Enter the cost of renting one room: $";
cin>>rent;

cout<<"Enter the number of rooms booked: ";
cin>>rooms;

cout<<"Enter number of days the rooms are booked: ";
cin>>days;

cout<<"Enter sales tax(%): ";
cin>>salesTax;

//Finding out the discount as per number of rooms booked.
if(rooms>=30)
   totalDiscount = discount_30;

else if(rooms>=20)
   totalDiscount = discount_20;

else if(rooms>=10)
   totalDiscount = discount_10;

else
   totalDiscount=0;

//Finding out if additional discount is applied or not.
if(days>=3)
totalDiscount += additional_discount;

//Processing

//Calculating the cost per single room
roomCost = rent - rent*totalDiscount; (I THINK THIS IS THE ERROR)

//Total cost for all rooms
totalCost = roomCost*rooms*days;

//Finding out the tax
tax = totalCost * (salesTax/100);

//Final bill
finalBill=totalCost+tax;

//displaying the output
cout<<endl<<endl<<endl;

cout<<"The cost of renting one room: $"<<roomCost;
cout<<"\nThe discount on each room: "<<totalDiscount<<"%";
cout<<"\nNo. of rooms booked: "<<rooms;
cout<<"\nNo. of days the rooms booked: "<<days;

cout<<"\n\nTotal cost of the rooms: $"<<totalCost;
cout<<"\nSales Tax : $"<<tax;
cout<<"\nTotal Billing Amount : $"<<finalBill;

return 0;
}
Hello Alex4321,

To get you started. This link should help with the tags http://www.cplusplus.com/articles/z13hAqkS/

For your header files you should include "iomanip". This works as a manipulator to "iostream". This is a useful file that you should get use to using.

The preferred floating point type is "double". It has better precision than a float. As an example
 
constexpr double discount_30{ 0.3 };  // <--- Stores as "0.2999999999999999889" float = "0.3000000119f" 

At least using VS2017.

In "main" the first 3 "cout" statements could be replaced with:
 
std::cout << std::fixed << std::showpoint << std::setprecision(2);  // <--- Requires header file "iomanip". 

 
cout << "Enter the cost of renting one room: $";  // <--- This is the cost of 1 night not the total. 

I have not tested this yet, but I think this is more of what you want:
1
2
roomCost = rent - rent * totalDiscount; // (I THINK THIS IS THE ERROR)
// roomCost = (rent * days) - ((rent * days) * total discount); 


I will know more when I compile the program and test it.

Andy
Hello Alex4321,

Now that I have had the chance to work with the program a bit.

In the lines:
1
2
3
4

cout << "The cost of renting one room: $" << roomCost;  // <--- Choose the right variable.
cout << "\nThe discount on each room: " << totalDiscount << "%";


Given your variables:

double rent{}, salesTax;  // <--- A better name that might help could be "baseRoomCost".
double roomCost{ 0.0 };

For line 2 of the code if you want to print 20% then multiply "totalDiscount" by 100.

Andy
I agree with the above post. The tax indicated .1, .2, and .3 are 10%... can be confusing. If multiplied by 100 and adding the "%" would be helpful.



It is possible why the error is .2 instead of .15 is because the setprecision(1) is set to the first decimal place.

For the first issue, since it seems you'd like the price before the adjustments. Having two variable names for the original rent and post rent price could solve your print issue.
Last edited on
Topic archived. No new replies allowed.