cin problem

Jul 19, 2009 at 12:10am
Sorry to bother you with this, but sometimes when I do cin >> nbr; following a previous cin >> operation, the cin fails. It doesn't read anything and just skips right by it.

I'm just getting back into C++ and had the same problem years ago. At that time, I always followed cin >> nbr; with: cin.get(); the get() seems to clean up the input stream, or something. I still don't understand what is really going on here, and there must be an explanation and a better way.

Here is a current example:

#include <iostream>
#include <string>
using namespace std;

int main() {

cout << "Enter one or more numbers followed by Enter and Cntl+d" << endl;

double x = 0;
int tot = 0;

while (cin>>x) ++tot;

cout << "Last entered x = " << x << endl;

// The following cin fails

int nbr = 0;
cout << "Enter a number" << endl;

cin >> nbr; // This Fails

cout << '\n' << The value of nbr is " << nbr << endl;

return 0;
}
/*
Enter one or more numbers followed by Enter and Cntl+d
4 5
Last entered x = 5
Enter a number:
The value of nbr is 0
*/

Of course the problem here might have something to do with the termination of the previous cin,
being: Enter Cntl+d for EOF
However, I've had this problem in other places. I'm sure someone has an idea for me.

Thank you for your patience.
Last edited on Jul 19, 2009 at 1:43am
Jul 19, 2009 at 3:20am
Yes, the problem is precisely that the user terminated standard input (according to your instruction).
Don't do that.

I will typically use a stringstream to get such input from a user.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
  {
  unsigned number_of_numbers = 0;
  double   sum_of_numbers    = 0.0;

  cout << "Please enter one or more numbers. Press ENTER twice to finish.\n> " << flush;
  string s;
  while (getline( cin, s ) && !s.empty())
    {
    istringstream ss( s );
    double x;
    while (ss >> x)
      {
      number_of_numbers += 1;
      sum_of_numbers    += x;
      }
    if (!ss.eof())
      cout << "Only enter numbers please!\n";
    cout << "> " << flush;
    }

  cout << "You entered " << number_of_numbers << " numbers.\n";
  cout << "Their sum is " << sum_of_numbers << ".\n";
  cout << "The average is " << (sum_of_numbers / number_of_numbers) << ".\n";

  return 0;
  }


Once EOF (or any other error) is signalled, all attempts to input data fail. You must first clear the stream (cin.clear();) before attempting further input.
1
2
3
4
5
6
7
8
9
10
cout << "Last entered x = " << x << endl;

// Clear the EOF condition on the standard input
cin.clear();

// Now this will not fail
int nbr = 0;
cout << "Enter a number" << endl;

cin >> nbr;  // Works fine... 

Hope this helps.
Jul 21, 2009 at 4:31pm
Yes, Thank you Duoas (2387).

My further research had come across your second solution: stream.clear(); cin.clear();

However, your first solution is really slick. I like it.

Thank you.
Jul 29, 2009 at 5:23pm
The number in parentheses is the number of posts that he has, it's not part of his name . If you look now it will be higher, just fyi.
Jul 29, 2009 at 6:14pm
The number in parentheses is the number of posts that he has, it's not part of his name . If you look now it will be higher, just fyi.


I opted for this ^_^
Topic archived. No new replies allowed.