Jump to content
  • Advertisement
Sign in to follow this  
rappen

variables

This topic is 4032 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
Share on other sites
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;

Share this post


Link to post
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Ok thank you guys!

Problem solved

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!