Be a Supporter!

Problem with C++. :(

  • 480 Views
  • 11 Replies
New Topic Respond to this Topic
Zune
Zune
  • Member since: Jun. 25, 2004
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Problem with C++. :( 2005-12-26 19:07:16 Reply

I 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;
}

DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to Problem with C++. :( 2005-12-26 19:12:48 Reply

#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.


BBS Signature
Zune
Zune
  • Member since: Jun. 25, 2004
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to Problem with C++. :( 2005-12-26 19:24:47 Reply

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? '_'

DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to Problem with C++. :( 2005-12-26 19:26:06 Reply

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?


BBS Signature
thoughtpolice
thoughtpolice
  • Member since: Mar. 24, 2003
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to Problem with C++. :( 2005-12-26 19:38:01 Reply

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

Ravens-Grin
Ravens-Grin
  • Member since: Jun. 3, 2003
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Problem with C++. :( 2005-12-27 23:24:13 Reply

Another way of preventing it is running the program from the command prompt.

Greatfaith
Greatfaith
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to Problem with C++. :( 2005-12-30 09:56:13 Reply

I usually have to type the cin.get(); two times in a row, and then it works for me.

PONGpaddle
PONGpaddle
  • Member since: Sep. 23, 2003
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to Problem with C++. :( 2005-12-30 10:56:01 Reply

Note that <conio.h> isn't a C++ library, it's a C library. The proper library to use should be <cconio>.

0x41
0x41
  • Member since: Dec. 30, 2004
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to Problem with C++. :( 2005-12-30 11:33:52 Reply

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.

thoughtpolice
thoughtpolice
  • Member since: Mar. 24, 2003
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to Problem with C++. :( 2005-12-30 19:12:19 Reply

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

PONGpaddle
PONGpaddle
  • Member since: Sep. 23, 2003
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to Problem with C++. :( 2005-12-30 20:25:29 Reply

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.

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to Problem with C++. :( 2005-12-31 10:09:34 Reply

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 :)

BBS Signature