// Program 2.25: Declares distance of an object
// given speed in MPH and time in hours and minutes
// Author: Eric Martin
// Date: 10/1/2004
#include <string>
#include <iostream>
#include <getch>
using namespace std;
float main ()
{
// Declare speed in MPH
// Declare time in hours and minutes
// Formulate

float speed;
float time;
float distance;
int char;

cout << "Compute distance traveled given speed and time" <<
endl;
cout << "Enter speed in MPH: ";
cin >> speed;
cout << "Enter time in hours and minutes: ";
cin>> time;

distance = speed / time;

cout << "It takes " << time << " minutes to travel "
<< distance << " miles at "
<< speed << " miles per hour" << endl;
int char;
return 0;
}
What do I need to include in this program in order to keep it from closing after getting the result? Someone suggested getch, but the compiler says the file does not exist... :?:

Recommended Answers

All 3 Replies

>#include <getch>
If your compiler supports getch, it will most likely be in conio.h. But because getch is not a standard function, cin.get() is recommended instead:

#include <iostream>

using namespace std;
// Any other headers you need

int main()
{
  // Your program here

  cin.get();
}

This will work except in cases where cin>> leaves a newline in the stream. This occurs more often than you might think, so it would be a good idea to flush the stream first and then call get:

#include <iostream>
#include <limits>

using namespace std;
// Any other headers you need

int main()
{
  // Your program here

  cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
  cin.get();
}

There are other ways to flush the input stream, most of them are nonstandard. One nifty standard way is to use rdbuf:

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

Though whether that's better than numeric_limits is debatable. And there's always the brute force loop:

char c;

while ( cin.get ( c ) && c != '\n' )
  ;

to pause a program just put the header conio.h
and when u want to pause write..
system("pause");
and if u want to clear the screen write
system("cls");
hope i've answered ur question

>to pause a program just put the header conio.h
conio.h is not a standard header, and on top of that it isn't the correct header to do what you're suggesting.

>system("pause");
system is declared in stdlib.h. But it's a bad idea in general to use system for things like this because it's not portable (the argument will be different on different systems), it's unsafe ("pause" could be a malicious program), and it's slow because it calls the system command interpreter. The same goes for "cls". Your suggestion if implemented correctly will only work on Windows and DOS machines. You should strive for portability wherever possible, and choose the best nonportable option when you can. system is not the best nonportable option.

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.