The Enchanted Cave 2
Delve into a strange cave with a seemingly endless supply of treasure, strategically choos
4.34 / 5.00 31,296 ViewsGhostbusters B.I.P.
COMPLETE edition of the interactive "choose next panel" comic
4.07 / 5.00 10,082 ViewsI just got a C++ book and I'm still in Chapter 1, hehe....
Anyway, all of the projects in it use the return 0; statement at the end, and in my compiler (Bloodshed Dev) the program closes right after I run it. So instead I use the cin.get(); function which seems to work...until now.
Now I'm working on a project where whenever the user inputs a celcius tempature and press enter, the program gives them the faranheit tempature. But now after I press enter, it just closes! Can someone please help me to make this work? The code is below. Thank you so much.
#include <iostream>
using namespace std;
int main() {
double ctemp, ftemp;
cout << "Input a Celcius tempature and press ENTER: ";
cin >> ctemp;
ftemp = (ctemp * 1.8) + 32;
cout << "Farenheit tempature is: " << ftemp;
return 0;
}
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
double ctemp, ftemp;
cout << "Input a Celcius tempature and press ENTER: ";
cin >> ctemp;
ftemp = (ctemp * 1.8) + 32;
cout << "Farenheit tempature is: " << ftemp;
getch();
return 0;
}
I learned to use getch() at the end. It stops the program until any key is hit.
You need conio.h to use it.
At 12/26/05 07:12 PM, GamesCool wrote: You need conio.h to use it.
Thanks a lot for that, it worked. But I don't understand why my book didn't supply that information. Does that mean the book sucks? '_'
Your welcome.
It might be an older method. I learned it in C++ class so thats the only method of stopping the programming I've ever used.
Does your book suck, well, what book is it?
The thing is this: when that program hits return 0; it's told to end, and that is normally done by simply closing your application window. Putting a cin.get(); before solves this dilemma, however, if the application still closes, it's because the Standard Input Buffer (aka STDIN) is not clear, and needs to be flushed with a call to fflush();
You don't need to use getch(); for two reasons as well:
1. It's not part of the C++ standards. You can use this macro to end your program (put it before your include statements):
#define WAIT_4_END fflush(stdin); cin.get();
The only technicality with this is that you will always have to use use namespace std; or it won't work.
2. Getch(); isn't even standard in the C world. Depending on your compiler, it may or may not support it.
omg.
Playstation Network tag: muffin-noodle
the empty set
Another way of preventing it is running the program from the command prompt.
I usually have to type the cin.get(); two times in a row, and then it works for me.
Note that <conio.h> isn't a C++ library, it's a C library. The proper library to use should be <cconio>.
At 12/30/05 10:56 AM, TheDrunkMonkey wrote: Note that <conio.h> isn't a C++ library, it's a C library. The proper library to use should be <cconio>.
You are wrong. "cconio" is not a standard library. It's not included in the VC++ package and probably not in Dev-C++. Google turns up little results/information as well.
At 12/30/05 09:56 AM, Greatfaith wrote: I usually have to type the cin.get(); two times in a row, and then it works for me.
That is because your STDIN buffer is not clear. Do this instead:
fflush(stdin);
cin.get();
omg.
Playstation Network tag: muffin-noodle
the empty set
At 12/30/05 11:33 AM, 0x41 wrote: You are wrong. "cconio" is not a standard library. It's not included in the VC++ package and probably not in Dev-C++. Google turns up little results/information as well.
I found that VC++ doesn't follow what I've known from GCC and MinGW. It seems borrows thing from C.
If I'm just messing about writing something as a test or whatever, I sometimes stick something like this in at the end:
blah: goto blah;
It will keep the program open so you can see the output.
Sup, bitches :)