First program

First program

Post by STUDIOny on Thu Jul 23, 2009 9:31 am
([msg=27279]see First program[/msg])

So yes its basic, but anyway.
I'm basically just asking if it is OK? I know it works, but I'm looking for ways to improve my coding, making sure nothing I do is bad practice.

Code: Select all
#include <iostream>

using namespace std;

int tempInt1 = 1;
int tempInt2 = 2;
int tempInt3 = 3;
int total1 = tempInt1 + tempInt2;
int total2 = total1 * tempInt3;

int main()
{
    cout << "Hello World!" << endl;
    cout << "We are going to be adding a few numbers:" << endl;
    cout << "First we will add: " << tempInt1 << " + " << tempInt2 << " = " << total1 << endl;
    cout << "Next, we will multiply " << total1 << " * " << tempInt3 << " = " << total2 << endl;
    cout << "Goodbye world!" << endl;
    return 0;
}


Trying now to think of what I could design, for a little practice.
Any ideas?
STUDIOny
Experienced User
Experienced User
 
Posts: 73
Joined: Thu Feb 05, 2009 7:40 pm
Location: Newtownards, Northern Ireland
Blog: View Blog (0)


Re: First program

Post by BhaaL on Thu Jul 23, 2009 11:02 am
([msg=27283]see Re: First program[/msg])

The best way to start is IMO to look for things you need. Small tools, medium sized helper applications, and soon after that larger programs that will do major tasks.

Think of a goal, look up ways to achieve it, and just get into it. Trial and Error is a good way to learn things (especially learn of mistakes you made), since mistakes happen to be more present in memory than casual stuff that isnt out of the ordinary.
Also, if possible, try to get someone to look at your code and provide hints/improvements. Like here on the forums, on IRC or from other friends/professionals you know.
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: First program

Post by tgoe on Thu Jul 23, 2009 1:45 pm
([msg=27294]see Re: First program[/msg])

Generally, you'll want to define variables in the scope that they are used. In larger programs, tracking down everything that happens to a global variable is pretty boring.
User avatar
tgoe
Contributor
Contributor
 
Posts: 638
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)


Re: First program

Post by STUDIOny on Fri Jul 24, 2009 7:58 am
([msg=27348]see Re: First program[/msg])

So in regard to this, it's as simple as changing my original code to:

Code: Select all
#include <iostream>

using namespace std;

int main()
{
int tempInt1 = 1;
int tempInt2 = 2;
int tempInt3 = 3;
int total1 = tempInt1 + tempInt2;
int total2 = total1 * tempInt3;

    cout << "Hello World!" << endl;
    cout << "We are going to be adding a few numbers:" << endl;
    cout << "First we will add: " << tempInt1 << " + " << tempInt2 << " = " << total1 << endl;
    cout << "Next, we will multiply " << total1 << " * " << tempInt3 << " = " << total2 << endl;
    cout << "Goodbye world!" << endl;
    return 0;
}
STUDIOny
Experienced User
Experienced User
 
Posts: 73
Joined: Thu Feb 05, 2009 7:40 pm
Location: Newtownards, Northern Ireland
Blog: View Blog (0)


Re: First program

Post by thedotmaster on Fri Jul 24, 2009 10:48 am
([msg=27353]see Re: First program[/msg])

I find this site pretty good for when starting new languages: http://projecteuler.net/
Good for practising algorithm skills.
Image
User avatar
thedotmaster
Contributor
Contributor
 
Posts: 984
Joined: Sun May 04, 2008 4:39 pm
Location: North West UK
Blog: View Blog (1)


Re: First program

Post by CloneDark on Fri Jul 24, 2009 11:40 am
([msg=27356]see Re: First program[/msg])

Try to avoid using iostream in C++. Instead, it is better to go with C's printf (which stands for print function, it writes to your console). so your code should read:

Code: Select all
#include <stdio.h>

/* I always put void in empty functions. It is perfectly legal C++, and I guess it carries over from my C days. Just looks nicer, does the same thing as leaving it blank */
int main( void )
{
    int tempInt1 = 1;
    int tempInt2 = 2;
    int tempInt3 = 3;
    int total1 = tempInt1 + tempInt2;
    int total2 = total1 * tempInt3;

    // \n = newline, I guess the same as endl. (endl might append \n\0, but I never use it so~)
    printf("Hello World!\n");
    printf("We are going to be adding a few numbers:\n");
    // %i = integer placeholder. You could also use %d for decimal, %c for char, %x for hex, %s for string, and so forth.
    // It works by placing the first argument after your string into the first placeholder, then the second argument
    // into the second placeholder. For example:
    /*
    char moo = 'a';
    int cow = 12;
    printf("moo = %c, cow = %i (or 0x%x in hex). I am %i", moo, cow, cow, 1337);
    //Output: "moo = a, cow = 12 (or 0x0C in hex). I am 1337"
    */
    printf("First we will add: %i + %i = %i\n", tempInt1, tempInt2, total1);
    printf("Next, we will multiply %i * %i = %i\n", total1, tempInt3, total2);
    printf("Goodbye world!");
    return 0;
}


There are MANY reasons why you should use stdio over iostream, the major one being iostream has vastly inferior output formatting capabilities. While it can do all the same things as printf, it is very counter-intuitive, very obscure how it works, and requires much much more code than it should.
CloneDark
New User
New User
 
Posts: 1
Joined: Fri Jul 24, 2009 11:24 am
Blog: View Blog (0)


Re: First program

Post by BhaaL on Fri Jul 24, 2009 11:54 am
([msg=27360]see Re: First program[/msg])

It's print formatted, not print function.
Try to avoid giving biased hints to newcomers that still don't know their way around, unless you give a good reason to do so.
cout might lack the sophisticated formatting specifiers printf has, but it is simply the-C++-way. Plus, I doubt he knows about format specifiers; you didn't use any fancy ones either that would warrent for printf to be used instead.

And just for the fancy hex specifier, except he didnt use/need it in first place, this is how you'd do it:
Code: Select all
cout << "this is decimal: " << someInt << ", while this is hex: " << hex << someInt << endl;
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: First program

Post by STUDIOny on Sat Jul 25, 2009 7:11 pm
([msg=27434]see Re: First program[/msg])

CloneDark has just hugely confused me =|

Cheers for the link to: http://projecteuler.net/ DotMaster <3

EDIT: And what difference does using int main ( void ) actually make?
STUDIOny
Experienced User
Experienced User
 
Posts: 73
Joined: Thu Feb 05, 2009 7:40 pm
Location: Newtownards, Northern Ireland
Blog: View Blog (0)


Re: First program

Post by BhaaL on Sun Jul 26, 2009 3:30 am
([msg=27446]see Re: First program[/msg])

In particular, none.
While int, char, float etc are types that hold values, void can be seen as type that is...well...void.

The whole main() thing is just a defined entry point, so that the OS knows what to call when executing your app.
You can use either int main() or int main(int argc, char **argv) if you want command line arguments.
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: First program

Post by STUDIOny on Mon Jul 27, 2009 8:07 am
([msg=27524]see Re: First program[/msg])

Another question about ending lines.

I've been using:

Code: Select all
<< endl;


Ive started to use \n
I can use it in the following example:

Code: Select all
cout << "We are going to be adding a few numbers:\n";


but, where would I include the \n in the following code?

Code: Select all
cout << "First we will add: " << tempInt1 << " + " << tempInt2 << " = " << total1;
STUDIOny
Experienced User
Experienced User
 
Posts: 73
Joined: Thu Feb 05, 2009 7:40 pm
Location: Newtownards, Northern Ireland
Blog: View Blog (0)


Next

Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests