Thread: Getline problem

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    1

    Getline problem

    I'm pretty new to programming (especially C++), and I'm having a problem using cin.getline.
    The program is suppose to get the title of a book (as well as other things), but completely skips over it and displays 'Title: ' and then immediately displays 'Price: '.
    What is really driving me insane is that I copied out all of the code relevent to getting and displaying the title and put it in another program all by itself and it worked perfectly.
    I'm sure I'm making some very stupid mistake, but I've been looking through it for days and can't find what I'm doing wrong.

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    int main()
    {       const int SIZE = 50;
            int quantity;
            char date[9], isbn[20], title[SIZE], again='Y';
            float price, subtotal, total, tax;
    		
            while (again == 'y' || again == 'Y')
            {       cout << "\n";
                    cout << "Date: ";
                    cin >> date;
                    cout << "Quantity of Book: ";
                    cin >> quantity;
                    cout << "ISBN: ";
                    cin >> isbn;
                    cout << "Title: ";
    	cin.getline(title, SIZE);
                    cout << "Price: $";
                    cin >> price;
                    
    	subtotal = quantity * price;
                    tax = subtotal * 0.06;
                    total = subtotal + tax;
                   
    	cout << "\n";
                    cout << "Serendipity Booksellers\n";
                    cout << "\n";
                    cout << "Date: " << date << endl;
                    cout << "\n";
                    cout << "Qty\t";
                    cout << "ISBN\t\t";
                    cout << "Title\t\t\t\t\t";
                    cout << "Price\t";
                    cout << "Total" << endl;
                    cout << "________________________________________________________________________________";
                    cout << "\n";
    	cout << quantity << "\t";
                    cout << isbn << "\t";
                    cout << title << "\t\t\t";
                    cout << "$" << setprecision(2) << fixed << price << "\t";
                    cout << "$" << setprecision(2) << fixed << subtotal << "\t";
                    cout << "\n";
                    cout << "\t\t\tSubtotal " << setw(42) << "$" << setprecision(2) << fixed << subtotal;
                    cout << "\t\t\tTax " << setw(48) << "$" << setprecision(2) << fixed << tax;
                    cout << "\t\t\tTotal " << setw(45) << "$" << setprecision(2) << fixed << total << endl;
                    cout << "\n";
                    cout << "Thank You for Shopping at Serendipity!\n";
    
                    cout << "Would you like to process another transaction (y/n). ";
                    cin >> again;
            }
            return 0;
    }
    I'd really appreciate any help anyone could give me. Thanks!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    cin.ignore();
    cin.getline(title, SIZE);
    I think adding the line in bold should fix your problem.

  3. #3
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Shoudn't it be:

    Code:
    cin.ignore(1000, '\n')
    (1000 could be any big number) Or is that superfluous?
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Or is that superfluous?
    Not at all. Actually handling cin is more complicated than that, because any time that a number is expected but the user enters other characters, the stream goes into a fail state.

    Therefore, if I needed to get numerical input in several places, I'd write a separate function for that.

    Code:
    int get_int(const std::string& prompt, const std::string& error_msg)
    {
        int result;
        std::cout << prompt;
        while (!(std::cin >> result)) { //while reading input fails
            std::cout << error_msg;
            std::cin.clear(); //reset the error flags of cin
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //clear buffer
        }
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        return result;
    }
    (In a program like that I would recommend using std::string instead of char arrays.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  3. getline() problem
    By mrafcho001 in forum C++ Programming
    Replies: 5
    Last Post: 06-12-2005, 01:16 AM
  4. Need help with structures
    By yoursport in forum C++ Programming
    Replies: 8
    Last Post: 04-20-2005, 11:59 AM
  5. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM