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: 298
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, 4:32 PM   #4
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, 12:28 PM   #5
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: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, 10:16 PM   #8
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 214
Rep Power: 3 Seif is on a distinguished road
Exclamation

Quote:
Originally Posted by 357mag View Post
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.
With all due respect chap your question did not require two seperate threads, and if you would read fully the replies given on both threads, there have been a few good responses that answer your question. We appreciate that everyone has to begin somewhere. But you have to appreciate that we give this help out of the kindness of our good souls, and when week by week we get new beginners asking the same questions, not reading the forum rules and spamming with new threads, It get kinda tedious. A simple forum search for the term "cin.get" brings up 3 pages of similar related threads.

Also by the fact that you havent indented your code nor put it in code tags makes it a nightmare for us to read, making it harder for us to help you. And can be seen disrespectful by some of the fellow forum members that you won't take the extra time to help us, help you.

#include "stdafx.h"
#include "iostream"
#include <cstdio>

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;

fflush(stdin);
cin.get();

return 0;
}

There much better, and if you look closely at the code you may see a little brucey bonus

sorry for the rant. its not a dig. just hoping I can put it into perspective why some of us come off as sarcastic jerks.
Seif is offline   Reply With Quote
Old Apr 18th, 2007, 8:33 PM   #9
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   #10
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
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 4:43 AM.

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