PDA

View Full Version : [C++] Tutorial 2: variables and easy input


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.

evelstar7
11-23-2008, 07:28 PM
nice tut but i never realy got c++ so i will stay with gfx i always wanted to make a peace of software like i a dodian skill calculater

Arsenal
11-23-2008, 11:04 PM
Just a hint guys. Cout is like Echo for Java or like <p></p> for HTML. Just to help you guys out. =] Yay! You did part two! Hehe.

Thiefmn6092
12-03-2008, 06:05 PM
Hi, reasonably good tutorial but you have made some mistakes to. Variables can be thought of as cubbies, sure, however, your example with a character array to store console input is to specific. You need to explain how your example is specific to a console input and not for anything. Also, what happens when your name is longer than 40 characters? You should be using std::string for this, not a raw array. You can find this in string, or <string>.

ToshiXZ
12-10-2008, 09:50 PM
I do know strings, but that was too complex for the first day or two someone is starting c++. And if they want to change it, then change the 40 to whatever you want? And since there isn't much of an advantage using strings here, and since strings aren't build in to c++, you can't do switch statements etc. Use chars if you can, strings if you can't.

kuzmin
12-10-2008, 09:57 PM
What program can I download, that I can make programs within?..soz i have no experience in this field.

ToshiXZ
12-10-2008, 10:07 PM
Posted it in [C++] Your first program.
Check it out, same section:
http://dodian.com/forums/showthread.php?t=37759

Kyle
01-03-2009, 12:05 PM
Just search on google for DevC++ and you can download it and start making programs.
~kyle

Underdog5
04-21-2009, 08:36 AM
Thanks dude, now I can start to code stuff that actually has no errors!!