0

Let's suppose I'd like to read an integer from the console, and I would not like the program to break if it is fed non-integer characters. This is how I would do this:

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

int main() {
    string input; int n;
    cin >> input;
    if(!(stringstream(input)>>n)) cout << "Bad input!\n";
    else cout << n;
    return 0;
}

However, I see that http://www.cplusplus.com/doc/tutorial/basic_io/ uses getline(cin,input) rather than cin >> input. Are there any relevant differences between the two methods?

Also I wonder, since string is supposed not to have any length limits... What would happen if someone passed a 10GB long string to this program? Wouldn't it be safer to store the input in a limited-length char table and use, for example, cin.getline(input,256)?

  • May be this helps for clarifying how to do this best: [How to test whether stringstream operator>> has parsed a bad type and skip it](http://stackoverflow.com/questions/24504582/how-to-test-whether-stringstream-operator-has-parsed-a-bad-type-and-skip-it) – πάντα ῥεῖ Jul 05 '15 at 10:30

3 Answers3

0

std::getline gets a line (including spaces) and also reads (but discards) the ending newline. The input operator >> reads a whitespace-delimited "word".

For example, if your input is

123 456 789

Using std::getline will give you the string "123 456 789", but using the input operator >> you will get only "123".


And theoretically there's no limit to std::string, but in reality it's of course limited to the amount of memory it can allocate.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 2
    The limit is `std::string::max_size` which is implementation defined. If this limit is exceeded then `std::ios_base::failbit` is set. – David G Jul 05 '15 at 14:57
0

the first gets a line, the second gets a world.if your input "hello world"


  1. getline(cin,str) : str=="hello world"
  2. cin>>str: str="hello"

and dont worry about out of range, like vector ,string can grow

kiviak
  • 1,083
  • 9
  • 10
0
  • operator>> reads a word (i.e. up to next whitespace-character)
  • std::getline() reads a "line" (by default up to next newline, but you can configure the delimiter) and stores the result in a std::string
  • istream::getline() reads a "line" in a fashion similar to std::getline(), but takes a char-array as its target. (This is the one you'll find as cin.getline())

If you get a 10 GB line passed to your program, then you'll either run out of memory on a 32-bit system, or take a while to parse it, potentially swapping a fair bit of memory to disk on a 64-bit system.

Whether arbitrary line-length size limitations make sense, really boils down to what kind of data your program expects to handle, as well as what kind of error you want to produce when you get too much data. (Presumably it is not acceptable behaviour to simply ignore the rest of the data, so you'll want to either read and perform work on it in parts, or error out)

somaen
  • 21
  • 2