No, type in the listing given to you
exactly as you read it!
DO NOT REPLACE THE STATEMENT "thisisanumber" WITH A NUMBER!
Variables a like buckets - you put something in them for storage.
You give each bucket a name, like you and I have names on this forum.
This name must be unique (as on this forum) so we can always identify our buckets without any doubt.
There are rules about the names you can give variables, just as I am not allowed to use offensive words in my name on this forum.
One of the rules is that you cannot start a variable's name with a number, which is what you did by typing the line:
int 3;
There are different
types of bucket to hold different types of value.
The statement:
int thisisanumber;
creates a bucket called "thisisanumber" which will hold an integer, that is a whole number between -2147483648 and 2147483647. The type of bucket is called 'int'
Different types of bucket include char and float, so we can store letters and decimal values as well as integers.
(I would write
float thisisadecimal;
to create a bucket to hold a decimal called thisisadecimal)
The general format to
declare a variable is like this:
type identifier;
The rest of the program:
cout<<"Please enter a number:";
This line will print "Please enter a number:" on your screen.
Anything contained with double quotes like this is a string literal.
It will appear exactly as typed.
THIS IS A VERY IMPORTANT DIFFERENCE!
cin>>thisisanumber;
The program will now wait for you to enter a number on the screen and press return. When you press return (and assuming you have typed a number) the computer will put this number into the bucket called thisisanumber.
cout<<"You entered: "<<thisisanumber;
The computer will now print on your screen "You entered:" followed by the contents of the bucket called thisisanumber.
Some other stuff you can do with variables.
Also note that anything appearing between // and a newline will be ignored.
This is referred to as a comment and allows me to annotate the code.
You can copy and paste the code below into a text file and then compile it.
Code:
#include <iostream.h>
int main()
{
int thisisanumber;
thisisanumber = 99; // fill the bucket with the value 99
int anothernumber; // create another bucket, called anothernumber
anothernumber = 1; // fill another number with the value 1
int yetanothernumber; // create a third bucket ...
// fill yetanothernumber with the value contained in thisisanumber added to
// the value contained in anothernumber
yetanothernumber = thisisanumber + anothernumber;
// show the value contained in yetanothernumber on the screen
// note the difference between yetanothernumber appearing in
// quotes, which will show the word "yetanothernumber" and
// yetanothernumber appearing without quotes which allows us to
// access its contents
cout << "The value contained in yetanothernumber is : " << yetanothernumber;
return 0;
}
You might also want to know that you can't do this:
cin >> 5;
You need to have something that you can fill at the end of the statement.
You can fill a variable but you can't fill a number or a value, in the same way you can fill a bucket but you can't fill water.
Where you wrote;
cout<<"randomthing: "<<7;
however, you can output a value, just as you can output a variable.
On screen this will show 'randomthing: 7' on your screen.
Well, I hope all that helped a bit anyway.
If you can find it "The Complete Idiots Guide to c++" by Paul Snaith is a fairly good book to learn from.
Amazon reviews:
http://www.amazon.com/gp/product/cus...=ATVPDKIKX0DER