Need to store a constant even though its a changing variable??

Sorry, I do not know how to fully use this website
anyways, this code is correct on my task which is:
"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. "

But the only problem with it is the website says I need to store my discount as a const variable, but the discount changes based on my if else statement so how do I store it as one?
I looked through my text and googled and had no luck.
I through the declaration statements at the top the "const double" to see if it would work, but no it didn't... sadly lol.

thank you.



#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main() {

double roomCost, discount, dRoomCost, totalCostOfRooms, afterRoomCost;
double const extraDiscount = 0.05;
double dAfterRoomCost;
double salesTax;
double total;
const double HTL_SP_DISCOUNT_10 = 10;//10%
const double HTL_SP_DISCOUNT_20 = 20;//20%
const double HTL_SP_DISCOUNT_30 = 30;//30%
int noOfRooms, noOfDays;

cout << showpoint << fixed << setprecision (2);

cout << "Please enter the cost of one room: ";
cin >> roomCost;

cout << "Please enter the number of rooms booked: ";
cin >> noOfRooms;

cout << "Please enter the number of days booked: ";
cin >> noOfDays;

cout << "Enter the sales tax (as a percent): ";
cin >> salesTax;

salesTax = salesTax / 100;

if (noOfRooms >= 10 && noOfRooms < 20)
discount = 0.10;
else if (noOfRooms >= 20 && noOfRooms < 30)
discount = 0.20;
else if (noOfRooms >= 30)
discount = 0.30;
else
discount = 0;



if (noOfDays >=3)
discount = discount + extraDiscount;
else
discount = discount;

dRoomCost = roomCost * discount;
afterRoomCost = roomCost - dRoomCost;

afterRoomCost = afterRoomCost - dAfterRoomCost;

cout << "\nThe cost of renting one room is: $ " << roomCost << endl;

discount = discount * 100;
cout << "And the discount is " << discount << "%" << endl;

cout << "\nThe number of rooms booked is: " << noOfRooms << endl;
cout << "And the number of days booked is: " << noOfDays << endl;

totalCostOfRooms = (noOfRooms * afterRoomCost) * noOfDays;

cout << "The total cost of the room(s): " << totalCostOfRooms << endl;

salesTax = totalCostOfRooms * salesTax;

cout << "The sales tax is: $ " << salesTax << endl;

total = totalCostOfRooms + salesTax;

cout << "The total bill is: $ " << total << endl;




return 0;
}
Last edited on
You can use a function
1
2
3
4
5
6
double get_discount(int& noOfDays, int& extraDiscount) {
	if (noOfDays >= 3) {
		discount += extraDiscount
	}
	discount *= 100;
}

and then keep your variable as const
const double discount = get_discount(noOfDays, extraDiscount);
you can make a const double discount[] = {values...} and create a function of num rooms & num days to get the correct index into the table.
Topic archived. No new replies allowed.