antiloquax
Posts: 406
Joined: Sun Nov 20, 2011 11:37 am
Contact: Website

Re: My first C++ program

Wed Apr 18, 2012 7:26 pm

I don't know much C++ yet. But here's my first program! I called it "firstprog.cpp".


#include <stdio.h>

int main()
{
    int i;
    for(i=0; i<10; i++)
    {
        puts("Hello, world!n");
    }
    return 0;
}


To make the program run, go to the directory where you saved it. Type:


g++ firstprog.cpp -o firstprog


This will compile your program into an executable called "firstprog".

Then run it with:


./firstprog


Here's a screen-shot:


Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: My first C++ program

Wed Apr 18, 2012 7:30 pm

I'm not telling you anything you don't already know, but that's really just C.

I'm pretty sure somewhere along the way, someone wrote an object-oriented C++ hello, world.  Probably one using templates, too.

And any of the other 63000 fancy features that make up the beast known as C++.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26398
Joined: Sat Jul 30, 2011 7:41 pm

Re: My first C++ program

Wed Apr 18, 2012 7:39 pm

Congratulations! And kudos for using the Pi for what's it's intended!
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

antiloquax
Posts: 406
Joined: Sun Nov 20, 2011 11:37 am
Contact: Website

Re: My first C++ program

Wed Apr 18, 2012 7:53 pm

@ JamesH thanks

@JoeSchmoe I am only a beginner with C++. I'll do something properly object oriented next time!

Narishma
Posts: 151
Joined: Wed Nov 23, 2011 1:29 pm

Re: My first C++ program

Thu Apr 19, 2012 7:02 am

antiloquax said:


@ JamesH thanks

@JoeSchmoe I am only a beginner with C++. I'll do something properly object oriented next time!


A more C++-ish hello world program would look something like:


#include <iostream>

int main()

{

for (int i = 0 ; i < 10 ; i++)

{

std::cout << "Hello world!" << std::endl;

}

return 0;

}


User avatar
teh_orph
Posts: 346
Joined: Mon Jan 30, 2012 2:09 pm
Location: London
Contact: Website

Re: My first C++ program

Thu Apr 19, 2012 7:25 am

I'm trying to think how C++ hello world can be completely obfuscated with templates...any one got an idea? (nothing "too over the top" though )

AlexPT
Posts: 47
Joined: Mon Mar 05, 2012 11:51 am

Re: My first C++ program

Thu Apr 19, 2012 8:53 am

Nice going mate. Now keep going and improve your self!

Good point to start, I start programming on a ti-83(Assembly) and then TPascal, C and some VB/html. Now I'm more into Java

A good way to think, in the star, is to made algorithm in flowchart

john_wage
Posts: 156
Joined: Thu Mar 22, 2012 6:20 am
Contact: Website

Re: My first C++ program

Thu Apr 19, 2012 8:57 am

The entire idea behind Templates in C++ is to make very large projects more easily maintained. Using them in a small application doesn't hurt of course, but by the time you are making projects that would take any significant use of templates you'd already be way past the beginner stages of C++.

There is no shame in making an entire application in code that just happens to be backwards compatible with C.  Just because you are "programming in C++" doesn't mean you MUST use C++ unique features, it just means you keep your options open to implement them, should the opportunity arise.

antiloquax
Posts: 406
Joined: Sun Nov 20, 2011 11:37 am
Contact: Website

Re: My first C++ program

Thu Apr 19, 2012 10:09 am

Yeah, I just wanted to post up an example of how to use the g++ compiler really.


lb
Posts: 263
Joined: Sat Jan 28, 2012 8:07 pm

Re: My first C++ program

Thu Apr 19, 2012 3:45 pm

Needless to say C++ is a horrible language for starting to learn programming. Stick to C or use something like Python.

john_wage
Posts: 156
Joined: Thu Mar 22, 2012 6:20 am
Contact: Website

Re: My first C++ program

Thu Apr 19, 2012 4:07 pm

@lb

Depends what your entry level is really. If you are completely new to programming at all then yeah, there are better ways to get into the "programming mindset".

If you have some experience from maybe HMTL, Java-Script, or other easy, well documented scripting languages then getting in to C++ isn't that big of a deal in my onion.

And also from my point of view there is no real point tunneling down on C instead of C++, C++ isn't harder really if you have no experience with either. Just because some of the high-end features that people sometimes use to advocate why C++ is better than C, are too advanced for a beginner, there is nothing that says you have to learn that stuff right away, the "easy stuff" is just as easy in C++ as it is in C.

JonB
Posts: 220
Joined: Tue Nov 29, 2011 9:26 pm

Re: My first C++ program

Thu Apr 19, 2012 7:58 pm

lb said:


Needless to say C++ is a horrible language for starting to learn programming. Stick to C or use something like Python.


Disagree with you on that one.

Typical learning curve for C++ is C -> C and structures -> C++ (structures with methods) -> stream manipulation / STL (to remove the C library idioms) -> Object modelling.

Learning with C may give you bad habits that can be difficult to unlearn. However, it is a good foundation if your end game is C++ or Java because the syntax is so similar. Further, it is very close to the metal and will give you a good understanding of pointers (ie, memory addresses) which are fundamental to understanding how computers actually work.

JonB
Posts: 220
Joined: Tue Nov 29, 2011 9:26 pm

Re: My first C++ program

Thu Apr 19, 2012 8:00 pm

JamesH said:


Congratulations! And kudos for using the Pi for what's it's intended!


That's what I meant to say...!

geeday
Posts: 23
Joined: Mon Jan 23, 2012 10:13 pm

Re: My first C++ program

Fri Apr 20, 2012 8:36 am

lb said:


Needless to say C++ is a horrible language for starting to learn programming. Stick to C or use something like Python.


Do you think you a newbie is better to start programming with C (as suggested here), rather than Python, as C forces you "type" variables, which is arguably the most fundamental thing in writing code?

I started with C++ at uni and all that talk of classes and objects fried my mind back then.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26398
Joined: Sat Jul 30, 2011 7:41 pm

Re: My first C++ program

Fri Apr 20, 2012 9:19 am

geeday said:


lb said:


Needless to say C++ is a horrible language for starting to learn programming. Stick to C or use something like Python.


Do you think you a newbie is better to start programming with C (as suggested here), rather than Python, as C forces you "type" variables, which is arguably the most fundamental thing in writing code?

I started with C++ at uni and all that talk of classes and objects fried my mind back then.


I think that's an interesting question. My Python knowledge is limited, but it does make variables of differing types very easy to use and combine - but is that a bad thing? Will it just make the move to a typed language more difficult?
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

geeday
Posts: 23
Joined: Mon Jan 23, 2012 10:13 pm

Re: My first C++ program

Fri Apr 20, 2012 9:36 am

I think you should start typed, then move to scripts  (starting with objects in Python might be good given C++ syntax is horrific.). That way you have a fundamental understanding.

you should understand this kind of stuff:

print "%d" % 44.234 #i assume this would print 44. but you get me

Given my background is maths/engineering, perhaps I am more interested in precision which is one area where you have to understand types.

All in all, this is the boring way and will give less visual results earlier, so just write code and let people correct you afterwards!!!

antiloquax
Posts: 406
Joined: Sun Nov 20, 2011 11:37 am
Contact: Website

Re: My first C++ program

Fri Apr 20, 2012 9:53 am

I'm actually not really trying to learn C or C++ at the moment. I have mainly been doing Python and Java.

I've posted a couple of Python games on this forum:

http://www.raspberrypi.org/for.....-to-python

And I did a little post on Java on my blog:

http://teampython.wordpress.co.....a-program/

(I haven't got a JDK running on my RPi emulation yet.


Return to “Beginners”