POV-Ray : Newsgroups : povray.off-topic : My first C++ program : Re: A test Server Time
4 Nov 2024 13:16:49 EST (-0500)
  Re: A test  
From: Warp
Date: 21 Sep 2008 04:41:06
Message: <48d608a2@news.povray.org>
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

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.