Jump to content

C++ Help


Recommended Posts

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 happening

Pls help

Link to comment
Share on other sites


Thanks it helped

But another problem

On 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 compiled

But on executing it a black window appears very fast and disappears

on executing the same in cmd.exe it gives the output there

Why is it dependent on cmd.exe and how to make it independent of it?

Pls reply

Link to comment
Share on other sites

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

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 by phkninja
Link to comment
Share on other sites

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...