Outpost Universe Forums

Off Topic => General Interest => Topic started by: Larrythepoet on April 18, 2010, 06:25:52 PM

Title: I'm Trying To Learn C++
Post by: Larrythepoet on April 18, 2010, 06:25:52 PM
Alright, I've decided to start learning C++, but I am having a bit of trouble with it. I'm using code::blocks but I can't build the program I'm trying to run. Does anyone know a good compiler that's easy on beginners?
Title: I'm Trying To Learn C++
Post by: CK9 on April 18, 2010, 06:41:15 PM
"not I" said the duck
Title: I'm Trying To Learn C++
Post by: Hooman on April 18, 2010, 06:41:47 PM
I suppose you can try the free version from Microsoft. There used to be a Visual Studio 2008 Express editition, although, now it appears to be 2010.
http://msdn.microsoft.com/en-us/vstudio/default.aspx (http://msdn.microsoft.com/en-us/vstudio/default.aspx)

Or there's always good old g++ if you're on Linux.

I'd recommend the Microsoft Visual Studio C++ compiler for most Windows development.
 
Title: I'm Trying To Learn C++
Post by: Sirbomber on April 18, 2010, 07:07:37 PM
Uhhh... So, you can't get something to compile, so obviously it must be the compiler's fault...?

Have you tried "Hello world"?
Title: I'm Trying To Learn C++
Post by: Larrythepoet on April 18, 2010, 08:27:13 PM
Yes, It says there is an incompatible compiler and that nothing can be done...

""Hello World - Debug" uses an invalid compiler. Skipping...
Nothing to be done."
Title: I'm Trying To Learn C++
Post by: Kayedon on April 18, 2010, 08:58:37 PM
Try copy-pasting some HelloWorld code from a website tutorial and see if it compiles. If not, you must be really good at breaking things.

Or, the more likely cause is you're doing something wrong. No offence meant, that's just the basic when someone picks up a program and has no idea what to do.
Title: I'm Trying To Learn C++
Post by: Hooman on April 18, 2010, 11:41:43 PM
CodeBlocks isn't a C++ compiler. It's just an IDE. You need to supply a compiler separately. Usually it uses the Visual Studio compiler. Microsoft used to release the compiler without the IDE for free, so people used that with CodeBlocks. Now Microsoft releases a full IDE with their compiler, so there is probably little reason to use CodeBlocks. Plus, the few times I used CodeBlocks, it had a bit of a work in progress feel to it. I suspect it was never quite fully complete, and I kind of doubt there will be as much incentive to work on it with Microsoft's new free bundled packages.

I could be wrong though. CodeBlocks might be more cross platform or something. But it's still likely more work to setup. Just using Visual Studio is probably easier if that's an option for you. Considering it's free, and most internet connections these days won't choke on it's size, I'd say try that one first.
 
Title: I'm Trying To Learn C++
Post by: Larrythepoet on April 19, 2010, 07:39:04 AM
Alright, I thought the compiler was in Code::blocks... I'll try that tonight, thanks
Title: I'm Trying To Learn C++
Post by: TH300 on April 19, 2010, 09:54:02 AM
You could also use gcc as compiler: http://wiki.codeblocks.org/index.php?title...ler#MinGW.2FGCC (http://wiki.codeblocks.org/index.php?title=Installing_a_supported_compiler#MinGW.2FGCC)
Title: I'm Trying To Learn C++
Post by: CK9 on April 19, 2010, 11:23:07 AM
gcc is what a lot of college courses in C++ will have you use (but most of them will prefer unix/linux because it is, by far, a lot more coder friendly)
Title: I'm Trying To Learn C++
Post by: Larrythepoet on April 19, 2010, 04:13:09 PM
Ok, I made my first program with visual studios, thanks guys!
Title: I'm Trying To Learn C++
Post by: Larrythepoet on April 20, 2010, 08:03:19 PM
How do I store a string in a variable? I've tried using a "char" variable but I don't think I am doing it right...


char result;
...
...
if (x = y)
    {
        result="the same";
    }
Title: I'm Trying To Learn C++
Post by: Sirbomber on April 20, 2010, 08:15:13 PM
Have you tried string?  :P  
Title: I'm Trying To Learn C++
Post by: Larrythepoet on April 20, 2010, 08:23:05 PM
*cough* That might work... Thanks
Title: I'm Trying To Learn C++
Post by: Larrythepoet on April 20, 2010, 09:15:06 PM
Yay! I've written my first program. You type two different numbers and then it tells you if they are equal, the difference, the sum and the product!
Title: I'm Trying To Learn C++
Post by: Spikerocks101 on April 21, 2010, 12:18:00 AM
can it tell you which one will win a 100 meter dash?
Title: I'm Trying To Learn C++
Post by: CK9 on April 21, 2010, 01:41:20 AM
yea, the one that is lesser will win, as it has less weighing it down :P
Title: I'm Trying To Learn C++
Post by: Hooman on April 21, 2010, 01:49:51 AM
I suppose I should mention that the "char" datatype only holds a single character. To hold more than one, you'd need an array of chars. Of course that brings up the issues of array length, and how arrays are treated (nearly) identically as pointers.

The array length is a memory allocation issue. Either set a fixed static size that is guaranteed to be big enough, or use dynamic memory allocation, which requires explicit new/delete (or malloc/free) calls, which can be expensive if called frequently.

The pointer issue is important when doing comparisons. If you wrote the code you have above, it would compare the pointers to each other. That is, it would compare the addresses of two strings in memory, rather than the contents of the two strings. Hence two strings with identical characters would compare differently because they are stored in different memory locations. You'd need to use a string comparison function, such as strncmp (or strcmp, if you don't know a max length, and there is no possibility of missing null terminators).

There are a number of string classes in C++ that basically wrap char arrays to handle the details for you, such as the CString class. I usually just end up using raw char arrays though, but then I don't usually do anything fancy with strings.

 
Title: I'm Trying To Learn C++
Post by: TH300 on April 21, 2010, 12:30:28 PM
I strongly advice to use the STL string class. Its simpler than char arrays and part of the C++ standard (i.e. supported by every C++ compiler).

(There can still be reasons for using char arrays, such as keeping the binary small or in some rare cases avoiding some of the additional overhead)
Title: I'm Trying To Learn C++
Post by: CK9 on April 21, 2010, 01:35:05 PM
and a char array would be great for an anagram solver
Title: I'm Trying To Learn C++
Post by: Larrythepoet on April 23, 2010, 04:19:48 PM
:blink:  
Title: I'm Trying To Learn C++
Post by: Sirbomber on April 23, 2010, 04:24:13 PM
Quote
:blink:
Quote
Yay! I've written my first program. You type two different numbers and then it tells you if they are equal, the difference, the sum and the product!
You probably should have stopped reading after that post.
Title: I'm Trying To Learn C++
Post by: Larrythepoet on April 24, 2010, 11:13:22 AM
I'll re-read it in a few weeks :P