-
stupid program question
I'm reading a book on C++ and this is the first simple quiz question...
it says run this and guess what it does, now i know what it does but it wont compile.
Code:
#include <iostream>
int main();
{
int x = 5;
int y = 7;
cout "\n";
cout << x + y << " " << x * y;
cout "\n";
return 0;
}
on DEV C++ it says - 3 C:\Documents and Settings\Reece.LAPTOP\Desktop\test.cpp expected unqualified-id before '{' token
3 C:\Documents and Settings\Reece.LAPTOP\Desktop\test.cpp expected `,' or `;' before '{' token
and on Visual C++ Express Edition it says
Compiling...
tester.cpp
.\tester.cpp(3) : error C2447: '{' : missing function header (old-style formal list?)
Build log was saved at "file://c:\Documents and Settings\Reece.LAPTOP\My Documents\Visual Studio 2005\Projects\tester\tester\Debug\BuildLog.htm"
tester - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
can someone please just tell me whats wrong cuz its really winding me up because its soo simple.
-
Code:
#include <iostream>
int main(); // Remove this semicolon
{
int x = 5;
int y = 7;
std::cout << "\n";
std::cout << x + y << " " << x * y;
std::cout << "\n";
return 0;
}
-
For now, add this.
Code:
#include <iostream>
using namespace std;
int main()//;
{
int x = 5;
int y = 7;
cout << "\n";
cout << x + y << " " << x * y;
cout << "\n";
return 0;
}
And copy better.
-
Hmm, ok thanks. but why does the STD:: and deleting the ; work?
and using namespace std; doesnt work with it. get same error.
EDIT! using namespace std; worked with the << sorry, u didnt put the << till after :)
also, why the ; deleting?
-
Because int main(); is a function prototype, so all your code is ignored. Not that typing it is a problem: I see no reason why this wouldn't compile:
Code:
#include <iostream>
int main(void); // but it is a waste of kestrokes
int main(void) {
int x = 5, y = 7;
std::cout << "\n";
std::cout << x + y << " " << x * y;
std::cout << "\n";
return 0;
}
std is a namespace that all standard templates, objects, and functions are in. This needs to be explicitly declared, or you must say you are using the namespace std.
-
yeh it works as STD::
i am used to using int main(); as i normaly add namespace std; but i didnt think to this time.
sorry for posting a stupid thing like this.
Hugo.
-
another one. giving me the same error about missing function header for thr '{'
Code:
#include <iostream>
using namespace std;
// Function demonstration function
// prints out a usefull message
void DemonstrationFunction()
{
cout<<"In Demonstration Function\n";
}
// Function main - prints out a message, then
// calls DemonstrationFunction, then prints out
// a second message
int main();
{
cout<<"In Main\n";
DemonstrationFunction();
cout<<"Back in main\n";
system("PAUSE");
return 0;
}
really bugging me. sorry. thanks.
-
Code:
int main();
{
cout<<"In Main\n";
DemonstrationFunction();
cout<<"Back in main\n";
system("PAUSE");
return 0;
}
You were just told about this!
-
sorry just fixed it.
yeh but the last one worked WITH ; in it still. and they said it would work with ; as long as i added namespace
-
There is the excepton that you called system without including stdlib.h. That being said, calling system is the worst way to do a lot of things.
The program pauses for input only, so just use a standard input function to pause the program, like cin.get() or something.
-
it shouldn't - just stop doing that and you won't run into that
problem again - best solution
[update1]
they weren't saying that it would work with namespace std - that
was for the cout and stuff.
[/update1]
[update 2]
system is defined in iostream - since iostream was designed to
replace stdio.h and stuff.
[/update 2]
-
ok, its just ive always put ; after main im just so used to it.
btw, is there anyway to stop the program from exiting straight the way other than putting system("PAUSE"); at the end?
-
getchar(); ?
I don't know where that's defined, or whether it's in C or C++ or whether it matters. Or somewhere here.
-
yes - as already mentioned a cin.get() does the job, but
sometimes you'll need to clear the input buffer, otherwise it'll just
eat any junk in the buffer. Just do something like this:
Code:
int main (void)
{
int ch;
//body of program
while ((ch = cin.get()) != '\n' && ch != EOF);
cin.get ();
return 0;
}
or call cin.ignore() before cin.get()
[update]
getchar() works too - in C its in stdio.h but it happens to be in
iostream too - like a lot of things from stdio.h and stdlib.h
[/update]
-
k thanks alot mate.
hmm wots EOF?