issue with my 1st C++ program..

OK, SO THIS IS WHAT I HAVE FOR MY FIRST PROGRAM (IT IS SUPPOSED TO CONVERT CELSIUS TEMP TO FAHRENHEIT TEMP):

#include <iostream>
using namespace std;

int main() {
double ctemp, ftemp;

cout << "Input a Celsius temperature, and press ENTER: ";
cin >> ctemp;
cout << "The equivalent Fahrenheit temperature is: " << (ctemp * 1.8) + 32;

return 0;
}


BUT THE PROBLEM IS THAT IT WILL NOT STAY OPEN AFTER I PUT THE CELSIUS TEMP AND PRESS 'ENTER.' WHAT AM I DOING WRONG HERE??
add cin.ignore(); before return 0;
this is an article about your problem:
http://www.cplusplus.com/forum/articles/7312/
you know what the sad thing is.. everyone is saying that the "system(PAUSE);" way of solving this problem is a bad habit, and i listen to those who know more than i do, but after trying all that other stuff, the "system(PAUSE);" way was the only one that has worked for me.. i don't want to embark on bad habits here, so can anyone show me exactly how to insert maybe the "cin.ignore();" syntax here...?
Insert the line std::cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' ); before return 0; and remember to #include <limits>
Add this code to the end of the program.

1
2
3
cin.clear();
cin.sync();
cin.ignore( numeric_limits <streamsize> ::max(), '\n' );


and include this at the top of the file
#include <limits>

Edit: Oops a second too late
Last edited on
yeah that worked Bazzy.. and thanks for the help eker676.. now all i have to do is wait to read about what all that stuff i just inserted into the code means... this book i am reading sucks b/c it doesn't tell you how to keep the program running... thanks
everyone is gonna jump down my throat for this and it may be a long shot but try this

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main() {
double ctemp, ftemp;

cout << "Input a Celsius temperature, and press ENTER: ";
cin >> ctemp;
cout << "The equivalent Fahrenheit temperature is: " << (ctemp * 1.8) + 32<< endl;

return 0;
}
^ You're absolutely right. Anyone could have copied the posted code.

@haco

It might assume that the user is running the program from the command line.
Last edited on
Haha I can't tell if I sense sarcasm or not. All I was saying is if he's new, he might not notice that it printed out his results he just didn't notice them because they were on the same line as a command prompt
system("pause") and cin.ignore()
are two totally different things. If you are writing cross-platform code, maybe you shouldn't use system("pause"); , but I don't think you are a professional programmer. As long as you are using windows as a platform, there is NOTHING wrong with using it.
You will hear 'experts' disagreeing, but don't buy it for a second. When you are at their level, you can learn other options. They don't seem to understand that some people don't know as much as they do.
I'm in my 15th week of c++ at Penn State, and we are required to use "pause" at the end of each program so that the graders can see what our code does. When these people tell you that in your very first program, you shouldn't use anything that violates 'best practices', they have clearly forgotten how it was to be a beginner.
Use it liberally, and as you learn other methods, you can gradually change.
As far as your code, all you have to do is add a system("pause") at the end, and it works fine.
Unless your prof tells you not to use it(won't happen, everyone on this site used it when they were beginners, but they have forgotten that), put it in, it works.

Am I the only one that noticed it worked fine as was?
I can't believe you guys have him doing crazyness like:
1
2
3
cin.clear();
cin.sync();
cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

did you bother to explain exactly what that does and why?
Did you learn that in your first program? Didn't think so.

Quit making it more complicated, and help the new people out.
All he has to do is a simple system("pause");
This isn't cs 460, it's 101, remember that.
Last edited on
hey yeah thanks for the moral support grcunning. good stuff. by the way ellimist14, did you say it works fine as it is?? does that have anything to do with your IDE by any chance? it did not work for me.. just shut down.. anyway, the system("PAUSE"); works as does the std::cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' ); EXCEPT with this second strategy i was clever enough to omit the "std" b/c i already specified the object with the "using" statement, "using namespace std" this is all great info though... i am learning here
Yeah it's IDE. I use pico and I like it. I feel unix is alot better. But yeah I think that was why
ah man.. i just figured out that the ONLY thing that was actually working for me was the system("PAUSE"); that whole cin.ignore( numeric_limits <streamsize> ::max(), '\n' ); was not the thing that was causing the program to stay open.. i thought it was helping but that was only b/c i had accidentally kept the system("PAUSE") in the code... i guess i'll figure it out later... i dont know anymore
I'm sure there are many different ways to accomplish that task that I haven't learned yet. The main point is that you are trying to learn a language...to divert your attention from that to trying to figure out how to keep a window open is counterproductive. Ask your prof, I bet he says the same thing. Concentrate on making your code work first, then about how to make it cross platform.
We have a sticky post about this. (console closing down) that discusses it ad nauseum if you are interested.
Don't let this confuse you, just concentrate on making your code do what it is designed to do.
Topic archived. No new replies allowed.