Difference between istream::get(char&) and operator>> (char&) - c++

My question seems to be the same as this one, but I didn't find an answer since the original question seems to ask something more specific.
In C++98, what is the difference between
char c;
cin.get(c);
and
char c;
cin >> c;
?
I've checked the cplusplus reference for get and operator>>, and they look the same to me.
I've tried above code and they seem to behave the same when I input a char.

The difference depends on when there is a whitespace character on the stream buffer.
Consider the input ' foo'
char c;
cin.get(c);
Will store ' ' in c
However
char c;
cin >> c;
Will skip the whitespace and store 'f' in c

In addition to what's already been said, std::istream::get() is also an unformatted input function so the gcount() of the stream is affected, unlike the formatted extractor. Most of the overloads of get() and getline() have mostly been made obselete by the introduction of std::string, its stream extractors, and std::getline(). I'd say to use std::istream::get() whenever you need a single, unformatted character straight from the buffer (by using its single or zero argument overload). It's certainly quicker than turning off the skipping of whitespace first before using the formatted extractor. Also use std::string instead of raw character buffers and is >> str for formatted data or std::getline(is, str) for unformatted data.

Related

C++ String parse of white spaces [duplicate]

When should std::cin.getline() be used? What does it differ from std::cin?
Let's take std::cin.getline() apart. First, there's std::. This is the namespace in which the standard library lives. It has hundreds of types, functions and objects.
std::cin is such an object. It's the standard character input object, defined in <iostream>. It has some methods of its own, but you can also use it with many free functions. Most of these methods and functions are ways to get one or more characters from the standard input.
Finally, .getline() is one such method of std::cin (and other similar objects). You tell it how many characters it should get from the object on its left side (std::cin here), and where to put those characters. The precise number of characters can vary: .getline() will stop in three cases:
1. The end of a line is reached
2. There are no characters left in the input (doesn't happen normally on std::cin as you can keep typing)
3. The maximum number of characters is read.
There are other methods and functions that can be used with the std::cin object, e.g.
std::string s;
int i;
std::cin >> s; // Read a single word from std::cin
std::cin >> i; // Read a single number from std::cin
std::getline(std::cin, s); // Read an entire line (up to \n) from std::cin
std::cin.ignore(100); // Ignore the next 100 characters of std::cin
In case with char*, std::cin.getline getting line, instead of std::cin getting first word.
Did you read any documentation (e.g. http://www.cplusplus.com/reference/string/getline/)?
Basically, std::cin (or more generally, any std::istream) is used directly in order to obtain formatted input, e.g. int x; std::cin >> x;. std::cin.getline() is used simply to fill a raw char * buffer.
(Very simplefied)My answer is, that std :: cin.getline() can contain spaces, while std :: cin >> can not.
As already others have answered (even better) roughly speaking, use getline() to read an entire line (i.e., a string terminating with \n) and cin>>var to read a number compatible with the type of var (integer, float, double etc.) or a single word.
In this answer I want to emphasize a problem that arises when mixing the two methods. When you do:
int a;
string s;
cin>>a;
getline(cin, s)
cin leaves an end of line, \n, character which is then read by getline();. It is possible to overcome this problem by using cin.ignore().
int a;
string s;
cin>>a;
cin.ignore();
getline(cin, s)

Input hex strings as char at once

I have a strings like this:
315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b16349c146fb778cdf2d3aff021dfff5
Is there a way to read it from file at once into string object keeping in mind that every 2 chars are hexadecimal representation of byte? I.e. I need a reading with conversion from hex to char.
UPDATE
Guys, please read carefully what I asked.
I'm able to write conversion functions and looping along a string.
BUT I need read a string of hex to a string of char at once.
No any looping. No conversions by hands.
Something like cin >> ...some string variable...
Thanks.
UPDATE2
Imagine I have the string "315c4eeaa8b5". I want to write something like cin >> string_var and get that string_var containing exactly the "'0x31','0x5c','0x4e','0xea','0xa8','0xb5'". Please note, this last is an ordinal std::string. I.e. 0x31,0x5c,etc are codes of chars.
Hope it makes thing clearer.
Either you code something up or you use something that already exits. If you are using C++ IO streams then I would suggest taking a look at Boost.IOStreams library and especially its Filtering Streams concept. You can use the tab expanding 2.2.5.2 input_filter tutorial example as a base for your hexadecimal input filter implementation.
You can use istream::opeartor>> with the std::hex manipulator to parse as hexadecimal:
ifstream in("...");
char buffer[3];
vector<char> chars;
while (in.read(buffer, 2))
{
buffer[2] = '\0';
char c;
istringstream(buffer) >> hex >> c;
chars.push_back(c);
}

Is it possible to manipulate some text with an user-defined I/O manipulator?

Is there a (clean) way to manipulate some text from std::cin before inserting it into a std::string, so that the following would work:
cin >> setw(80) >> Uppercase >> mystring;
where mystring is std::string (I don't want to use any wrappers for strings).
Uppercase is a manipulator. I think it needs to act on the Chars in the buffer directly (no matter what is considered uppercase rather than lowercase now). Such a manipulator seems difficult to implement in a clean way, as user-defined manipulators, as far as I know, are used to just change or mix some pre-determined format flags easily.
(Non-extended) manipulators usually only set flags and data which the extractors afterwards read and react to. (That is what xalloc, iword, and pword are for.) What you could, obviously, do, is to write something analogous to std::get_money:
struct uppercasify {
uppercasify(std::string &s) : ref(s) {}
uppercasify(const uppercasify &other) : ref(other.ref) {}
std::string &ref;
}
std::istream &operator>>(std::istream &is, uppercasify uc) { // or &&uc in C++11
is >> uc.ref;
boost::to_upper(uc.ref);
return is;
}
cin >> setw(80) >> uppercasify(mystring);
Alternatively, cin >> uppercase could return not a reference to cin, but an instantiation of some (template) wrapper class uppercase_istream, with the corresponding overload for operator>>. I don't think having a manipulator modify the underlying stream buffer's contents is a good idea.
If you're desperate enough, I guess you could also imbue a hand-crafted locale resulting in uppercasing strings. I don't think I'd let anything like that go through a code review, though – it's simply just waiting to surprise and bite the next person working on the code.
You may want to check out boost iostreams. Its framework allows defining filters which can manipulate the stream. http://www.boost.org/doc/libs/1_49_0/libs/iostreams/doc/index.html

cin doesn't take input as ENTER

char ch[4];
char* ptr;
ptr = ch;
while(1)
{
cin >> *ptr;
if(*ptr == '\n')
break;
ptr++;
}
Here I just wrote a bit of sample code where I am trying to get out of a while loop when user writes ENTER but it's not working. Please help me. Thank you in advance.
To get a single character, use std::istream::get. This should work for getting newlines as well.
But instead of getting characters in a loop until you get a newline, why not just use something like std::getline:
std::string str;
std::getline(cin, str);
Or if you only want to get max three characters you can use std::istream::getline:
char ch[4];
cin.getline(ch, 4, '\n');
You are reading input into the value of a character. That's what *ptr means. I think you want just plain ptr, which is a pointer to an array of characters, which is something that is meant to receive data. What you wrote is basically this:
char c;
cin >> c;
I don't think that's what you meant, nor would it work even if it were, since as Joachim Pileborg points out above, the >> operator skips whitespace like newlines. In general, it is always best to be very robust when it comes to reading input. Provide adequate space, and either use a variable that can grow automatically (like std::string) or tell the system how much space you have (like fgets()).
The following will read a line:
istream& getline (char* s, streamsize n );
The extraction operator would skip leading white-spaces and stop execution on encountering any subsequent white-space. So, when you want to do something like this, use std::istream::get() or std::istream::getline().

std::cin.getline( ) vs. std::cin

When should std::cin.getline() be used? What does it differ from std::cin?
Let's take std::cin.getline() apart. First, there's std::. This is the namespace in which the standard library lives. It has hundreds of types, functions and objects.
std::cin is such an object. It's the standard character input object, defined in <iostream>. It has some methods of its own, but you can also use it with many free functions. Most of these methods and functions are ways to get one or more characters from the standard input.
Finally, .getline() is one such method of std::cin (and other similar objects). You tell it how many characters it should get from the object on its left side (std::cin here), and where to put those characters. The precise number of characters can vary: .getline() will stop in three cases:
1. The end of a line is reached
2. There are no characters left in the input (doesn't happen normally on std::cin as you can keep typing)
3. The maximum number of characters is read.
There are other methods and functions that can be used with the std::cin object, e.g.
std::string s;
int i;
std::cin >> s; // Read a single word from std::cin
std::cin >> i; // Read a single number from std::cin
std::getline(std::cin, s); // Read an entire line (up to \n) from std::cin
std::cin.ignore(100); // Ignore the next 100 characters of std::cin
In case with char*, std::cin.getline getting line, instead of std::cin getting first word.
Did you read any documentation (e.g. http://www.cplusplus.com/reference/string/getline/)?
Basically, std::cin (or more generally, any std::istream) is used directly in order to obtain formatted input, e.g. int x; std::cin >> x;. std::cin.getline() is used simply to fill a raw char * buffer.
(Very simplefied)My answer is, that std :: cin.getline() can contain spaces, while std :: cin >> can not.
As already others have answered (even better) roughly speaking, use getline() to read an entire line (i.e., a string terminating with \n) and cin>>var to read a number compatible with the type of var (integer, float, double etc.) or a single word.
In this answer I want to emphasize a problem that arises when mixing the two methods. When you do:
int a;
string s;
cin>>a;
getline(cin, s)
cin leaves an end of line, \n, character which is then read by getline();. It is possible to overcome this problem by using cin.ignore().
int a;
string s;
cin>>a;
cin.ignore();
getline(cin, s)