C++ 101

southpaw5253

Newbie
Joined
Nov 18, 2005
Messages
46
Reaction score
0
when i try to compile this SIMPLE program...i cant an unexpected end of file error.
what is wrong? i bet its something really dumb but im not sure. plz help me!



#include<iostream>
using namespace std;

int main()
{
int num1 = 0;
int num2 = 0;
int total = 0;

cout << "Enter first number:";
cin >> num1;
cout << "Enter second number:";
cin >> num2;
total = num1 + num2;
cout << total;
return 0;
}
 
i don't know c++ but it looks okay to me, though i believe a space needs to be between #include and <iostream>, and maybe a ; after it, though i'm not sure.
 
What compiles are you using?

Also try placing a space after the last }
 
If you're using any decent compiler, it would give you a syntax error if something was actually wrong with the syntax. I think your compiler is fubar.
 
im using the compiler that comes on Visual Studio 6.0

the error is:
C:\Documents and Settings\Test\Test1.cpp(5) : error C2447: missing function header (old-style formal list?)
Error executing cl.exe.
 
well ive got it to work.... i don't know how i dont see any difference between the code thats working now and the stuff ive posted... strange
anyway, thanks for all the help, ill post the working code:

#include<iostream>
using namespace std;

int main()
{
int num1 = 0;
int num2 = 0;
int total = 0;

cout << "Enter first number:";
cin >> num1;
cout << "Enter second number:";
cin >> num2;
total = num1 + num2;
cout << total << endl;
return 0;
}
 
southpaw5253 said:
when i try to compile this SIMPLE program...i cant an unexpected end of file error.
what is wrong? i bet its something really dumb but im not sure. plz help me!



#include<iostream>
using namespace std;

int main()
{
int num1 = 0;
int num2 = 0;
int total = 0;

cout << "Enter first number:";
cin >> num1;
cout << "Enter second number:";
cin >> num2;
total = num1 + num2;
cout << total;
return 0;
}
Just a little advise. The = 0 is not needed because all variables = 0 unless you add a value to it of course.
 
theSteven said:
Just a little advise. The = 0 is not needed because all variables = 0 unless you add a value to it of course.

plus the return 0 is not required, but a system("PAUSE") at the end helps, to check if the outcome is correct
 
southpaw5253 said:
thanks a ton i didn't know that
can declare variables just like

int roflcopter;
String lollerskate;

and they default to null
 
DEATH eVADER said:
plus the return 0 is not required, but a system("PAUSE") at the end helps, to check if the outcome is correct

Rather you should use cin.get() though.
 
WTF? Where do you guys get that they would default to 0?
Unless you're doing a debug build or working with global variables, that will NOT be the case.

This isn't java.
 
Err, you really shouldn't declare variables without initialising them. It's bad programming practice. C++ does not automatically initialise your variables with 0, unlike other languages. Therefore, you cannot assume anything about a declared variable's value unless you explicitly set it somewhere. For example, an integer you just declared could have 2308 as value, or 28, or 5502.

This sucks in situations like this:
Code:
int main( )
{
   //we declare a variable without initializing
   int foo;
   
   //some condition that for the purpose of this example will not be true
   if ( ennui.looksLike(frodo) == false )
      foo = 5; //the variable is only set to 5 if the condition is true, and it's not

   //here we assume foo defaults to 0 on declaration...
   if ( foo == 0 )
   {
        cout << "Ennui looks like Frodo!";

        /*
        but as it turns out, sometimes this is printed, and sometimes it isn't
        so you might go off and debug the looksLike function for, say, a failing 
        haircut-comparison algorithm, while it's actually your uninitialized 
        variable being a bitch
        */
   }
   //it goes without saying that it would be better to use a bool here, but that's why it's an example :P
   return 0;
}

To keep yourself from making mistakes with this it is usually best to initialize every variable you declare.

As with most guidelines, the more experienced you are with them, the more you can safely diverge from them.

edits: Bert outposted me
 
theSteven, DEATH eVADER .. where the f*** do you guys get your info from? it's all wrong.

You do need the = 0 when declaring integers. and there's no String class in C++.
and yes, you also need the return 0;
and no, you don't place a ; after an #include

sheesh!
 
It is very true info, also pointer to variable's don't need to be int* roflcopter, they can also be int+ roflcopter, int- roflcopter or int% roflcopter.
 
AFAIK when declaring variables in C++ you just reserve space in memory for it. You dont change the memory, so anything thats allready there will be what the variable is.

C++ can be complex especially if you are used to features of other languages.
 
HunterSeeker said:
AFAIK when declaring variables in C++ you just reserve space in memory for it. You dont change the memory, so anything thats allready there will be what the variable is.

C++ can be complex especially if you are used to features of other languages.
quite
 
just an aside.. you need to include <string> to use the string classes, and <stdlib.h> for the system("Pause") function, among other things.
 
Back
Top