Jun 12, 2012 at 2:07pm UTC
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 Jun 12, 2012 at 2:13pm UTC
Jun 12, 2012 at 2:14pm UTC
That makes it so i have to enter another number before it closes :/
Jun 12, 2012 at 3:13pm UTC
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 Jun 12, 2012 at 3:14pm UTC
Jun 13, 2012 at 12:17pm UTC
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.
Jun 13, 2012 at 3:21pm UTC
You can use getch() function to pause program. It is in conio.h header file.
Jun 13, 2012 at 7:59pm UTC
but then how do i resume it?
Jun 13, 2012 at 8:03pm UTC
Use system ("pause" )
(let the flame and hate war begin)
Jun 13, 2012 at 8:03pm UTC
Resuming will just be a matter of pressing 'enter' afterwards.
Jun 13, 2012 at 8:06pm UTC
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 Jun 13, 2012 at 8:07pm UTC
Jun 13, 2012 at 8:11pm UTC
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