POV-Ray : Newsgroups : povray.off-topic : My first C++ program Server Time
4 Nov 2024 17:31:56 EST (-0500)
  My first C++ program (Message 101 to 110 of 149)  
<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>
From: Darren New
Subject: Re: A test
Date: 21 Sep 2008 10:24:36
Message: <48d65924$1@news.povray.org>
Orchid XP v8 wrote:
> Warp wrote:
>>   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";
> 
> Mmm, I guess that would also work...

The pendant in me wants to have an actual condition as the argument to 
"while". :-)  Or at least

do {
   ....
} while (number != target);

-- 
Darren New / San Diego, CA, USA (PST)


Post a reply to this message

From: Orchid XP v8
Subject: Re: A test
Date: 21 Sep 2008 10:26:45
Message: <48d659a5$1@news.povray.org>
Darren New wrote:

> The pendant in me wants to have an actual condition as the argument to 
> "while". :-)  Or at least
> 
> do {
>   ....
> } while (number != target);

Yeah - although that would require "number" to be declared outside the 
while loop so that it actually exists. ;-) More subtly, it would have to 
be initialised to a value that is guaranteed to not equal "target". Ooo, 
*that* could be a nasty bug to try to find...!

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Darren New
Subject: Re: A test
Date: 21 Sep 2008 10:39:10
Message: <48d65c8e@news.povray.org>
Orchid XP v8 wrote:
> Darren New wrote:
> 
>> The pendant in me wants to have an actual condition as the argument to 
>> "while". :-)  Or at least
>>
>> do {
>>   ....
>> } while (number != target);
> 
> Yeah - although that would require "number" to be declared outside the 
> while loop so that it actually exists. ;-)

Yes, I think so.

> More subtly, it would have to 
> be initialised to a value that is guaranteed to not equal "target". Ooo, 
> *that* could be a nasty bug to try to find...!

No, because you're using a "do while" instead of just a "while". That 
like "repeat until" in Pascal (except with a reversed boolean, of course).

And yes, you'd have to initialize the value, but that's all high-school 
programming type stuff. Probably the compiler at this point would warn 
you the variable isn't initialized.

-- 
Darren New / San Diego, CA, USA (PST)


Post a reply to this message

From: Mueen Nawaz
Subject: Re: (And in Haskell. Obviously.)
Date: 21 Sep 2008 11:14:46
Message: <48d664e6$1@news.povray.org>
Slime wrote:
>> I have a similar issue with stuff like
>>
>>   while (*x++ = *y++) {}
> 
> FYI, I have an issue with stuff like that too. C and C++ do make it easy to 
> write bad code. This is the second time you've mentioned that example, so I 
> assume someone once showed it to you and thought they were being clever. 
> They weren't! Or maybe it's just a popular example, because I was just 
> reading a book about coding style last night that gave that as a bad 
> example.

	The truth is that there is no shortage of programmers out there who 
write like this. I've literally read a C (or was it C++) book  that 
admitted it was obfuscated, and in the same breath suggesting that the 
reader should become accustomed to it because those constructs are common.

-- 
The meek shall inherit the dearth.


                     /\  /\               /\  /
                    /  \/  \ u e e n     /  \/  a w a z
                        >>>>>>mue### [at] nawazorg<<<<<<
                                    anl


Post a reply to this message

From: Orchid XP v8
Subject: Re: My first C++ program
Date: 21 Sep 2008 12:26:26
Message: <48d675b2@news.povray.org>
Warp wrote:

>   The above example code can thus be written as:
> 
>     while(true)
>     {
>         int value;
>         std::cin >> value; // Try to read an integer
>         if(std::cin.fail()) // If there was no integer in the input
>         {
>             std::cin.clear(); // Clear the error flags
>             std::string s;
>             std::cin >> s; // Read a string
>             if(!std::cin.good()) break;
>             std::cout << "The input was something else: " << s << "\n";
>         }
>         else
>         {
>             std::cout << "The input was an integer: " << value << "\n";
>         }
>     }
> 
>   Thus removing the need for stringstreams.

This doesn't appear to work. It waits for me to type a second token, and 
then works with that. (Either that or my program has some other random 
bug... If I ever figure out how to work VisualStudio's debugger I'll let 
you know!)

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Fredrik Eriksson
Subject: Re: A test
Date: 21 Sep 2008 13:56:48
Message: <op.uhuesyd87bxctx@e6600>
On Sun, 21 Sep 2008 16:24:37 +0200, Darren New <dne### [at] sanrrcom> wrote:
> The pendant in me

Don't worry. It will come out naturally in a few days...



-- 
FE


Post a reply to this message

From: Darren New
Subject: Re: A test
Date: 21 Sep 2008 14:10:12
Message: <48d68e04$1@news.povray.org>
Fredrik Eriksson wrote:
> On Sun, 21 Sep 2008 16:24:37 +0200, Darren New <dne### [at] sanrrcom> wrote:
>> The pendant in me
> Don't worry. It will come out naturally in a few days...

Normally, complaining about spelling is silly. In this case, tho...

-- 
Darren New / San Diego, CA, USA (PST)


Post a reply to this message

From: Warp
Subject: Re: My first C++ program
Date: 21 Sep 2008 14:26:57
Message: <48d691f1@news.povray.org>
Orchid XP v8 <voi### [at] devnull> wrote:
> This doesn't appear to work. It waits for me to type a second token, and 
> then works with that.

  It works here. Did you try it with gcc?

-- 
                                                          - Warp


Post a reply to this message

From: Orchid XP v8
Subject: Re: My first C++ program
Date: 21 Sep 2008 14:36:51
Message: <48d69443$1@news.povray.org>
Warp wrote:
> Orchid XP v8 <voi### [at] devnull> wrote:
>> This doesn't appear to work. It waits for me to type a second token, and 
>> then works with that.
> 
>   It works here. Did you try it with gcc?

Just for completeness, yeah. Also didn't work. Unless I did something 
"else" wrong with the code as well...

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Orchid XP v8
Subject: Re: A test
Date: 21 Sep 2008 15:14:49
Message: <48d69d29$1@news.povray.org>
Orchid XP v8 wrote:

> Now, if it was pretty-printed like THIS:
> 
>   http://hpaste.org/10565
> 
> you wouldn't be as confused. (?) If you have some kind of text editor 
> with syntax-hilighting, this isn't an issue.

So I'm guessing either nobody noticed this tucked away here, or it 
doesn't make much difference. (Or nobody even cares anymore... o_O )

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>

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