I've just started using C++ for my engineering course. I downloaded the Dev-C++ compiler and I am having trouble with the cout command. Everytime the compiler outputs a value/ sentence etc, the command prompt shuts down before anything is displayed.
I am running the program on XP Pro

Does anyone have suggestions on how to fix this?

Regards Kevin

>the command prompt shuts down before anything is displayed
Actually, everything is displayed just as you intended, but the window closes too fast for you to see that. This is a common problem that can be fixed simply by asking for input before the program terminates:

#include <iostream>
#include <limits>

using namespace std;

int main()
{
  // Your code here

  // Clean up the stream and ask for input
  cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
  cin.get();
}

On a side note, you'll see a bunch of different ways to clean up the standard input stream, most of which are wrong in one way or another. So be wary of any suggestions except the above.

#include <iostream>
#include <limits>

using namespace std;

int main()
{
  // Your code here

  
  system("pause");
}
system("pause");

Don't step backwards from correct and portable code to something that isn't. (And you forgot to #include the header that declares system() as well.)

Don't step backwards from correct and portable code to something that isn't. (And you forgot to #include the header that declares system() as well.)

Hi dave...i do agree tht its pretty backward step.....
how about using getch() using #include<conio.h>

and i am using DEV C++ 4.9.9.2...it doesn't require any header for system("pause")..it works with <iostream>

how about using getch() using #include<conio.h>

That's a nonstandard function from a nonstandard header.

and i am using DEV C++ 4.9.9.2...it doesn't require any header for system("pause")..it works with <iostream>

system is declared in <cstdlib>.

>how about using getch() using #include<conio.h>
That's even worse. Hey, I have an idea! How about this?

#include <iostream>
#include <limits>

using namespace std;

int main()
{
  // Your code here

  // Clean up the stream and ask for input
  cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
  cin.get();
}

*Whacks sunnypalsingh with a clue bat*

system is declared in <cstdlib>.

Hi dave i am not denying that...i am just saying that my program doesn't give any error if i don't include cstdlib(in devc++)...works fine with only <iostream>

>works fine with only <iostream>
Some compilers include unnecessary headers in others. A common one is <string> and <cstdlib> included inside of <iostream>. Don't rely on an implementation's quirks. Of course, you should do what I say and not what I do, because even the code I posted in this thread has the same problem. std::streamsize is only officially declared in <ios>. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.