variables

Started by
8 comments, last by nobodynews 16 years, 11 months ago
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.
Advertisement
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;
http://furboxes.com/forum
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.
[TheUnbeliever]
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.
Ok thank you guys!

Problem solved

PS: So for now on i have to put always <iostream> and not <iostream.h>...Why not?
the iostream.h is outdated and isn't in ISO c++...I think
Quote:Original post by rappen
PS: So for now on i have to put always <iostream> and not <iostream.h>...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.
[TheUnbeliever]
Ok, thank you guys.
Quote:Original post by TheUnbeliever
Now, 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.
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

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement