// 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... :?:

Dani AI

Generated

The original question was about keeping the console open after the result appears, but the posted code has several other bugs that make the program fragile. correctly warned against nonstandard headers; suggested a Windows-only system call. Below is a short, practical fix that (1) corrects the program logic, (2) uses only standard headers, and (3) waits for the user to press Enter in a portable way.

Key fixes to apply before adding a pause:

  • Use int main() (not float main) and return 0 on success.
  • Avoid naming a variable char (keyword) and avoid ambiguous names; prefer hours/minutes or timeHours.
  • Convert hours+minutes to fractional hours and compute distance as distance = speed * timeHours (your code used division).
  • Do simple input validation so the program fails gracefully on bad input.

Example corrected program (portable, uses Enter to pause):

#include <iostream>
#include <string>

int main()
{
  double speed;
  int hours = 0, minutes = 0;

  std::cout << "Compute distance traveled given speed and time\n";
  std::cout << "Enter speed in MPH: ";
  if (!(std::cin >> speed)) return 1;

  std::cout << "Enter time as two integers (hours minutes): ";
  if (!(std::cin >> hours >> minutes)) return 1;

  double timeHours = hours + minutes / 60.0;
  double distance = speed * timeHours;

  std::cout << "It takes " << timeHours << " hours to travel "
            << distance << " miles at " << speed << " MPH\n";

  std::string dummy;
  std::getline(std::cin, dummy);            // consume leftover newline
  std::cout << "Press Enter to exit...";
  std::getline(std::cin, dummy);            // wait for Enter

  return 0;
}

Notes and small tips:

  • system("pause") and conio.h/getch will work only on some compilers/OSes and are discouraged for portable code.
  • If you run the program from a terminal/command prompt instead of double-clicking the executable, you usually do not need any pause—the output stays visible.

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.