So I need help writing this C++ program on visual studio 2010, I got as far as I
ID: 3546306 • Letter: S
Question
So I need help writing this C++ program on visual studio 2010, I got as far as I could but I can not seem to get it write. I have a copy of the code I have written so far and a copy of what the final program should look like, thanks.
My code so far....
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main ()
{
const int Max_Guests = 20;
//arrays
char RoomNumberArray[Max_Guests];
char GuestNameArray[Max_Guests];
double RoomChargeArray[Max_Guests];
double MealChargeArray[Max_Guests];
double TotalChargeArray[Max_Guests];
int NumberOfGuests;
char RoomNumber;
char Name;
double RoomCharge;
double MealCharge;
int TotalNumberOfGuests = 0;
double TotalRoomCharges = 0;
double TotalMealCharges = 0;
double TotalCharge = 0; //individual guest total?
double GrandTotal = 0;
cout << "Enter the number of guests: ";
cin >> NumberOfGuests;
for (int x= 0; x < NumberOfGuests; x++)
{
cout<< "Enter the room number for guest #:" << (x+1) << ": ";
cin>> RoomNumber;
cout<< "Enter the name for guest #:" << (x+1) << ": ";
cin>>Name;
cout<< "Enter the room charge for guest #:" << (x+1) << ": ";
cin>> RoomCharge;
cout<< "Enter the meal charge for guest #:" << (x+1) << ": ";
cin>> MealCharge;
char RoomNumberArray[x] = RoomNumber;
char GuestNameArray[x] = Name;
double RoomChargeArray[x] = RoomCharge;
double MealChargeArray[x] = MealCharge;
double TotalChargeArray[x] = TotalCharge;
TotalRoomCharges += RoomCharge;
TotalMealCharges += RoomCharge;
}
GrandTotal= TotalRoomCharges - TotalMealCharges;
cout<< endl;
cout << "ROOM#"
<< setw(14) << "CUST NAME"
<< setw(14) << "ROOM CHG"
<< setw(14) << "MEAL CHG"
<< setw(14) << "TOTAL CHG" << endl;
cout << "_______________________________________________________________" <<endl;
for (int x = 0; x< NumberOfGuests; x++)
{
cout << "#" << (x + 1)
<< setw (6) << RoomChargeArray[x]
<< setw(7) << fixed << setprecision(2)
<< RoomChargeArray[x]
<< setw (8) << fixed
<< setprecision(2) << MealChargeArray [x]
<< setw (8) << fixed
<< setprecision(2)
<< TotalChargeArray [x]<< endl;
}
cout << " The total number of guest are: " << fixed << setprecision(2) << TotalNumberOfGuests<< endl;
cout << "The total room charges are: $" << fixed << setprecision(2) << TotalRoomCharges<<endl;
cout << "The total meal charges are: $" << fixed << setprecision(2) << TotalMealCharges<<endl;
cout << "The grand total is: $" << fixed << setprecision(2) << GrandTotal<<endl;
system ("pause");
}
What the program should look like when running correctly ...
Explanation / Answer
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;
int main() {
// struct to hold room info
struct Room{
string roomNumber;
string guest;
float roomCost;
float mealCost;
// constructor
Room(string rnumber, string guest, float rcost, float mcost):
roomNumber(rnumber), guest(guest), roomCost(rcost), mealCost(mcost) {}
};
int numGuests = 0;
cout << "Enter the number of guests: ";
cin >> numGuests;
float roomTotal = 0;
float mealTotal = 0;
vector<Room> rooms;
string roomNum;
string guestName;
float roomCharge;
float mealCharge;
for(int i = 1; i <= numGuests; i++){
cout << "Enter the room number for guest #" << i << ": ";
cin.ignore();
getline(cin, roomNum);
cout << "Enter the name for guest #" << i << ": ";
getline(cin, guestName);
cout << "Enter the room charge for guest #" << i << ": ";
cin >> roomCharge;
cout << "Enter the meal charge for guest #" << i << ": ";
cin >> mealCharge;
rooms.emplace_back(Room(roomNum, guestName, roomCharge, mealCharge));
}
printf("%-10s%-20s%-10s%-10s%-10s ", "ROOM#", "CUST NAME", "ROOM CHG", "MEAL CHG", "TOTAL CHG");
printf("%-10s%-20s%-10s%-10s%-10s ", "_____", "_________", "________", "________", "_________");
for(int i = 0; i < numGuests; i++){
Room r = rooms.at(i);
roomTotal += r.roomCost;
mealTotal += r.mealCost;
printf("%-10s%-20s$%-10.2f$%-10.2f$%-10.2f ", r.roomNumber.c_str(), r.guest.c_str(), r.roomCost, r.mealCost, r.roomCost + r.mealCost);
}
cout.precision(2);
cout << "The total number of guests are: " << numGuests << endl;
cout << "The total room charges are: $" << fixed <<roomTotal << endl;
cout << "The total meal charges are: $" << fixed << mealTotal << endl;
cout << "The grand total is: $" << fixed <<roomTotal + mealTotal << endl;
system("PAUSE");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.