PDA

View Full Version : Pausing a C++ Program



Synapse
30-03-10, 03:26 AM
Greetings everyone!

I'm currently trying to grasp the basics of C++. I've been using 'system("PAUSE")' to pause a program but according to a number of programmers, this is a rather bad habit. Is there a better, more efficient way to pause a program please? I'm currently using Visual Studio 2008.

Help is always much appreciated.

P1raten
30-03-10, 08:10 AM
Why would you need to pause a program?

Synapse
30-03-10, 08:29 AM
Let's take this program as an example:



// This programs converts feet to metres or vice versa

#include <iostream>
using namespace std;

int main ()
{
short userchoice;
double metres;
double feet;

cout << "To convert feet to metres, please press 1\n";
cout << "To convert metres to feet, please press 2\n";
cin >> userchoice;
cout << "\n";

if (userchoice == 1)
{
cout << "Please enter the number of feet: ";
cin >> feet;
metres = feet/3.28;
cout << feet << " feet = " << metres << " metres\n";
}

if (userchoice == 2)
{
cout << "Please enter the number of metres: ";
cin >> metres;
feet = metres*3.28;
cout << metres << " metres = " << feet << " feet\n";
}

cout << "\n";
system("PAUSE");
return 0;
}


In this case, a pause is needed after displaying the result or else the program would instantly close without allowing the user to see it. Should I stick to 'system("PAUSE")' for simple programs?

P1raten
01-04-10, 02:58 AM
Let's take this program as an example:



// This programs converts feet to metres or vice versa

#include <iostream>
using namespace std;

int main ()
{
short userchoice;
double metres;
double feet;

cout << "To convert feet to metres, please press 1\n";
cout << "To convert metres to feet, please press 2\n";
cin >> userchoice;
cout << "\n";

if (userchoice == 1)
{
cout << "Please enter the number of feet: ";
cin >> feet;
metres = feet/3.28;
cout << feet << " feet = " << metres << " metres\n";
}

if (userchoice == 2)
{
cout << "Please enter the number of metres: ";
cin >> metres;
feet = metres*3.28;
cout << metres << " metres = " << feet << " feet\n";
}

cout << "\n";
system("PAUSE");
return 0;
}


In this case, a pause is needed after displaying the result or else the program would instantly close without allowing the user to see it. Should I stick to 'system("PAUSE")' for simple programs?
I suggest that you download Visual Studio, since you don't need to pause the program if you use it. :)
To run the program simply press ctrl + F5 :)

Synapse
01-04-10, 06:56 AM
Thanks for the tip mate! :)

Diocia
09-04-10, 11:10 PM
you could create a loop that after the program is run, it asks if the user wants to run the program again. If so loop to the beginning, if not the program exits.