c++ help...

siftings81

Member
Sep 3, 2001
61
0
0
ran into a little problem with a program I'm doing...
say I have:

string userdate, bookisbn, booktitle;
int bookquantity;
float bookprice, total, tax, final;

cout << "Date: ";
cin >> userdate;
cout << "Quantity of Book: ";
cin >> bookquantity;
cout << "ISBN: ";
cin >> bookisbn;
cout << "Title: ";
cin >> booktitle;
cout << "Price: ";
cin >> bookprice;

It'll work fine if the user inputs a title with no spaces, but if there are spaces, then cin >>bookprice is skipped. I've tried using getline(cin, booktitle) and cin.ignore(), but nothing works completely. Any help I would apreciate.
 

LimeGreen

Banned
Oct 23, 2001
145
0
0
Originally posted by: siftings81
ran into a little problem with a program I'm doing...
say I have:

string userdate, bookisbn, booktitle;
int bookquantity;
float bookprice, total, tax, final;

cout << "Date: ";
cin >> userdate;
cout << "Quantity of Book: ";
cin >> bookquantity;
cout << "ISBN: ";
cin >> bookisbn;
cout << "Title: ";
cin >> booktitle;
cout << "Price: ";
cin >> bookprice;

It'll work fine if the user inputs a title with no spaces, but if there are spaces, then cin >>bookprice is skipped. I've tried using getline(cin, booktitle) and cin.ignore(), but nothing works completely. Any help I would apreciate.

for anything with a space in it use cin.getline, like this:


// cin.getline(name_of_var, num_of_chars, char_to_stop_at);

char booktitle[26];
cout << "Enter the book title: ";
cin.getline(booktitle, 25, '\n');

also if anything is being input before the cin.getline or you're experiencing that it's acting weird (it might surpass the whole cin.getline before you even type anything in for title) then you'll probably need to use cin.ignore to ignore what is in the buffer before you use cin.getline. I'm not sure how but I think its:

// cin.ignore(# of characters to ignore);