-
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!
-
Code:
cin.ignore();
cin.getline(title, SIZE);
I think adding the line in bold should fix your problem.
-
Shoudn't it be:
Code:
cin.ignore(1000, '\n')
(1000 could be any big number) Or is that superfluous?
-
Quote:
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.)