C++ not working properly

add this:

cin.ignore(cin.rdbuf()->in_avail() + 1);

before your return statement. then you hit enter.
 
or you can use system ("pause"); with the cstdlib.

Code:
#include <iostream>
#include <cstdlib>

using namespace std;

int main ()
{
cout << "Hello world" << endl;
/* If you were to use stdio it'd be: */
/* printf ("Hello World\n"); */
system ("pause");
return 0;
}

It's not good to use a function such as cin or cin.get to "pause" the program. Think if you had multiple source files that you included, pausing like that in one of your other sources could cause our program to hang. Another reason is people could write exploits and use buffer overflow attacks on your computer.

Another solution would be to open up your terminal, by typing "command" in start, run. Then finding the location of your executable. Then typing in the location for example it could be in C:\DOCUME~1\USER\DESKTOP\program.exe This way your program won't just flicker by anyway and you don't have to use bad programming practices.
 
Um...are you sure you're clicking "start without debugging?" If you select "start debugging" in your IDE (assuming you are using Microsoft Visual C++) the program will test run and will quit automatically after all the commands have been executed. "Start Without Debugging" displays "Press any key..." in the console after the program is done executing. At least that's the problem I had when I first taught myself C++.
 
Back
Top Bottom