![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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? |
|
|
|
|
|
#2 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 298
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 214
Rep Power: 3
![]() |
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?
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#8 | |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 214
Rep Power: 3
![]() |
Quote:
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. |
|
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |