The Student Room Group

c++ - do not understand this code

CODE:
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
int choice;

while (true)
{
cout << "Please enter an integer: ";
string input, test;
cin >> input;
istringstream ss(input);
bool goodint = (ss >> choice);
bool goodstr = (ss >> test);
if (goodint && !goodstr)
break;

cout << "Invalid input: " << input << endl;
}

cout << "The integer was: " << choice << endl;

return 0;
}

--------
I do not understand what
a) istringstream does
b) what does ss >> choice mean? (and what does ss>>test mean)?

thanks
Reply 1
Hi, I'm no expert, however I can give you at least a basic explanation.

istrinstream represents an input stream. In the context of how it internally works, it is probably some sort of buffer.

Anyway, the << and >> operators, are how data moves in and out of a stream. Technically, these are bit shifting operators, which holds true for streams, but what is happening, is say if you look at:

cout << "Please enter an integer: "; This is pushing the string "Please enter an interger: "; into an ostringstream (which is an output stream). In the context of cout. std::cout is the default system output stream which is bound to the console.

the line istringstream ss(input); initialises an input string stream called 'ss' and the constructor, ss( input ), will most likely mean that the input stream has been initialised with the input as its default value. The input value, is a string, and its value is determined from

cin >> input;

which means that the value of input, is pulled from the user input stream, (whatever you type).

the next part ss>>choice pulls the last integer pushed into the stream and stores it in the variable choice, the bool goodint, will then be assigned "true" if the number was infact a number (this only happens if the operation was successful). The same value is also pulled from the buffer, and stored in test and if that value is a string, then goodstr = true. (Which will happen if the string conversion operation was successful.)

This is a validation test, to check whether the input is a valid integer. If it is not an integer, it asks you to do it again. The point of this, is because a string will contain characters that can not be valid, so a casting operation cannot be used to convert the data from the stream (ss) into an integer value, so this code is designed to ensure that the user can only input an integer value, before continuing with the rest of the program.

In short (incase my explanation was waffly):

a) istringstream is an input stream. It is the same kind of input stream as cin, however cin is bound to the console window, meaning that its values change based on what the user types. This is why a separate istringstream (ss) was defined, so that this could instead be used to store the same information as the cin stream, so it doesn't get erased.

b) ss>>choice takes the value from the istringstream called ss, and attempts to store it in the variable "choice". the bool goodint captures whether this was successful or not.
ss>>test takes the value aswell, except attempts to store it in the variable "test". The boolean, goodstr captures whether this was successful. (As this is a test for valid number entry into a console, we want this to be false, otherwise the user has entered text, rather than a number).

Quick Reply

Latest

Trending

Trending