# variables

## Recommended Posts

rappen    112
Hey, i've got a little problem. i've a variable code but it wouldn't compile and run #include <iostream.h> int main() { int 3,2; cout <<"3:"; cin >> 3; cout <<"\n2:"; cin >> 2; cout << "\n" << 3+2; return 0; } So...what's the problem? - Rappen.

##### Share on other sites
Death100    122
You can't make a variable that starts with a number. For example:

int 3; //won't work

however

int Pie; //will work

If you want to set those variables to a value:

int pie = 20, cheese = 30;

##### Share on other sites
1. Use [code] and [/code] for short snippets of code, use [source] and [/source] for longer pieces.

2. In future, please post the first error message you get from the compiler in the case of a compile-time error or, in the event of a run-time error, the expected behaviour of the program in addition to what actually happens. We're not psychic -- well, mostly anyway. ;-)

3. #include<iostream>, not #include <iostream.h> (how many tutorials are doing this?? Almost all the beginners seem to have this at some point or another)

4. You need using namespace std; after the #include directive, or you need to prefix cin and cout with std::. As a beginner, you're better off with the first option.

4. You can't begin a variable name with a number.

Try:

#include <iostream>using namespace std;int main(){    int x, y;     cout << "3:";     cin >> x; // Hope the user inputs 3!    cout <<"\n2:";     cin >> y; // Hope the user inputs 2!    cout << "\n" << x + y; // Output the sum of whatever was entered;    return 0;}

EDIT: A little bit of sloppiness.

##### Share on other sites
jpetrie    13103
In the future, post your error messages.

You have a number of things wrong with that code. The first that would actually cause an error is that you cannot name variables "3" and "2." Variable names must start with a letter or an underscore; they cannot start with (or be) digits.

##### Share on other sites
rappen    112
Ok thank you guys!

Problem solved

PS: So for now on i have to put always <iostream> and not <iostream.h>...Why not?

##### Share on other sites
omgh4x0rz    122
the iostream.h is outdated and isn't in ISO c++...I think

##### Share on other sites
Quote:
 Original post by rappenPS: So for now on i have to put always and not ...Why not?

iostream.h was what was used prior to the C++ standard (in 1998, I think). Now, the file is called iostream. I'm not sure what the rationale was, but it's the Right Thing to do because iostream.h is deprecated and is supplied to stop old (pre-standard) code breaking.

##### Share on other sites
rappen    112
Ok, thank you guys.

##### Share on other sites
Aardvajk    13207
Quote:
 Original post by TheUnbelieverNow, the file is called iostream. I'm not sure what the rationale was, but it's the Right Thing to do.

I think that <iostream> guarantees to put everything in the std namespace whereas <iostream.h> may do or may not, depending on the cycles of the moon and so on.

##### Share on other sites
nobodynews    3126
I don't know what resource you're using to learn, but one of the first things that should be (and often is) taught about variables is that they can only begin with a letter. Coupled with the fact that iostream.h a)has Never been standard (existed before there was a standard) and b)might not exist with all compilers (and probably shouldn't as it isn't standard) and this leads to my suspicion you are using a crappy learning resource. Try reading:

C++: A Dialog or maybe C++ Language Tutorial from CPlusPlus.com