ToshiXZ
11-18-2008, 10:58 PM
Well now that you have your IDE, you know what c++ is (roughly), and you've typed good old hello world, it's time for you to start real programming!!
What I'm going to teach today is amazingly simple, just as easy or easier then hello world, but is actually used in every single c++ program that does much of anything (variables at least, some GUI(Graphical User Interface) programs don't use cin, but almost all DOS ones do).
Part 1
The explanation part:
I'm going to explain variables quick, because it's a huge topic. When you buy a new computer, it may have 2GB ram. 1024 bytes = 1kb. 1024kb = 1mb. 1024 mb = 1gb. You get the idea, gigabytes are millions of bytes. Each byte is basically like a cubby where you can store something. When we create variables, we basically grab a cubby, slap a name tag on it, and throw our stuff into it.
Part 2
The code!:
Now I'm going to actually teach you the code.
To make a variable, or DECLARE it as many programmers say, type this right under your int main()
char name[40];
This is what that code does.
Grabs a cubby, and slaps a nametag on it. The nametag says that the cubby will be holding characters, and that the cubby shall be referrred to as "name". The [40] indicates how many characters shall be stored in it.
Now let's store something in the variable ok?
To store the stuf in the cubby name, use:
cin >> name;
Let's say we want to ask what the user's name is, and then say hello to them.
I'm going to just show you the whole program which does that:
#include <iostream>
using namespace std;
int main ()
char name[40]
{
cout << "Hello! What is your name?\n";
cin >> name;
cout << "Welcome to my program " << name;
system("PAUSE");
return 0;
}
Compile it, and if you have any questions, post it!!!
Part 3
The unexplained:
Well, you still don't understand all the code we've been typing so far, so I'm going to explain it to you now.
system("PAUSE"); runs pause.exe in C:/Windows/system32/
So right away we know this won't work on Mac or Linux.
return 0; basically it tells Windows (or Mac or Linux) that the program has finished, over and out! And then the program shuts down.
When we put ""s in cout, we want it to show the exact text we typed. So what about name? Without ""s. Basically it prints the value (what's in the cubby) named name. So whatever the user entered!.
\n means the line is done, go to the next line.
What I'm going to teach today is amazingly simple, just as easy or easier then hello world, but is actually used in every single c++ program that does much of anything (variables at least, some GUI(Graphical User Interface) programs don't use cin, but almost all DOS ones do).
Part 1
The explanation part:
I'm going to explain variables quick, because it's a huge topic. When you buy a new computer, it may have 2GB ram. 1024 bytes = 1kb. 1024kb = 1mb. 1024 mb = 1gb. You get the idea, gigabytes are millions of bytes. Each byte is basically like a cubby where you can store something. When we create variables, we basically grab a cubby, slap a name tag on it, and throw our stuff into it.
Part 2
The code!:
Now I'm going to actually teach you the code.
To make a variable, or DECLARE it as many programmers say, type this right under your int main()
char name[40];
This is what that code does.
Grabs a cubby, and slaps a nametag on it. The nametag says that the cubby will be holding characters, and that the cubby shall be referrred to as "name". The [40] indicates how many characters shall be stored in it.
Now let's store something in the variable ok?
To store the stuf in the cubby name, use:
cin >> name;
Let's say we want to ask what the user's name is, and then say hello to them.
I'm going to just show you the whole program which does that:
#include <iostream>
using namespace std;
int main ()
char name[40]
{
cout << "Hello! What is your name?\n";
cin >> name;
cout << "Welcome to my program " << name;
system("PAUSE");
return 0;
}
Compile it, and if you have any questions, post it!!!
Part 3
The unexplained:
Well, you still don't understand all the code we've been typing so far, so I'm going to explain it to you now.
system("PAUSE"); runs pause.exe in C:/Windows/system32/
So right away we know this won't work on Mac or Linux.
return 0; basically it tells Windows (or Mac or Linux) that the program has finished, over and out! And then the program shuts down.
When we put ""s in cout, we want it to show the exact text we typed. So what about name? Without ""s. Basically it prints the value (what's in the cubby) named name. So whatever the user entered!.
\n means the line is done, go to the next line.