Author Topic: I'm learning C++!  (Read 11024 times)

0 Members and 1 Guest are viewing this topic.

Offline imo_inx

  • Manman, SaviourOfTheMultiverse!
  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 473
  • Rating: +27/-8
  • imo_inx
    • View Profile
I'm learning C++!
« 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?


Offline ajorians

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 105
  • Rating: +47/-0
    • View Profile
Re: I'm learning C++!
« Reply #1 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! :)


Offline harold

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 226
  • Rating: +41/-3
    • View Profile
Re: I'm learning C++!
« Reply #2 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.
Blog about bitmath: bitmath.blogspot.nl
Check the haroldbot thread for the supported commands and syntax.
You can use haroldbot from this website.

Offline Streetwalrus

  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3821
  • Rating: +80/-8
    • View Profile
Re: I'm learning C++!
« Reply #3 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:

Offline shmibs

  • しらす丼
  • Administrator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2132
  • Rating: +281/-3
  • try to be ok, ok?
    • View Profile
    • shmibbles.me
Re: I'm learning C++!
« Reply #4 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.
« Last Edit: June 10, 2013, 05:13:46 pm by shmibs »

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: I'm learning C++!
« Reply #5 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;

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline Madskillz

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 488
  • Rating: +32/-2
    • View Profile
Re: I'm learning C++!
« Reply #6 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.

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: I'm learning C++!
« Reply #7 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.

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: I'm learning C++!
« Reply #8 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.

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline XiiDraco

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 513
  • Rating: +32/-5
  • Forget the numbers, just call me, Recreation.
    • View Profile
    • Black-Lark Games
Re: I'm learning C++!
« Reply #9 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!
« Last Edit: June 19, 2013, 01:54:49 pm by XiiR3CR34T10N »

Offline Legimet

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 336
  • Rating: +29/-0
    • View Profile
Re: I'm learning C++!
« Reply #10 on: June 19, 2013, 03:16:59 pm »
I wouldn't recommend using goto's and labels.

Also, using namespace std is discouraged, 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.

Offline Lionel Debroux

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2135
  • Rating: +290/-45
    • View Profile
    • TI-Chess Team
Re: I'm learning C++!
« Reply #11 on: June 19, 2013, 03:19:13 pm »
Agreed, "using namespace std;" is extra-lazy.
Member of the TI-Chess Team.
Co-maintainer of GCC4TI (GCC4TI online documentation), TILP and TIEmu.
Co-admin of TI-Planet.

Offline Madskillz

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 488
  • Rating: +32/-2
    • View Profile
Re: I'm learning C++!
« Reply #12 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. 

Offline XiiDraco

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 513
  • Rating: +32/-5
  • Forget the numbers, just call me, Recreation.
    • View Profile
    • Black-Lark Games
Re: I'm learning C++!
« Reply #13 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!

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: I'm learning C++!
« Reply #14 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.