/// This program that calculates the total cost of a hotel stay.
///The daily base charge is as follow:
/// Deluxe Double Room RM550
/// Deluxe Room RM400
/// Standard Room RM300
/// The hotel also charges for the following optional daily services:
/// Laundry RM20 per day
/// Extra Towel rm10 per day
/// Transport RM50 per day
#include <iostream>
using namespace std;
float calcBaseCharges,days,roomType,calcServiceCharge,laundryCost,extratowelCost,transportCost,calcTotalCharges,aseCharges,serviceCharges;
int main()
do{
cout << "How many days do you want to spend in the hotel? ";
cin >> days;
if (days < 1)
{
cout << "Please enter a valid number." << endl;
}
else
break;
}while (days >= 1);
do {
cout << "Please choose the room from the following options: " << endl;
cout << "Enter 1 for the Deluxe Double Room. " << endl;
cout << "Enter 2 for the Double Room. " << endl;
cout << "Enter 3 for the Standard Room. " << endl;
cin >> roomType;
if (roomType < 1 || roomType > 3)
{
cout << "Please enter a valid number." << endl;
}
else
break;
}while (roomType >= 1 || roomType <= 3);
switch (roomType)
{
case 1:
roomPerDay = 550;
break;
case 2:
roomPerDay = 400;
break;
case 3:
roomPerDay = 300;
break;
default:
return 1;
}
cout << "Enter 'Y' for yes or 'N' for no for the following optional services: " << endl;
do{
cout << "Do you want a laundry service during your stay? ";
cin >> laundry;
if (laundry == 'Y' || laundry == 'y')
{
laundryCost=20;
break;
}
else if (laundry == 'N' || laundry == 'n')
{
laundryCost=0;
break;
}
else
{
cout << "You entered an invalid answer. Please enter 'Y' for yes or 'N' for no. " << endl;
}}
while (laundry != 'Y' && laundry != 'y' && laundry != 'N' && laundry != 'n');
do{
cout << "Do you want an extra towel during your stay? ";
cin >> extratowel;
if (extratowel == 'Y' || extratowel == 'y')
{
extratowelCost=10;
break;
}
else if (extratowel == 'N' || extratowel == 'n')
{
extratowelCost=0;
break;
}
else
{
cout << "You entered an invalid answer. Please enter 'Y' for yes or 'N' for no. " << endl;
}}
while (extratowel != 'Y' && extratowel != 'y' && extratowel != 'N' && extratowel != 'n');
do{
cout << "Do you want transportation service during your stay? ";
cin >> transport;
if (transport == 'Y' || transport == 'y')
{
transport=50;
break;
}
else if (transport == 'N' || transport == 'n')
{
transport=0;
break;
}
else
{
cout << "You entered an invalid answer. Please enter 'Y' for yes or 'N' for no. " << endl;
}}
while (transport != 'Y' && transport != 'y' && transport != 'N' && transport != 'n');
cout << "The Total charges are RM" << totalCharges << endl;
cout << endl;
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.
/// This program that calculates the total cost of a hotel stay.
///The daily base charge is as follow:
/// Deluxe Double Room RM550
/// Deluxe Room RM400
/// Standard Room RM300
/// The hotel also charges for the following optional daily services:
/// Laundry RM20 per day
/// Extra Towel rm10 per day
/// Transport RM50 per day
#include <iostream>
usingnamespace std;
double calcBaseCharges, days, roomType, calcServiceCharge, laundryCost, extratowelCost, transportCost, calcTotalCharges, aseCharges, serviceCharges;
int main()
{
int days{}; // <--- ALWAYS initialize all your variables.int roomType{};
double roomPerDay{};
double baseCharges{};
double serviceCharges{};
double totalCharges{};
char laundry{};
char extratowel{};
char transport{};
double laundryCost{};
double extratowelCost{};
double transportCost{};
do
{
cout << "How many days do you want to spend in the hotel? ";
cin >> days;
if (days < 1)
{
cout << "Please enter a valid number." << endl;
}
elsebreak;
} while (days >= 1);
do
{
cout <<
"Please choose the room from the following options:\n"" Enter 1 for the Deluxe Double Room.\n"" Enter 2 for the Double Room.\n"" Enter 3 for the Standard Room.: ";
cin >> roomType;
if (roomType < 1 || roomType > 3)
{
cout << "Please enter a valid number." << endl;
}
elsebreak;
} while (roomType >= 1 || roomType <= 3);
switch (roomType)
{
case 1:
roomPerDay = 550;
break;
case 2:
roomPerDay = 400;
break;
case 3:
roomPerDay = 300;
break;
default:
return 1;
}
cout << "Enter 'Y' for yes or 'N' for no for the following optional services:\n";
do
{
cout << "Do you want a laundry service during your stay? ";
cin >> laundry;
if (laundry == 'Y' || laundry == 'y')
{
laundryCost = 20;
break;
}
elseif (laundry == 'N' || laundry == 'n')
{
laundryCost = 0;
break;
}
else
{
cout << "You entered an invalid answer. Please enter 'Y' for yes or 'N' for no. " << endl;
}
} while (laundry != 'Y' && laundry != 'y' && laundry != 'N' && laundry != 'n');
do
{
cout << "Do you want an extra towel during your stay? ";
cin >> extratowel;
if (extratowel == 'Y' || extratowel == 'y')
{
extratowelCost = 10;
break;
}
elseif (extratowel == 'N' || extratowel == 'n')
{
extratowelCost = 0;
break;
}
else
{
cout << "You entered an invalid answer. Please enter 'Y' for yes or 'N' for no. " << endl;
}
} while (extratowel != 'Y' && extratowel != 'y' && extratowel != 'N' && extratowel != 'n');
do
{
cout << "Do you want transportation service during your stay? ";
cin >> transport;
if (transport == 'Y' || transport == 'y')
{
transport = 50;
break;
}
elseif (transport == 'N' || transport == 'n')
{
transport = 0;
break;
}
else
{
cout << "You entered an invalid answer. Please enter 'Y' for yes or 'N' for no. " << endl;
}
} while (transport != 'Y' && transport != 'y' && transport != 'N' && transport != 'n');
cout << "The Total charges are RM" << totalCharges << '\n';
cout << endl;
//{ // <--- These {}s are not needed.
baseCharges = days * roomType;
return baseCharges;
//}
//{ // <--- These {}s are not needed.
serviceCharges = days * (laundryCost + extratowelCost + transportCost);
return serviceCharges;
//}
totalCharges = baseCharges + serviceCharges;
return totalCharges;
}
In the 1st do/while your condition is: while (days >= 1);. So if I enter 4 the loop will continue until you enter (0)zero or a negative number. What you want is more like while (days < 1);.
The 2nd do/while is the same. A proper choice will keep you in the loop. What you want is an invalid choice to stay in the loop.
In line 147 you output the total changes, but up to this point "totalCharges" has never received a value and if you did not initialize the variable when you will either an error or a garbage value sent to the screen.
Lines 152, 157 and 162 all return a value, but since the only function is "main" the return statements leave the program.
In line 160 "totalCharges" finally gets a value, but it is to late to use it other than to see what the program returns when it ends. Also the values being returned in lines 152, 157 and 162 are "double"s, but "main" returns an "int".
#include <iostream>
#include <limits>
usingnamespace std; // <--- Best not to use.
// <--- Try to avoid using global variables. Also these are defined in "main" and never used.
//double calcBaseCharges, days, roomType, calcServiceCharge, laundryCost, extratowelCost, transportCost, calcTotalCharges, aseCharges, serviceCharges;
int main()
{
int days{}; // <--- ALWAYS initialize all your variables.int roomType{};
double roomPerDay{};
double baseCharges{};
double serviceCharges{};
double totalCharges{};
char laundry{};
char extratowel{};
char transport{};
double laundryCost{};
double extratowelCost{};
double transportCost{};
while (std::cout << "How many days do you want to spend in the hotel? " && !(cin >> days) || days < 1)
{
if (!std::cin)
{
std::cerr << "\n Invalid Input! Must be a number.\n\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
}
elseif (days < 1)
{
std::cerr << "\n Please enter a valid number.\n\n";
}
}
Working on your code is done a little at a time just like the way you should have written it.
I changed the do/while loop to a while loop which works better. Also since "days" is defined as a numeric variable entering a non numeric value will cause "cin" to fail and be unusable until it is fixed, the if statement in the loop.
This type of while loop is something that you should get use to early.
Following this part I adjusted the next "cout" to:
1 2 3 4 5 6 7 8
cout <<
"\n"" Please choose the room from the following options:\n"" Enter 1 for the Deluxe Double Room.\n"" Enter 2 for the Double Room.\n"" Enter 3 for the Standard Room.\n"" Enter choice: ";
cin >> roomType;
The spaces at the beginning of the strings are not necessary, but I find it easier to read this way.
The next do/while should look similar to the 1st one.
After the switch you need to have totalCharges = days * roomPerDay; and after that totalCharges += ???; as you add additional charges.
Then when each part is working you can put the whole thing in a do/while or while loop.
Something strange about those globals are the ones starting with "calc" which sound like the names of the missing function heads, and the other's are the needed parameters. Strange.
int main()
{
constexprdouble
DD_ROOM_COST { 550.0 },
DBL_ROOM_COST{ 400.0 },
STD_ROOM_COST{ 300.0 },
LAUNDRY_COST { 20.0 },
TOWEL_COST { 10.0 },
TRANS_COST { 50.0 };
// <--- These are no longer needed.
//double laundryCost{};
//double extratowelCost{};
//double transportCost{};
The 2nd while loop is a different format:
1 2 3 4 5 6 7 8 9
while
(cout <<
"\n"" Please choose the room from the following options:\n"" Enter 1 for the Deluxe Double Room. @RM" << DD_ROOM_COST << "\n"" Enter 2 for the Double Room. @RM" << DBL_ROOM_COST << "\n"" Enter 3 for the Standard Room. @RM" << STD_ROOM_COST << "\n"" Enter choice: " &&
!(cin >> roomType) || roomType < 1 || roomType > 3)
Notice how I used the constant variables to show the cost of each type of room. This way if any of the prices change you only have 1 place to change them.
Following the switch I added: totalCharges = days * roomPerDay;.
In the next parts the do/while loops work, so I left them alon with minor changes:
cout << "\n Enter 'Y' for yes or 'N' for no for the following optional services:\n";
do
{
cout << "\n Do you want a laundry service during your stay? (RM" << LAUNDRY_COST << "): ";
cin >> laundry;
if (laundry == 'Y' || laundry == 'y')
{
totalCharges += LAUNDRY_COST;break;
}
elseif (laundry == 'N' || laundry == 'n')
{
//laundryCost = 0; // <--- This line is not needed.
break;
}
else
{
cerr << "\n You entered an invalid answer. Please enter 'Y' for yes or 'N' for no.\n";
}
} while (laundry != 'Y' && laundry != 'y' && laundry != 'N' && laundry != 'n');
When you finish all the do/while loops "totalCharges" will have the proper value.
This is 1 possible test run:
How many days do you want to spend in the hotel? a
Invalid Input! Must be a number.
How many days do you want to spend in the hotel? 0
Please enter a valid number.
How many days do you want to spend in the hotel? 4
Please choose the room from the following options:
Enter 1 for the Deluxe Double Room. @RM550
Enter 2 for the Double Room. @RM400
Enter 3 for the Standard Room. @RM300
Enter choice: a
Invalid Input! Must be a number.
Please choose the room from the following options:
Enter 1 for the Deluxe Double Room. @RM550
Enter 2 for the Double Room. @RM400
Enter 3 for the Standard Room. @RM300
Enter choice: 0
Please enter a valid number.
Please choose the room from the following options:
Enter 1 for the Deluxe Double Room. @RM550
Enter 2 for the Double Room. @RM400
Enter 3 for the Standard Room. @RM300
Enter choice: 6
Please enter a valid number.
Please choose the room from the following options:
Enter 1 for the Deluxe Double Room. @RM550
Enter 2 for the Double Room. @RM400
Enter 3 for the Standard Room. @RM300
Enter choice: 2
Enter 'Y' for yes or 'N' for no for the following optional services:
Do you want a laundry service during your stay? (RM20): y
Do you want an extra towel during your stay? (RM10): y
Do you want transportation service during your stay? (RM50): y
The Total charges are RM1680
Press Enter to continue: