Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: imo_inx on June 10, 2013, 03:33:54 pm

Title: I'm learning C++!
Post by: imo_inx on June 10, 2013, 03:33:54 pm
I'm currently learning C++.
This is my first program. It's a small text-adventure.
I'm still just learning though...
Anything I should know?
Title: Re: I'm learning C++!
Post by: ajorians on June 10, 2013, 04:10:34 pm
Hi iNk&Venom,

I'd consider this pretty good for just starting!

All of your program is in the main function; if you want to make it easier to read you can separate things out into more functions.  For the mostpart you could translate your code to C easily (cout -> printf, string == -> strcmp, etc).  Eventually you'll try to do object-orientated programming; which is a whole new paradigm.

I'd suggest looking into for and while loops.  I started out using goto's as well; and they even exist in C# but replacing gotos with loops really do make the code easier to follow.  Keep learning! :)

Title: Re: I'm learning C++!
Post by: harold on June 10, 2013, 04:23:48 pm
I believe there are execution paths in which for example "sanity" is read without ever having been written to (though I admit I did not look that closely), that could give trouble

Simple way to fix that would be to just set them to some value when declaring them.
Title: Re: I'm learning C++!
Post by: Streetwalrus on June 10, 2013, 04:31:02 pm
Hmmm... I compiled it with g++ (I'm on Arch Linux) and it won't let me press enter. :banghead:
Title: Re: I'm learning C++!
Post by: shmibs on June 10, 2013, 04:53:39 pm
this isn't at all the sort of thing that c++ is designed to do. c++ is a language made to work with complex, constantly changing projects in a way that remains both fast and reliable. furthermore, c++ is not at all a good language for a beginner, as it is jammed full of advanced features that are fantastic for professionals but make it very difficult to understand for anyone else. you have to approach the language holistically, learning basically everything at once, or you'll never get what things are doing.

if you really want to learn c++, my suggestion would be to first master c, so that you at least know how basic program flow, like loops, conditionals, breaks, and cases; basic pointers; scope; headers; functions; and data structures, like arrays, structs, enums, etc, work. if you don't understand all of those perfectly you'll never get anywhere with c++ and will end up abusing it in ways it's not meant to be used. (EDIT: if you happen to have a calculator, then axe is an even better place to start, from which you can move on to c and then to c++, because the simplified instruction set and easier to understand approach to pointers [possible because of the calculator's simpler memory layout] are more beginner-friendly) if you want to write a program like this one, though, that's mostly text manipulation, go use a high-level scripting language. i'd recommend perl, because i hate python, but most people disagree with that :P

EDIT: also, as a word of warning:
Code: [Select]
if (torch == 0)
{
            cout << "The tunnel is too dark to explore.\n";
            goto ex;
}
is the usual way of writing a statement like this, and
Code: [Select]
if (torch == 0) {
            cout << "The tunnel is too dark to explore.\n";
            goto ex;
}
is also acceptable (and the way i prefer it). even
Code: [Select]
if (torch == 0) { cout << "The tunnel is too dark to explore.\n"; goto ex; }is sometimes used for really small statements. if you ever send a program with
Code: [Select]
if (torch == 0)
{cout << "The tunnel is too dark to explore.\n";
goto ex;}
in it to another programmer, however, he will almost certainly label you as an idiot.
Title: Re: I'm learning C++!
Post by: Sorunome on June 14, 2013, 03:07:19 am
Wut, you use labels, lol
I sould try not to use labels
And then instead of cout'ing \n I would use endl as it also flushes, e.g.
cout << "The tunnel is too dark to explore." << endl;
Title: Re: I'm learning C++!
Post by: Madskillz on June 18, 2013, 11:33:30 pm
shmibs is right on a few aspects...c++ isn't for beginners, however I never learned c first. I came from a java background. Pointers are going to be something you want to learn they are pretty important and powerful. Scoping and headers are probably another good area to read up on. How classes work would be in there too. Object Oriented Languages are awesome...

As for where to place the brackets in your code like in the four examples that shmibs gave. As he said the first two are usually the standard way of placing them. Go with which one makes it easier for you to read your code.
Title: Re: I'm learning C++!
Post by: Juju on June 19, 2013, 03:04:11 am
Pointers should give you a headache for sure. I think it's pretty much the most difficult part of C's syntax.

Also gotos and labels are usually discouraged.
Title: Re: I'm learning C++!
Post by: Sorunome on June 19, 2013, 01:38:42 pm
I did C++ after doing php and i'm still not that good at all that pointer stuff ^^
And I use the 2nd way shmibs showed for the brackets, it is IMO just the way to see best where what is.
Title: Re: I'm learning C++!
Post by: XiiDraco on June 19, 2013, 01:48:18 pm
Hi iNk&Venom,

I'd consider this pretty good for just starting!

All of your program is in the main function; if you want to make it easier to read you can separate things out into more functions.  For the mostpart you could translate your code to C easily (cout -> printf, string == -> strcmp, etc).  Eventually you'll try to do object-orientated programming; which is a whole new paradigm.

I'd suggest looking into for and while loops.  I started out using goto's as well; and they even exist in C# but replacing gotos with loops really do make the code easier to follow.  Keep learning! :)



Ha, I didn't know there were goto's in C#, and thats the main language I use, lol.

this isn't at all the sort of thing that c++ is designed to do. c++ is a language made to work with complex, constantly changing projects in a way that remains both fast and reliable. furthermore, c++ is not at all a good language for a beginner, as it is jammed full of advanced features that are fantastic for professionals but make it very difficult to understand for anyone else. you have to approach the language holistically, learning basically everything at once, or you'll never get what things are doing.

if you really want to learn c++, my suggestion would be to first master c, so that you at least know how basic program flow, like loops, conditionals, breaks, and cases; basic pointers; scope; headers; functions; and data structures, like arrays, structs, enums, etc, work. if you don't understand all of those perfectly you'll never get anywhere with c++ and will end up abusing it in ways it's not meant to be used. (EDIT: if you happen to have a calculator, then axe is an even better place to start, from which you can move on to c and then to c++, because the simplified instruction set and easier to understand approach to pointers [possible because of the calculator's simpler memory layout] are more beginner-friendly) if you want to write a program like this one, though, that's mostly text manipulation, go use a high-level scripting language. i'd recommend perl, because i hate python, but most people disagree with that :P

EDIT: also, as a word of warning:
Code: [Select]
if (torch == 0)
{
            cout << "The tunnel is too dark to explore.\n";
            goto ex;
}
is the usual way of writing a statement like this, and
Code: [Select]
if (torch == 0) {
            cout << "The tunnel is too dark to explore.\n";
            goto ex;
}
is also acceptable (and the way i prefer it). even
Code: [Select]
if (torch == 0) { cout << "The tunnel is too dark to explore.\n"; goto ex; }is sometimes used for really small statements. if you ever send a program with
Code: [Select]
if (torch == 0)
{cout << "The tunnel is too dark to explore.\n";
goto ex;}
in it to another programmer, however, he will almost certainly label you as an idiot.

Or C#, It's really easy for beginners. Personally, I find the first way you described to be my favorite.

shmibs is right on a few aspects...c++ isn't for beginners, however I never learned c first. I came from a java background. Pointers are going to be something you want to learn they are pretty important and powerful. Scoping and headers are probably another good area to read up on. How classes work would be in there too. Object Oriented Languages are awesome...

As for where to place the brackets in your code like in the four examples that shmibs gave. As he said the first two are usually the standard way of placing them. Go with which one makes it easier for you to read your code.

Funny I came from a Java background too, then I realized it was... Well... Quite a selfish language. So now I use C# to make games for PlayStation network :D!
Title: Re: I'm learning C++!
Post by: Legimet on June 19, 2013, 03:16:59 pm
I wouldn't recommend using goto's and labels.

Also, using namespace std is discouraged (http://www.parashift.com/c++-faq/using-namespace-std.html), because the whole point of namespaces is to prevent namespace collisions. Instead, just prefix stuff with std::, it's really not that much to type.
Title: Re: I'm learning C++!
Post by: Lionel Debroux on June 19, 2013, 03:19:13 pm
Agreed, "using namespace std;" is extra-lazy.
Title: Re: I'm learning C++!
Post by: Madskillz on June 22, 2013, 09:34:59 pm
Quote
Funny I came from a Java background too, then I realized it was... Well... Quite a selfish language. So now I use C# to make games for PlayStation network

I've been doing mobile development for the last few years. Mainly Objective-C, I had also done some homebrew development on a psp back when it first came out. I really like what Sony has done in relation to their whole SDK policy for the mobile and Vita stuff. 
Title: Re: I'm learning C++!
Post by: XiiDraco on June 26, 2013, 10:33:59 pm
Quote
Funny I came from a Java background too, then I realized it was... Well... Quite a selfish language. So now I use C# to make games for PlayStation network

I've been doing mobile development for the last few years. Mainly Objective-C, I had also done some homebrew development on a psp back when it first came out. I really like what Sony has done in relation to their whole SDK policy for the mobile and Vita stuff. 
You aren't the only one. Since PSM publishing license is now free, I actually have something to work forward to that is free!
Title: Re: I'm learning C++!
Post by: DJ Omnimaga on June 27, 2013, 02:32:00 am
Indeed, I hate how on the 360 and iOS it costs like $100 to publish stuff and in Xbox case you need to pay every year. Kinda like music on big sites, so if your sales are lower than your payments, you lose money.
Title: Re: I'm learning C++!
Post by: Streetwalrus on June 28, 2013, 06:42:54 am
iOS requires yearly payment too. At least google only wants $25 on account  creation. :D
Title: Re: I'm learning C++!
Post by: Xeda112358 on June 28, 2013, 07:36:43 am
Cool, I just started really learning C++ a few days ago, too! I finally made the start of a Mandelbrot set explorer yesterday :)

I basically just used Google to look up how to do things. If I know what the code is supposed to do, then I can usually figure out what each part of the code is doing and that is how I am learning new things. My code is pretty ugly, but I am hoping it will get better as I get more experience XD

Here is a video : https://docs.google.com/file/d/0B4HNIXQZLWM8ZzJUV2NFa3V1LU0/edit?usp=sharing

It makes me really strain my brain to learn this stuff, but it is rewarding!

EDIT: It is so much more of a pain to set up than TI-BASIC x.x
Title: Re: I'm learning C++!
Post by: Joshuasm32 on June 28, 2013, 08:45:16 am
Yay, Xeda!  Something other than calculator programming?  [I remember from TI|BD]  Good for you!   :thumbsup:  C++ is very similar to TI-BASIC.  In fact, other than a few changes in syntax, C++ (particularly iostream) is quite similar.
Title: Re: I'm learning C++!
Post by: Lionel Debroux on June 28, 2013, 08:47:39 am
Indeed, C++ has fewer, and different, abstractions than TI-BASIC has. They tackle different use cases and learning curves.
Title: Re: I'm learning C++!
Post by: Streetwalrus on June 28, 2013, 10:30:50 am
Looks pretty cool Xeda ! :D
Title: Re: I'm learning C++!
Post by: Joshuasm32 on June 28, 2013, 01:19:36 pm
Xeda, if you want some really great C++ tutorials, subscribe to "skeshatter" on Youtube or check out www.beginnerscpp.com.

(BTW: I am giving Zeda tutorial links?  This is strange...  XD)
Title: Re: I'm learning C++!
Post by: shmibs on June 28, 2013, 09:16:45 pm
Indeed, C++ has fewer, and different, abstractions than TI-BASIC has. They tackle different use cases and learning curves.

they're definitely different, but i'd say that it abstracts to a far greater degree than any form of BASIC. what comparison is there to be made to a language with function and class templates?

also, if we're handing out tutorial links, i'd point to this XD (http://programming-motherfucker.com/become.html#C%20/%20C++).
Title: Re: I'm learning C++!
Post by: DJ Omnimaga on June 30, 2013, 09:14:25 pm
I always liked mandelbrots because of how it can zoom in infinitively.