POV-Ray : Newsgroups : povray.off-topic : My first C++ program Server Time
30 Sep 2024 19:31:42 EDT (-0400)
  My first C++ program (Message 21 to 30 of 149)  
<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>
From: Orchid XP v8
Subject: Re: My first C++ program
Date: 19 Sep 2008 16:05:00
Message: <48d405ec$1@news.povray.org>
Darren New wrote:

> Well, that made *me* laugh.
> 
> "Nano is awful. I have nothing better. Oh, except the two most popular 
> text editors."

Have *you* ever tried to make Vim or Emacs work? :-P

Besides, Emacs isn't even a text editor - it's an operating system! ;-)

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


Post a reply to this message

From: Mueen Nawaz
Subject: Re: My first C++ program
Date: 19 Sep 2008 16:15:49
Message: <48d40875$1@news.povray.org>
Orchid XP v8 wrote:
> Spot the mistake:
> 
> int foo(vector<int>& stack)
> {
>   int x = stack.back();
>   stack.pop_back();
> }
> 
> For reasons I can't begin to understand,
> 
> 1. This doesn't produce any compile-time error.
> 
> 2. This function always returns the value "2144912088".

	Function lacks a return statement.

-- 
Inoculatte: To take coffee intravenously when you are running late.


                     /\  /\               /\  /
                    /  \/  \ 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: 19 Sep 2008 16:19:31
Message: <48d40953$1@news.povray.org>
Mueen Nawaz wrote:
> Orchid XP v8 wrote:
>> Spot the mistake:
> 
>     Function lacks a return statement.

Yup. A simple and obvious mistake. But unfortunately, not one the 
compiler warned me about. (I'm also curios to know exactly what the hell 
the function is returning... No doubt the reason there's no compiler 
warning is that this is actually a valid thing to do due to some bizare 
C language rule kept for compatibility or something random like that.)

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


Post a reply to this message

From: Vincent Le Chevalier
Subject: Re: My first C++ program
Date: 19 Sep 2008 16:31:10
Message: <48d40c0e@news.povray.org>
Orchid XP v8 a écrit :
> Mueen Nawaz wrote:
>> Orchid XP v8 wrote:
>>> Spot the mistake:
>>
>>     Function lacks a return statement.
> 
> Yup. A simple and obvious mistake. But unfortunately, not one the 
> compiler warned me about. (I'm also curios to know exactly what the hell 
> the function is returning... No doubt the reason there's no compiler 
> warning is that this is actually a valid thing to do due to some bizare 
> C language rule kept for compatibility or something random like that.)
> 
If you are using g++, you can add the option -Wall to your command line. 
The compiler should give a warning in this case... Well two actually, 
one because the variable x is not used for anything and one because the 
function does not return.

I'm leaving the explanation of why this is still legal to someone more 
competent :-)

-- 
Vincent


Post a reply to this message

From: Darren New
Subject: Re: My first C++ program
Date: 19 Sep 2008 17:51:59
Message: <48d41eff$1@news.povray.org>
Orchid XP v8 wrote:
> Have *you* ever tried to make Vim or Emacs work? :-P

I have used both for years at a time. I prefer vim, except for one 
stretch of time when it was still "vi" and I wanted automatic 
indentations for Tcl.

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


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: (And in Haskell. Obviously.)
Date: 19 Sep 2008 18:56:12
Message: <48d42e0c@news.povray.org>
Invisible wrote:
> Somehow... it seems so much easier in Haskell. ;-)

I understand the C++ one and the Haskell one looks like line noise.


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: My first C++ program
Date: 19 Sep 2008 19:02:40
Message: <48d42f90@news.povray.org>
Invisible wrote:
>>> ...you mean, you can take a string and make a stream that will read from
>>> it? (Or, presumably, write to it, if you desire.)
>> 
>>   Correct. Or, more precisely, you can write to an ostringstream and then
>> get a regular string out of it (with the str() function).
> 
> Ah  - because a string is fixed-size once created?

No. std::string is mutable, and can change its size.

This is how you can convert numbers to/from strings:

int answer = 42;
std::ostringstream oss;
oss << answer;

//oss.str() returns a std::string containing "42".

-------------

int answer;
std::istringstream iss("42");
iss >> answer;

//answer now contains the number 42.


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: My first C++ program
Date: 19 Sep 2008 19:03:22
Message: <48d42fb9@news.povray.org>
Orchid XP v8 wrote:
> Darren New wrote:
> 
>> Well, that made *me* laugh.
>> 
>> "Nano is awful. I have nothing better. Oh, except the two most popular
>> text editors."
> 
> Have *you* ever tried to make Vim or Emacs work? :-P 

Vim is my main text editor. Even on Windows.


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: My first C++ program
Date: 19 Sep 2008 19:07:41
Message: <48d430bc@news.povray.org>
Invisible wrote:
> Warp wrote:
> 
>>   No need for atoi(). You can use stringstreams, as I wrote in the other
>> post.
> 
> I did think using a C function seemed a little bogus.
> 
> (For one thing, AFAIK, there is *no way* to know whether atoi()
> succeeded or failed; if it "fails", you get a zero. So was the string
> actually a zero, or did the conversion fail??)

atoi is always a bad idea.

If you're writing C, you should use strtol instead. If you're writing C++,
you should use a stringstream instead.

Or use boost lexical_cast, which looks like a cast but works with streams to
do string-number conversions. But any boost library involves lots of
inter-library dependencies, and would surely increase your compilation
times (the compiler has to untangle all the templated mess :P). So just
stick to streams.


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: My first C++ program
Date: 19 Sep 2008 19:09:09
Message: <48d43115@news.povray.org>
Orchid XP v8 wrote:
> (I'm also curios to know exactly what the hell
> the function is returning...

Whatever random value happened to be in the appropriate place of the stack.


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.