How to read in user entered comma separated integers?

All of the existing answers are excellent, but all are specific to your particular task. Ergo, I wrote a general touch of code that allows input of comma separated values in a standard way: template<class T, char sep=’,’> struct comma_sep { //type used for temporary input T t; //where data is temporarily read to operator … Read more

cin >> fails with bigger numbers but works with smaller ones?

Try: std::cout << std::numeric_limits<int>::max() << std::endl; // requires you to #include <limits> int on your system is likely a 32-bit signed two’s complement number, which means the max value it can represent is 2,147,483,647. Your number, 3,999,999,999, is larger than that, and can’t be properly represented by int. cin fails, alerting you of the problem. … Read more

std::getline on std::cin

Most likely you are trying to read a string after reading some other data, say an int. consider the input: 11 is a prime if you use the following code: std::cin>>number; std::getline(std::cin,input) the getline will only read the newline after 11 and hence you will get the impression that it’s not waiting for user input. … Read more

Problem of using cin twice

The \n is left in the input stream as per the way operator>> is defined for std::string. The std::string is filled with characters from the input stream until a whitespace character is found (in this case \n), at which point the filling stops and the whitespace is now the next character in the input stream. … Read more