Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 18th, 2007, 4:29 AM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
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?
357mag is offline   Reply With Quote
Old Apr 18th, 2007, 4:52 AM   #2
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 296
Rep Power: 4 Klarre is on a distinguished road
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.
__________________
http://www.klarre.se
Klarre is offline   Reply With Quote
Old Apr 18th, 2007, 10:37 AM   #3
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 214
Rep Power: 3 Seif is on a distinguished road
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?
Seif is offline   Reply With Quote
Old Apr 18th, 2007, 12:28 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 18th, 2007, 4:32 PM   #5
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
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"
#include "iostream"

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	int integer1, integer2, sum;

	cout << "Enter first integer\n";
	cin >> integer1;
	cout << "Enter second integer\n";
	cin >> integer2;
	sum = integer1 + integer2;
	cout << "The sum is " << sum << endl;

	cin.get();
             cin.ignore();

	return 0;
}


And is there a way to make the console window close not just by pressing the Enter key, but the Spacebar as well?

Last edited by Mjordan2nd; Apr 20th, 2007 at 10:01 AM. Reason: fixed code tags
357mag is offline   Reply With Quote
Old Apr 18th, 2007, 4:50 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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????? :beard:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 18th, 2007, 8:25 PM   #7
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
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.
357mag is offline   Reply With Quote
Old Apr 18th, 2007, 8:33 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 18th, 2007, 9:08 PM   #9
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
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.
357mag is offline   Reply With Quote
Old Apr 18th, 2007, 9:55 PM   #10
melbolt
Hobbyist Programmer
 
melbolt's Avatar
 
Join Date: Feb 2005
Location: PA, USA
Posts: 237
Rep Power: 4 melbolt is on a distinguished road
Send a message via AIM to melbolt Send a message via Yahoo to melbolt
Quote:
Originally Posted by 357mag View Post
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.
I'm not sure if this is what you are looking for, but the reason cin.get() keeps your console window from closing is because it is awaiting input of a character from the user.

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?
__________________
I have never let my schooling interfere with my education. -Mark Twain-

Xbox live gamertag: melbolt
melbolt is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Slackware installation guide for Linux beginners coldDeath Coder's Corner Lounge 104 Jul 29th, 2007 4:40 AM
please help me to compile cwl157 C++ 37 Mar 6th, 2006 6:12 PM
replace space with ' * ' TecBrain C++ 15 Apr 13th, 2005 12:32 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM
Whats wrong... brandcolt C++ 6 Mar 1st, 2005 10:37 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:37 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC