1

I know that this is a common question but i could not find any solution to this question without using vectors and ctrl + d/c. I have encounter a infinite while loop while using cin to get a unknown amount of integer. the while loop does not stop executing even after a enter in pressed. Thanks a lot!

while(cin >> num)
{
    num--;
    sizeB = 0;
    setB[sizeB] = num;
    sizeB++;
}
cin.ignore();
cin.clear();
  • Whitespace is ignored in formatted input operations. A newline is whitespace. Maybe you want to use `getline` instead? – Zeta Jul 17 '14 at 12:11
  • If `num` is an integer than typing letters and pressing Enter will cause the loop to exit – M.M Jul 17 '14 at 12:23
  • 1
    @MarcoA the condition is `cin`. (`operator>>` returns reference to `cin`). It's equivalent to doing `!cin.fail()`, i.e. check that neither end-of-file occurred, nor that something was typed which failed to be converted to the type of `num`. – M.M Jul 17 '14 at 12:24
  • @Zeta I am sorry i could not use getline as if i use get line it will be read all the values as one sentence. – Aurora_rainbow Jul 17 '14 at 12:26
  • @MattMcNabb I'm not used to this usecase, that's definitely true (http://www.cplusplus.com/reference/ios/ios/operator_bool/). To be precise `'does not return the same as member good, but the opposite of member fail'` – Marco A. Jul 17 '14 at 12:27
  • @MattMcNabb num is a int and enter did not cause the loop to exit thus i am troubled. – Aurora_rainbow Jul 17 '14 at 12:27
  • @Aurora_rainbow Maybe [this one](http://stackoverflow.com/questions/24504582/test-whether-stringstream-operator-has-parsed-a-bad-type?noredirect=1#comment37965807_24504582) helps you to understand. – πάντα ῥεῖ Jul 17 '14 at 12:27
  • @MattMcNabb I'll delete mine, it's the first and might be the first thing someone picks up if he's in a hurry. – Marco A. Jul 17 '14 at 12:29
  • @Aurora_rainbow type in letters. like `"zod"` and press enter – M.M Jul 17 '14 at 12:29
  • @Aurora_rainbow consider editing your question so that it actually asks a question – M.M Jul 17 '14 at 12:44
  • @πάνταῥεῖ so does that means that while(cin >> num) cant be use if there isnt a file/ text given? – Aurora_rainbow Jul 17 '14 at 12:45
  • @Aurora_rainbow No this means you should use `getline()` as suggested and parse and check for errors based on the given input string. If you have encountered an error or what ever your ending condition is, break the loop. – πάντα ῥεῖ Jul 17 '14 at 12:49

2 Answers2

2

It is possible to use getline function to get data line-by-line, then read values via a stringstream:

#include <iostream>
#include <stdio.h>
#include <sstream>

using namespace std;

int main() {
  string line;
  while(getline(cin, line)) {
    stringstream str_stream(line);
    int num;
    while(str_stream >> num) {
      cout << "..." << num << "..." << endl;
    }
    cout << "----" << endl;
  }
}
kzagar
  • 355
  • 2
  • 4
0

while takes a bool argument. In your code, cin >> num returns an istream&, that is converted calling istream::operator bool() (and I'd guess evaluates to true if the stream is not closed)

read a string and convert it to int, break when the string is empty:

while (1) {
   std::string theString;
   std::getline(std::cin, theString);
   if (theString.empty())
      break;
   int num= atoi(theString.c_str());
   ...
}
Exceptyon
  • 1,584
  • 16
  • 23