Web Hosting Talk







View Full Version : Slight C++ Issue


MarkioE
10-04-2008, 11:41 AM
At the moment I'm just teaching myself the basics of C++ and it's going quite badly :)

I have the basic code:

#include <iostream>
using namespace std;

int main ()
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n";
return 0;
}


And it compiles fine, but when I try to run it the program runs fine but closes straight away. Any ideas why?

I'm using Visual C++ Studio 2008.

Xeentech
10-04-2008, 12:33 PM
That's normal for that type of application. Because it has nothing left to do it will finish executing then exit and return control to what ever called it.

If you were to call it from your command prompt the output would stay on screen. Or you could make the exec wait for say a couple seconds with the sleep() call, it's in windows.h.. unsure how you call that from C++ these days, probably just sleep() in the default namespace though TBH.

MarkioE
10-04-2008, 01:36 PM
Hmm.. I still don't really understand how to stop it from closing :(

RSkeens
10-04-2008, 01:57 PM
Xeentech is right, the return 0; indicates that the program should finish executing.

Steve_Arm
10-04-2008, 02:00 PM
In the old days you told the program to wait for some input and then exit.
In C it's something like getc().

ThatScriptGuy
10-04-2008, 02:02 PM
Instead of opening your compiled program from windows explorer (IE double clicking), open up command prompt, cd to the directory containing your executable, and execute it from the command line. You will then be able to see your output.

etogre
10-04-2008, 03:30 PM
Ah, a simple problem.

Try adding the following line:

system("PAUSE");

Right before the return statement.


Alternatively you could create a menu, throw the whole program in a loop that only terminates when the user inserts 'Q'.

Codebird
10-04-2008, 04:51 PM
etogre's second solution is the best


#include <iostream>
using namespace std;

int main ()
{
int i;
char toDo;
cout << "What do you want to do? Q for quit or P for proceed \n";
cin >> toDo;
while(toDo=='P'){
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n\n";
cout << "What do you want to do? Q for quit or P for proceed \n";
cin >> toDo;
}
return 0;
}

LordChaos
10-08-2008, 07:22 AM
hey Sparky i get the same issue too

but if u want something fast and easy to not make the program exit. just make the user always input anything first to quit like below

int quit=0;

cout << "Press anything to quit"<< endl;
cin >> quit;

put that at the end of your main function

what will happen is your program will run and wont quit unless a user input is given at the end.

hope this helps


#include <iostream>
using namespace std;

int main ()
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n";
int quit=0;
cout << "Press anything to quit"<< endl;
cin >> quit;
return 0;
}

Bandit-X
10-08-2008, 09:52 AM
that's weird sparky123, it is supposed to pause at the end of that console app in Visual Studio.

Here is my output:
Please enter an integer value: 2
The value you entered is 2 and its double is 4.
Press any key to continue . . .

And... You do start without debugging. right? CTRL F5