Execution pausing

How do I pause my program before it ends it? Here is my code so you can see what I tried:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    double f;
    double c;
    char hold;
    cout << "Enter the degrees in Fahrenheit: ";
    cin >> f;
    cout << endl;
    c = (f - 32) / 1.8;
    cout << "The degrees in Celcius is: " << c;
    cin.get(hold);
    return 0;
}

that works when I compile and run, but when I click the .exe, it doesn't pause, help?
Last edited on
1
2
//cin.get(hold); // get() does not skip white spaces
cin >> hold ;
That makes it so i have to enter another number before it closes :/
closed account (zb0S216C)
If you #include <windows.h>:
std::cin.ignore((std::numeric_limits<std::streamsize>::max)(), '\n');

If you don't #include <windows.h>:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

Wazzak
Last edited on
It's still closing right after I press enter for the first time (after entering degrees in Fahrenheit), not in the compiler, but in the .exe that is made in the folder where i save the project.
You can use getch() function to pause program. It is in conio.h header file.
but then how do i resume it?
Use system ("pause")
(let the flame and hate war begin)
Resuming will just be a matter of pressing 'enter' afterwards.
Or you can just loop the whole program, and at the end ask if the user wants to do another calculation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    double f;
    double c;
    char cont = 1;
  
    while (cont){
        cout << "Enter the degrees in Fahrenheit: ";
        cin >> f;
        cout << endl;
        c = (f - 32) / 1.8;
        cout << "The degrees in Celcius is: " << c;
        cout << endl << "quit [0]   continue [1]" << endl;
        cin >> cont;
    }
   return 0;
}

Last edited on
i kind of like the idea of looping it, but i dont know, thanks everyone! :D i'll try these out when i get home
Topic archived. No new replies allowed.