pulkit Posted October 28, 2005 Share Posted October 28, 2005 Hi, I need help in C++when I try to compile anything in Dev C++ I get this error saying that `cout' undeclared (first use this function) (Each undeclared identifier is reported only once for each function it appears in.) `endl' undeclared (first use this function) I dont know why is this happeningPls help Link to comment Share on other sites More sharing options...
Zxian Posted October 28, 2005 Share Posted October 28, 2005 Do you have#include <iostream.h>using namespace std;at the top of your file? Link to comment Share on other sites More sharing options...
pulkit Posted October 29, 2005 Author Share Posted October 29, 2005 Thanks it helpedBut another problemOn compiling i get this error:`main' must return `int'So i compiled this code:#include <iostream>using namespace std;main(int) //the main procedure (also the first executed){ cout << "Line"; cout << " One" << endl; cout << "Line Two" << endl; cout << "Line Three" << endl << "Line Four" << endl;}Instead of this:#include <iostream>using namespace std;void main() //the main procedure (also the first executed){ cout << "Line"; cout << " One" << endl; cout << "Line Two" << endl; cout << "Line Three" << endl << "Line Four" << endl;}Then it got compiledBut on executing it a black window appears very fast and disappearson executing the same in cmd.exe it gives the output thereWhy is it dependent on cmd.exe and how to make it independent of it?Pls reply Link to comment Share on other sites More sharing options...
Zxian Posted October 31, 2005 Share Posted October 31, 2005 Your program isn't dependant on cmd.exe. It's doing exactly what it should. It's displaying various text and then exiting. The fact that it shows up as a command line comes from the fact that it's compiled as a text-based program (the most simple kind).If you want it to stop before exiting, you'll want to change your code to the following:#include <iostream>#include <stdio.h>using namespace std;int main(void) //the main procedure (also the first executed){ cout << "Line"; cout << " One" << endl; cout << "Line Two" << endl; cout << "Line Three" << endl << "Line Four" << endl; SYSTEM("PAUSE"); return 0;}I'll need to double check the code tomorrow, but I think that stdio.h is the header that you need. The SYSTEM("PAUSE") call basically enters the "PAUSE" command into a command line window. Try it yourself.The return 0; tells the parent of the program (most likely Windows) that the program exited just fine. Some compilers will complain if main doesn't return an int, others won't. I find it good habit to state that the function doesn't have any arguments (i.e. main(void) ) as opposed to leaving empty brackets. When I'm writing code, I'll sometimes write the name of a function, and leave the arguments empty, because I don't know the arguments I need yet. If I find out that I don't need any, I fill in void.If you want to move away from the command line window, you'll want to look into using Visual C++ and learning MFC. But my suggestion would be to stay away from that until you're much more familiar with programming and Object Oriented Programming. Link to comment Share on other sites More sharing options...
phkninja Posted November 1, 2005 Share Posted November 1, 2005 (edited) if you want to make the program cross-platform add in#include <iostream>#include <stdio.h>using namespace std;int main(void) //the main procedure (also the first executed){ cout << "Line"; cout << " One" << endl; cout << "Line Two" << endl; cout << "Line Three" << endl << "Line Four" << endl; wait(); return 0;}void wait(void){ while(!getchar());}Also if you wamt to program gui programs (that are also cross-platform) look at http://www.wxwindows.org. It is a library of bog standard C++ code that creates windows, X11 etc programs without much code change (or if written correctly with #defines, no change at all).That will eliminate the problem of having to learn MFC that is only M$ specific. Edited November 2, 2005 by phkninja Link to comment Share on other sites More sharing options...
Zxian Posted November 2, 2005 Share Posted November 2, 2005 Good call phkninja. Link to comment Share on other sites More sharing options...
SenthilKumarM Posted November 9, 2005 Share Posted November 9, 2005 Hi dudes,I think instead of writing a function seperately for waiting (just to see the output in output screen), we can use the following cool stuff.#include <iostream>#include <stdio.h>#include <conio.h>using namespace std;void main() { <program logic code>getch(); //The output screen will be displayed till user press any key.}Dudes, Have a think in this way also.With Smiles,Senthil Kumar M. Link to comment Share on other sites More sharing options...
phkninja Posted November 9, 2005 Share Posted November 9, 2005 yes you could use #include <iostream>#include <stdio.h>#include <conio.h>using namespace std;void main(){<program logic code>getch(); //The output screen will be displayed till user press any key.}BUT that wont compile on every compiler. getch() is not an ANSI/ISO standard function, getchar() is. Link to comment Share on other sites More sharing options...
Wraith Posted November 10, 2005 Share Posted November 10, 2005 Don't use stdio.h - it's deprecated in C++, and if you must use it, #include <cstdio>, which places all the functions within stdio.h inside the std:: namespace. Link to comment Share on other sites More sharing options...
phkninja Posted November 15, 2005 Share Posted November 15, 2005 Wraith - Don't use stdio.h - it's deprecated in C++, and if you must use it, #include <cstdio>, which places all the functions within stdio.h inside the std:: namespace.It aint supposed to be C++, its C and all C++ compilers compile c because of the constraints of the language.My point was not that getchar() is the only way to create a wait statement, if i was being really pedantic i would write the code in asm as it is faster than C ( and C is slightly faster than C++ as well as being better at handling low level directory access etc. C has its advantages over C++, but C++ also has its advantages over C ) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now