![]() |
Why do I have to enter two cin.get()?
I just installed Visual Studio 2005, and tried a simple program using C++. A console program. After my code was entered, I ran the program and it worked but the console window closed really fast. So I looked in one of my C++ books and the author said try writing cin.get() just before the return 0 statement. I tried that but it didn't work. A few sentences later he said in some environments you may have to enter cin.get() twice. I tried that and it worked.
Anyone know why that is? And are there any alternatives to entering cin.get() that would keep the console window open? |
There are a lot of posts regarding this issue in the forums.
It should be enough with one cin.get(), before the return 0 row. Since you are using Windows you can use the system("pause") function call instead of cin.get(). But this is specific to Windows. Please paste your code and we will see why you need two cin's. |
well as cin.get() gets the next character from the standard input, you are possibly adding an extra char to the input stream in your application? probably from a cin somewhere?
|
Oddly enough, you will find the answer in the OTHER thread where you asked the same question. Believe it or not, you can get a dozen different answers, if that's what you want, in a single thread. You don't need 12.
|
My program works when I enter either two cin.get() or cin.get and followed by cin.ignore. Here is my program. Any someone is going to have to explain in simple terms how do I use these code tags. I don't know why I have to use them. Why can't I just copy and paste my code? Here is my program:
:
#include "stdafx.h"And is there a way to make the console window close not just by pressing the Enter key, but the Spacebar as well? |
Here's why you need to use them: these pages are HTML. That means they discard extra whitespace. That means, further, that all the formatting and indentation in your code goes bye-bye. Code tags retain the formatting. Does that make sense?
Now, if you want to clear the stream BEFORE you try to get a character, would you put the clearing BEFORE the get, or AFTER the get? Personally, I would put it BEFORE the get, but that's just me. Does that make sense? You are new to the forum. Most people would consider reading the rules/FAQ the polite thing to do, and heed advice like using code tags. Is any of this making sense????? :confused: :rolleyes: :beard: |
I'm not following you. Clear the stream before you get a character? How do I put that in my code? I did find another way however. I can code cin.ignore(2) and that will also keep the console window open.
I just don't understand the connection between inputting two integers and keeping the console window open. The only thing I can surmise is when I input the first integer that is in the input stream or computers memory. When I input the second integer that is also in the input stream or computers memory. But I don't see a connection between that and having to type two cin.get() to keep the console window open in this particular program. If you think you can answer my questions without being a sarcastic jerk then go for it. Your attitude is deplorable. I've been a musician for 26 years and I teach guitar and piano. The kids that come to me for lessons don't know a thing about playing an instrument. But do you think I sit there and tell them things and then sarcastically say, "does that make sense to you?" Certainly not. Programming is complex stuff, and I'm just a beginner. I'm going to have lots of questions and I don't think I'm asking too much to have my questions answered respectfully and courteously. If you can't do that, then don't answer. |
Sorry you don't care for my attitude. When I come to a programming forum, I expect a large percentage of the members to be able to read and comprehend. When I visit an institution for the mentally challenged, I have a different outlook. If you would READ, and THINK, instead of asking the same question multiple times without intervening READING and THINKING, and avoid posting the same question multiple times in order to avoid the possibility of having to THINK, you might eventually learn where high 'C' is.
Don't bother to tell me to not answer; I'll just tell you to not ask. If you can't handle a critique over your poor procedures, go see mama for a sugar tit. There are none here. |
Ah but DaWei! I am trying to think. This is tough stuff when you consider computers are probably the most complicated thing man has ever invented. And I will never be a programmer, I realize that. But I still enjoy doing it on my own. Your response was very predictable. By the way DaWei, I've done some looking on this cin.get() and many answers I find are simply, "keeps the console window from closing..." I need a little more explanation than that, particularly as it relates to my program.
|
Quote:
If you think about it, its actually kind of a hack, this is not what cin.get() was intended to be used for, but we need something to happen to stall the program from continuing to the next line of code(which ultimately terminates your program), therefore, by putting in the cin.get(), the program halts on that line until it gets what its looking for, which is a character on the input stream. apparently, since you have to put two of these in there, there is already a character on your input stream, therefore when you put the first cin.get() it reads in the one that's already there and then continues to the next line of code which ultimately terminates your program. now when you put a second one, it reads in that first character already on the stream, then it looks for a second one but there is no more for it to read so it awaits the user to input it before it continues to the next line. What Dawei and Seif have been saying is that in order to get it so the input stream is clear and there is nothing to read when you do your first cin.get() you need to clear out the stream. It has been a while since I've worked in C++ but I believe it was cin.flush() which will do this, I may be wrong but someone around here will correct me if I am. what Dawei was trying to tell you with that whole "before" and "after" talk was that you should move your cin.ignore() above the line containing cin.get() rather than after it like you have in your code above. If you think about it, you're getting that one leftover character off the input stream and then you are attempting to ignore it, which doesn't really make sense because you already dealt with it so now its gone. if you put the ignore before the get then you ignore this leftover character and then you attempt to read, now that it is gone the program will await user input since there is nothing left on the input stream for it to read. Hence, your program will wait for user input before continuing to the next line, ultimately, pausing your program until you give it what it wants(character input). questions? |
| All times are GMT -5. The time now is 12:42 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC