Reputation: 31
Here is my code for context. This is my second homework assignment intro to programming course and I have used everything we've learned in this assignment. I am not allowed to use anything I have not learned.
The part I am concerned with is the output at the very bottom (commented as information output). Currently, I am struggling to get everything perfectly right aligned (the last letter or number in the right hand column must align with each other as if I were typing them from the right.
Everything except the patient's name, room type, and days spent in the hospital are aligning properly, decimals aligned and everything. Changing the setw(10) to anything larger does nothing beneficial (ex: i had all of them to setw(40) and it still did not align anything properly. Any ideas?
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//constants
const float PRIVATE = 125.00;
const float SEMI = 95.00;
const float WARD = 75.00;
const float TV = 3.50;
const float PHONE = 1.75;
int main()
{ //local variables
string fname, lname, roomType, tvAccess, phoneAccess;
int id, days;
float roomBill, roomPrice, tvCharge, phoneCharge;
bool error = false;
//data collection/output
cout << "Welcome to the hospital self-service program. Your bill will be calculated here.\n\n" << endl;
cout << "Please enter your first and last name: ";
cin >> fname;
cin >> lname;
cout << fname << " " << lname << ", please enter the four digit identification number found on your hospital wristband: ";
cin >> id;
cout << "Please enter the number of days spent in the hospital: ";
cin >> days;
cout << "\nEnter the type of room you stayed in: \nEnter P for room type: Private \nEnter S for room type: Semi-Private\nEnter W for room type: Ward \n" << endl;
cin >> roomType;
cout << "\nDid your room come with access to Television? Y/N: ";
cin >> tvAccess;
cout << "Did your room come with access to a Telephone? Y/N: ";
cin >> phoneAccess;
//if and elses
if (roomType == "P" || roomType == "p")
{
error = false;
roomType = "Private Room";
roomPrice = PRIVATE;
}
else if (roomType == "S" || roomType == "s")
{
error = false;
roomType = "Semi-Private Room";
roomPrice = SEMI;
}
else if (roomType == "W" || roomType == "w")
{
error = false;
roomType = "Ward Room";
roomPrice = WARD;
}
else
{
cout << "Room type not valid. Exit the program and try again." << endl;
error = true;
}
if (tvAccess == "Y" || tvAccess == "y")
tvCharge = TV;
else
tvCharge = 0;
if (phoneAccess == "Y" || phoneAccess == "y")
phoneCharge = PHONE;
else
phoneCharge = 0;
//information output
cout << fixed << setprecision(2) << showpoint << endl;
cout << setw(24) << left << "\n\nPatient Full Name: " << setw(10) << right << fname << " " << lname << endl;
cout << setw(24) << left << "Identification Number: " << setw(10) << right << id << endl;
cout << setw(24) << left << "Days spent in hospital: " << setw(10) << right << days << " day(s)" << endl;
cout << setw(24) << left << "Room Type: " << setw(10) << right << roomType << endl;
cout << setw(24) << left << "Room Charge: " << setw(10) << right << roomPrice * days << endl;
cout << setw(24) << left << "Television Charge: " << setw(10) << right << tvCharge * days << endl;
cout << setw(24) << left << "Telephone Charge: " << setw(10) << right << phoneCharge * days << endl;
cout << setw(24) << left << "Total Charge: " << setw(10) << right << days * (phoneCharge + tvCharge + roomPrice) << endl;
system("pause");
return 0;
}
for clarification.
Upvotes: 0
Views: 1857
Reputation: 8466
The reason, they are not formatted correctly, is, that the setw
in
setw(10) << right << fname << " " << lname << endl;
and
setw(10) << right << days << " day(s)" << endl;
only applies to the first variable, i.e. fname
and days
. Everything else is just appended. You need to concatenate these strings first before using setw
here.
Upvotes: 3