I am using the getline command for the user to input a name to allow for spaces. however when I later try to print out the name if i enter a name with spaces it does not output correctly. How can i print out all the data that was gathered by the getline command.
Code:
//FILE: ola3.cc
//AUTHOR: Written by Cory Smith
//INSTRUCTOR: Dr. J. Yoo
//COURSE: CSCI 1170-06
//DUE DATE: 10/2/2003
//DESCRIPTION: This programs prompts that prints a statement for each overnight guest at its hotel.
// It inputs informations from the user and saves it to a file. This information is then recalled from this location and printed on the screen
//INPUTS: Name, room number, room rate, number of nights, telephone charges, and other charges
//OUTPUTS: Name, room number, room rate, number of nights, telephone charges, other charges, tax, subtotal, total
//CONSTANT: taxRate = .0925 which stands for 9.25%
//FORMULAS: tax = roomCost * taxRate;
// subTotal = roomCost + tax;
// total = subTotal + telephoneCharges + otherCharges;
//Header Files
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <windows.h>
using namespace std;
//Main function
int main()
{
//Variable Declarations
string fileName; //Name of the customers information file
string guestName; //Name of the guest
int roomNumber; //Room number of the guest
int numberOfNights; //Number of nights the guest stayed
float roomRate; //Rate per night of the room
float roomCost; //Total cost of the room for the guest
float telephoneCharges; //Telephone charges incurred by the guest
float otherCharges; // Other charges incurred by the guest
float additionalCharges; //Telephone charges + other charges
float subTotal; //Subtotal of the stay (before telephone and other charges)
float total; //Total cost for the guest's stay
float tax; //Tax on the guest's room
const float taxRate = .0925; //The tax rate
//Declares the file name to save the customer informaton to
cout << "Name of file to save customer information to: ";
getline(cin, fileName);
//Declares OFSTREAM variable to be used in the program
ofstream outData;
//Prepares the file for writing
outData.open(fileName.c_str());
//Check to make sure the file is open
if(outData.is_open() == true)
//The file exits
;
else
//The file doesnt exists
return 0;
//Prompts for all of the input data required and outputs it to the file specified by user
cout << "Guest's name: "; //The guest's name
getline(cin, guestName);
outData << guestName << endl;
cout << "Guest's room number: "; //The guest's room number
cin >> roomNumber;
outData << roomNumber << endl;
cout << "How much per night: "; //The cost of the room per night
cin >> roomRate;
outData << roomRate << endl;
cout << "How many nights: "; //How many nights the guest stayed
cin >> numberOfNights;
outData << numberOfNights << endl;
cout << "How much in telephone charges: "; //Telephone charges the guest may have incurred
cin >> telephoneCharges;
outData << telephoneCharges << endl;
cout << "How much in other charges: "; //Other charges the guest may have incurred
cin >> otherCharges;
outData << otherCharges << endl;
cout << endl;
roomCost = (roomRate * numberOfNights); //Caluclate the cost of the room before taxes
outData << roomCost << endl; //Stores room cost in file
tax = roomCost * taxRate; //Calculates the tax on the room
outData << tax << endl; //Stores tax in file
subTotal = roomCost + tax; //Calculates the total cost of the room with taxes but without additonal charges
outData << subTotal << endl; //Stores sub total in file
additionalCharges = telephoneCharges + otherCharges; //The telephone and other charges
outData << additionalCharges << endl; //Stores additional charges in file
total = subTotal + additionalCharges; //The total bill including room costs, taxes, and additonal charges
outData << total << endl; //Stores total in file
//Declares IFSTREAM variable to be used in the program
ifstream inData;
//Prepares teh file for reading
inData.open(fileName.c_str());
//Displays the reciept for the guest on the screen
cout << "***********RECEIPT***********\n";
cout << "Murfreesboro Hotel Inc.\n";
cout << endl;
cout << "Guest Name - ";
inData >> guestName;
cout << guestName << endl;
cout << "Room Number - ";
inData >> roomNumber;
cout << roomNumber << endl;
cout << "Room Rate - " << "$";
inData >> roomRate;
cout << roomRate << endl;
cout << "Number of Nights - ";
inData >> numberOfNights;
cout << numberOfNights << endl;
cout << "Room Cost - " << "$";
inData >> roomCost;
cout << roomCost << endl;
cout << "TAX: 9.25% - " << setprecision (4) << "$";
inData >> tax;
cout << tax << endl;
cout << "Subtotal - " << setprecision (5) << "$";
inData >> subTotal;
cout << subTotal << endl;
cout << "Telephone Charges - " << "$";
inData >> additionalCharges;
cout << telephoneCharges << endl;
cout << "Other Charges - " << "$";
inData >> otherCharges;
cout << otherCharges << endl;
cout << "TOTAL DUE - " << setprecision (5) << "$";
inData >> total;
cout << total << endl;
cout << endl;
cout << "Thank you for staying at Murfreesboro Hotel!\n";
cout << "We hope that you choose us for your next visit to Murfreesboro.\n";
cout << "***********RECEIPT***********\n";
cout << endl;
system("pause");
outData.close();
inData.close();
return(0);
}