0

My problem with the program listed below is when the question asked for the title of the book and you type in more than one word with white spaces inbetween the words like "History of Me" it will skip the next question. Any help will be appreciated.

#pragma hdrstop
 #pragma argsused
 #include <iostream>
 #include <iomanip>
 #include <cmath>
using namespace std;

int main()
{
     char date[8];
     int quantity;
     char isbn[13];
     char title [20];
     float bookprice = 0.00;


     //Sales slip information to be entered by user

     cout << "Please enter the date in mm/dd/yy format:\n" ;
     cin >> date;
     cout << "Please enter the quantity being purchased:\n" ;
     cin >> quantity;
     cout << "Please enter the ISBN number for the book:\n" ;
     cin >> isbn;
     cout << "Please enter the title of the book:\n" ;
     cin >> title;
     cout << "Please enter the price of the book:\n" ;
     cin >> bookprice;
system("pause");
return 0;
}

Edited by Nick Evan: Fixed formatting

4
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by firstPerson
0

This behavior is expected, because the operator >> of cin reads input in his stream until a end-of-line or a whitespace is found. So, the next operator>> call finds remaining input (the other words after the whitespace) in the cin buffer and takes the data. If you want to prevent this situation, you need to truncate the input to the first whitespace or the end-of-line character. You can do it by doing a fscanf call like this:

fscanf(stdin, %s\n, your_string_variable);

This is a C-style way to do it and I would like to see other ways, C++ oriented, to prevent a whitespace in the input.

0

cin does not read white spaces.
use cin.getLine(...), or cin.get(...) or getline(...); They don't skip spaces.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.