confusion

what is difference between cin.get and cin.getline i still very confusing with this
get leaves the delimiter, getline destroys it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <sstream>
#include <iostream>

int main()
{
    {
    std::istringstream s1("Hello, world.");
    char str[6];
    s1.get(str, sizeof str, ',');  // read up to the comma; leaves comma on the stream
    std::cout << "after reading \"" << s1.str() << "\" up to comma using istream::get,\n"
              << "the array contains \"" << str << "\"\n"
              << "next character to read is '" <<  (char)s1.get() << "'\n";
    }

    {
    std::istringstream s1("Hello, world.");
    char str[6];
    s1.getline(str, sizeof str, ',');  // read up to the comma; removes comma from the stream
    std::cout << "after reading \"" << s1.str() << "\" up to comma using istream::getline,\n"
              << "the array contains \"" << str << "\"\n"
              << "next character to read is '" <<  (char)s1.get() << "'\n";
    }
}


after reading "Hello, world." up to comma using istream::get,
the array contains "Hello"
next character to read is ','
after reading "Hello, world." up to comma using istream::getline,
the array contains "Hello"
next character to read is ' '


demo: http://coliru.stacked-crooked.com/a/ca25131fd835b618
Last edited on
if no use sstream can or not?
What do you ask?
Topic archived. No new replies allowed.