It appears you have not yet registered with our community. To register please click here.

Origin XT RPG Network Home







XT Project Introduction
Blah blah intro to this place. To be composed later.

 

C++ - Tutorial, Lesson 2, User Input and Variables


Mar 6 2007, 05:37 AM (Post #1)
Well why can't we do the shuffle?!
* * * * * *
Posts: 1,679
Cash: 183,455 / 0
Group: Nobility
Joined: 7/12/03 08:49 PM

Lesson 2 - User Input and Variables


So we've seen a program output some text to the screen, and now it's time to use C++ for things like data. Excited? You should be, because now we can get down and dirty with this language.

Example One


CODE

#include <iostream>
using namespace std;

int main()
{
int data1;
float data2;
double data3;

system("PAUSE");
return 0;

}

So what does this program do? Not much. It reserves space for three variables, pauses the system, then exits. I paused here to explain to you what these different variables mean.

int data1; - This declares a variable called "data1" of the data type integer. Used to hold numbers only.
float data2; - This declares a variable called "data2" of the data type float. Used to hold numbers only.
double data3; - This declares a variable called "data3" of the data type double. Used to hold numbers only.

So what the hecks the difference anyway? Besides tne names, it appears that there is no difference between these three data types, all of them are used for numbers after all. However, there is a significant difference. We'll see it in this example.

Example Two


CODE

#include <iostream>
using namespace std;

int main()
{
int data1;
data1 = 1.34543;
cout<<data1<<endl;

system("PAUSE");
return 0;

}

Compile this code and run it in your compiler. You should see this:
CODE

1
Press any key to continue...

Well, what the heck? It only displayed 1 and not the whole number! What happened to the decimals? Lets look at the same example, except using float.

Example Three


CODE

#include <iostream>
using namespace std;

int main()
{
float data1;
data1 = 1.34543;
cout<<data1<<endl;

system("PAUSE");
return 0;
}

You should see this:
CODE

1.34543
Press any key to continue...

Well, wow. There's our full number! So what does double do? Well, this will be a good time to clarify the true differences between these three data types.

When an int is declared for a variable, the program will reserve a space of 4 bytes of memory. This will only allow for whole numbers up to the full 32 bits.
When a float is declared for a variable, the program will also reserve a space of 4 bytes of memory.

Now that we've got that down, we can look at other data types.

Example Four


CODE

#include <iostream>
using namespace std;

int main()
{
char data1;
data1 = 'h';
cout<<data1<<endl;

system("PAUSE");
return 0;
}

If you've been following along closely, I'm assuming that you guessed that this program will output h to the screen, and you'd be correct. But what if you wanted it to output 'hi'? Try it and see what happens. You should see that only the i is outputed to the screen. This is because when a char variable is declared, the program only reserves 1 byte in memory for it, and therefore it will only hold one character. But there are ways around this! Let's take a look at my buddy, the string variable.

Example Five


CODE

#include <iostream>
#include <string>
using namespace std;

int main()
{
string data1;
data1 = "Hi!"
cout<<data1<<endl;

system("PAUSE");
return 0;
}

If you compile this, you will notice that with string, the whole message can be displayed! Notice that we used #include <string> in our program. If you wish to use the string data type, you must include this library. Now that we've got some data types down, we can move onto an important topic, which is basic user input.

Example Six


CODE

#include <iostream>
using namespace std;

int main()
{
int input;
int input2;
int result;

cout<<"Please input a number value."<<endl;
cin>>input;
cout<<"Please input another number value."<<endl;
cin>>input2;

result = input + input2;
cout<<"The result is: "<<result<<"."<<endl;

system("PAUSE");
return 0;
}

In this program we got some new stuff going on. We have some basic math involving some variables, and we have two instances where the machine is going to call for some input. If you compile this code, you will notice that the program is going to ask for some numbers. As you may have guessed, cin>> is used to get user input from the keyboard. Notice that the arrows are pointing in the opposite direction of cout<<. This is important to remember, and easy to confuse.

What the program does is get two numbers, one is stored in input and the other in input2. Then, input and input2 are added together and stored in the variable result. result is then displayed.

Now is an important time to discuss the sensativity of variable names. C++ is case sensative. So if you declare a variable called Result, it will be different than result. If you tell the above program to display Result, the compiler will have an error because there is no Result variable declared, but result was declared.

There are also correct and incorrect usages for variable names.
CODE

Correct - Result
result
Result_of_calculation
ResultOfCalculation
Result1ofCalculation1

Incorrect - 1_2_3
1_Result
11122111
*&#FFFA
result 1
and keywords (words that are native to the language or included in a library like cout, cin, case, double, float, int, char, etc.)

Afterword


That is all for this lesson!

Next Lesson:
Lesson 3 - Loops and Arrays: A wonderful team

~Nesticles~

This post has been edited by Jinghao: Jun 16 2007, 01:00 AM
Post Options