1

I wish to take inputs from console where the number of inputs are not known. All i know is that they are less then or equal to 10.

I wrote this code but it evaluates the last value twice.

int x;
    do{
        cin>>x;
        cout<<check(x)<<"\n";
    }while(std::cin);

The inputs are in this form:

12 
2 
45
user3250183
  • 426
  • 5
  • 17

2 Answers2

2

As @LightnessRacesinOrbit commented, it's while(!eof) bug again. You should check the stream state after reading from it, not before.

You can use cin>>x in the while-condition and apply a special value (i.e. anything not a number) as the end flag like this:

while (cin >> x)
{
    // got a value
}
Community
  • 1
  • 1
herohuyongtao
  • 45,575
  • 23
  • 118
  • 159
0

You may use istream::geline.

char input[1024]
std::cin.getline (input,1024);

After you read the line, you have to split it by white-spaces as delimiter. Just ignore 11th split onwards. And you may apply atoi or similar functions on the splits (some_split.c_str()) to get the numeric values

Nipun Talukdar
  • 4,206
  • 4
  • 26
  • 36