Orchid XP v8 <voi### [at] devnull> wrote:
> std::string txt;
> std::cin >> txt;
> std::istringstream iss(txt);
> int number;
> iss >> number;
I think you misunderstood the reason to use stringstreams. Recycling the
user input through the stringstream above is basically a no-op (except that
it's needlessly inefficient). You could have read the value directly from
std::cin:
int number
std::cin >> number;
Why I suggested using stringstreams in your original program was because
you wanted to read either a string or an integer. This can be done by
reading a string, and then trying to parse it as an integer with a
stringstream (which also allows you to check if the parsing succeeded).
However, here you are simply always reading an integer. There's no need
to use stringstreams.
> if (number < target) {std::cout << "Too low." << std::endl; continue;}
> if (number > target) {std::cout << "Too high." << std::endl; continue;}
> break;
Wouldn't it be simpler to do it like:
if(number == target) break;
if(number < target) std::cout << "Too low.\n";
else std::cout << "Too high.\n";
--
- Warp
Post a reply to this message
|