POV-Ray : Newsgroups : povray.off-topic : My first C++ program Server Time
30 Sep 2024 21:28:46 EDT (-0400)
  My first C++ program (Message 31 to 40 of 149)  
<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>
From: Nicolas Alvarez
Subject: Re: My first C++ program
Date: 19 Sep 2008 19:33:45
Message: <48d436d8@news.povray.org>
Invisible wrote:
> I wrote a function that returns the top element from the stack while
> simultaneously removing it. That cuts down the typing quite a bit.

The reason that isn't built-in is that it doesn't work well with objects.

If vector.pop_back() returned a reference to the popped object, you'd get
sure crashes (reading from freed memory, or at least, a destructed object).

If vector.pop_back() returned a copy of the popped object, you'd get bad
performance even when you don't need the object (useless copying), you just
want to remove it. And who would free that copy?

ints, however, are easily copyable and don't need "freeing" :)


Post a reply to this message

From: Slime
Subject: Re: My first C++ program
Date: 20 Sep 2008 00:59:53
Message: <48d48349$1@news.povray.org>
> 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.

What compiler are you using?

If it's possible for you to acquire a copy of Visual Studio (even an oldish 
one), I strongly recommend it.

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

From: Slime
Subject: Re: My first C++ program
Date: 20 Sep 2008 01:07:51
Message: <48d48527$1@news.povray.org>
> The tutorial recommends the C function atoi() - which, being a C function, 
> is about as user-friendly as Windows 2. Much hair-pulling involving 
> pointers ensues.
>
> (It turns out that you can assign a string to a char*, but not the 
> reverse. Go figure. Anyway, the solution is simple enough...)

C strings aren't so bad. If you have a std::string, you can call its c_str() 
method to get a (const) pointer to the internal string, which is just a 
sequence of bytes. So, you can use

atoi( mystring.c_str() )

to convert to an integer. This is what I always use, though I see there are 
other ways to do it.

> (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??)

As I understand it, atoi's behavior is to return the integer represented by 
the longest sequence of characters starting at the beginning of the string 
that represent an integer, defaulting to 0. By this definition it's not 
possible to fail. =) Sometimes this is useful.

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

From: Slime
Subject: Re: My first C++ program
Date: 20 Sep 2008 01:13:53
Message: <48d48691$1@news.povray.org>
> What you could do, is have a function like DoOperation(stack,op), and use 
> "op" as a token for the operation.

>     if (cmd == "+") {DoOP(stack,0); continue;}
>     if (cmd == "-") {DoOP(stack,1); continue;}
>     if (cmd == "/") {DoOP(stack,2); continue;}
>     if (cmd == "*") {DoOP(stack,3); continue;}

This is good, but instead of using integers like 0 1 2 3, I'd use an enum:

enum Operation
{
    OP_ADD,
    OP_SUBTRACT,
    OP_DIVIDE,
    OP_MULTIPLY,
};

An enum gives meaning to this sort of approach so you don't have to remember 
what something like "2" means.

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

From: Manuel Kasten
Subject: Re: My first C++ program
Date: 20 Sep 2008 03:09:15
Message: <48d4a19b@news.povray.org>
Slime schrieb:
> What compiler are you using?
> 
> If it's possible for you to acquire a copy of Visual Studio (even an oldish 
> one), I strongly recommend it.
> 

Related to that, the express editions of Visual Studio are available 
free of charge. Some say that the express editions are somewhat limited, 
I however did not run into any walls with them. OpenGL, DirectX, GUI 
work with Qt, all not a problem. Commandline apps work well, too.

For C/C++ work obviously you only need "Visual C++ 2008 Express 
Edition". Download here: http://www.microsoft.com/express/download/

Manuel Kasten


Post a reply to this message

From: Warp
Subject: Re: My first C++ program
Date: 20 Sep 2008 04:45:04
Message: <48d4b810@news.povray.org>
Nicolas Alvarez <nic### [at] gmailcom> wrote:
> If you're writing C, you should use strtol instead. If you're writing C++,
> you should use a stringstream instead.

  As I said in my other post, there is merit in the C functions: They are
much more efficient than stringstreams.

  So if you are converting a *huge* amount of strings to ints in a tight
loop, you might want to consider the C functions rather than stringstreams.

  In situations where speed has absolutely no relevance stringstreams are
much easier and safer to use.

-- 
                                                          - Warp


Post a reply to this message

From: Warp
Subject: Re: My first C++ program
Date: 20 Sep 2008 04:47:04
Message: <48d4b888@news.povray.org>
Warp <war### [at] tagpovrayorg> wrote:
>   Another C function can be used to both parse an integer from a string
> and know if there was one: strtod(). However, its usage is even more
> complicated than atoi().

  Yeah, it's strtol(), not strtod(). The latter is to read a floating
point number.

  (This shows how clear those C function names are... ;) )

-- 
                                                          - Warp


Post a reply to this message

From: Warp
Subject: Re: My first C++ program
Date: 20 Sep 2008 04:49:55
Message: <48d4b932@news.povray.org>
Slime <fak### [at] emailaddress> wrote:
> atoi( mystring.c_str() )

> to convert to an integer. This is what I always use, though I see there are 
> other ways to do it.

  The problem is that with atoi() you can't know if there was an integer
in that string in the first place. If there wasn't, it just returns zero,
and you can't know if there was a zero in the string or something which
was not a parseable number.

  (And no, it's not enough to check if mystring[0] == '0'. There are a
multitude of other ways for a valid number 0 to be in the string.)

  This might be very relevant if you are checking if there was a number
of something else in the string, and act accordingly.

-- 
                                                          - Warp


Post a reply to this message

From: Warp
Subject: Re: My first C++ program
Date: 20 Sep 2008 04:59:13
Message: <48d4bb61@news.povray.org>
Mueen Nawaz <m.n### [at] ieeeorg> wrote:
>         If you DO want to read a whole line, look up the getline function. That 
> will also take in an "empty" line.

  The problem with getline() is precisely that it reads an entire line.
If there are several whitespace-separated things in that line, it will
still read the entire line.

  Of course you can give that line to istringstream and then read those
whitespace-separated things with >> (and check with good() to see if it
succeeds).

-- 
                                                          - Warp


Post a reply to this message

From: Warp
Subject: Re: My first C++ program
Date: 20 Sep 2008 05:20:05
Message: <48d4c045@news.povray.org>
Orchid XP v8 <voi### [at] devnull> wrote:
> Yup. A simple and obvious mistake. But unfortunately, not one the 
> compiler warned me about.

  Than turn on the warning flags. You should always compile with warning
flags.

g++ -Wall -Wextra -pedantic -ansi -O3 yourprogram.cc

  Learn to use makefiles too. You can use the attached generic makefile
to automatically build your program. (You'll probably need gnu make.)

  (Note that if you add, remove or change any #include line in your
program, except those which include standard libraries, you'll have to
delete the .dep file before running make.)


Post a reply to this message


Attachments:
Download 'Makefile.txt' (1 KB)

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

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