retrieving the entire string after using getline

This is a discussion on retrieving the entire string after using getline within the C++ Programming forums, part of the General Programming Boards category; I am using the getline command for the user to input a name to allow for spaces. however when I ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    68

    retrieving the entire string after using getline

    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);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Six years overdue huh. And it's still worth points?

    Anyway, I thought you said you were using getline to read in the name:
    Code:
    inData >> guestName;

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    o whoops haha that needs to be changed but anyways

    Code:
      cout << "Guest's name: ";                               //The guest's name
        getline(cin, guestName);
        outData << guestName << endl;
    That is the getline im talking about. So it saves whatever I enter to a file.

    But when i try to retrieve it from a file it only works in what was enter was a name without spaces such as simply Cory, while Cory J. Smith would not work.

    Code:
    inData >> guestName;
    That is how i am recalling it from the file that it was saved to previously.

    Sorry if this doesnt make sense but essentially i need it to be able to accept a name with spaces.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    63
    Whether your assignment was due in 2003 or 2009 on October 2nd, it's still late.

    Anyways, let's assume you write each name on a separate line. All you'd need to do is this:

    PHP Code:
    getline(someStreamsomeString'\n'); 

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    The string is not multiple names it's on name.

    getline(cin, guestName);

    if i enter Cory J. Smith then Cory J. Smith is saved to the file.

    later when i do inData >> guestName;

    it reads it as

    Cory
    J.
    Smith

    three separate lines

    I need a way to grab it all from the txt file as one line.

    if I just enter Cory it works just fine.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by bleuz View Post
    o whoops haha that needs to be changed but anyways

    Code:
      cout << "Guest's name: ";                               //The guest's name
        getline(cin, guestName);
        outData << guestName << endl;
    That is the getline im talking about. So it saves whatever I enter to a file.

    But when i try to retrieve it from a file it only works in what was enter was a name without spaces such as simply Cory, while Cory J. Smith would not work.

    Code:
    inData >> guestName;
    Exactly my point. If you don't use getline here too, it doesn't make a single bit of difference what you did the first time.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    So when I am recalling it from the file I need to use getline also?

    what would it look like then?

    getline(inData, guestName);

    ???? help is much appreciated

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That looks like a winner to me.

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    yes yes thank you much appreciated for all your help. I realized my mistake. When i saved the information to the file i have like 11 lines. but when i printed it out again i left 2 things out so there ordering was off. Got it now thanks much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21