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, 11:16 PM   #11
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 242
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, 11:20 PM   #12
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
'flush' is for output streams. ignore will ignore (get rid of) a specified number of characters in an input stream. There are a couple of overloaded versions. The use of cin.sync () is effective in many systems, but there is some question of it being a standard thing. I honestly can't comment on that part. Reading random comments on the purpose of cin.get is hardly as effective as just turning to the documentation.

To expand on Mel's comment, which is correct: PROGRAMS ARE DESIGNED TO TERMINATE WHEN DONE. If you launch your program from the command line, the window belongs to the command interpreter. When your program terminates, the window remains (it belongs to the command interpreter) and you get a new prompt. You can also see what went before.

If you have an IDE that executes the program in it's OWN window, then when the program terminates, its window goes away. What to do? What to do? CHANGE THE TERMINATION REASON. Make it terminate on user input. Input comes from a stream. Make sure it's empty, or input will be seen, it will terminate, the window will go bye-bye.

Why is there junk input? Because one asked for input of a specified kind. The user entered more than that (if only by pressing the ENTER key, which is required to be present before input is injected into the stream). Don't want it there? Get it out.
__________________
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, 11:23 PM   #13
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Okay, I posted that while Seif was posting. Fflush (stdin) is not defined to work, though some compilers will implement it. It's also a C, not C++ paradigm. In C, one can use "rewind (stdin);". That sounds silly, but just read about it. In C++, just check out "ignore".
__________________
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 19th, 2007, 2:37 AM   #14
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
Well I kind of understand it but I still think its messed up. Here is the way I'm looking at it. Let's say the screen says "Enter first integer". Let's say I enter the integer 10. The number 10 is in the input stream. After that I must hit the Enter key. But from what I've read, when you hit the Enter key the program will read the number but it leaves the Enter keystroke unprocessed. Now my understanding of it so far is that the first cin.get() will process or take card of this first Enter keystroke.

Now moving on the program asks me to enter the second integer. Let's say I enter the number 12 and then press Enter. The program reads the number 12 but again leaves the Enter key unprocessed. Well if that's true wouldn't the second cin.get() process that stroke, and it seems to me the window would immediately close. So now it seems to me I would need not one, not two, but three cin.get() to keep the window open?
357mag is offline   Reply With Quote
Old Apr 19th, 2007, 9:39 AM   #15
rsnd
Hobbyist Programmer
 
rsnd's Avatar
 
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4 rsnd is on a distinguished road
clearly you didn't understand it. Did it not have to get past your first enter to read your second number?

bugger all that! ull learn them when you make your first scanner. use good old system("PAUSE");
__________________
Spread your wings and fly! Chicken!
rsnd is offline   Reply With Quote
Old Apr 19th, 2007, 10:21 AM   #16
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Except that system ("PAUSE") won't work on all systems. If you change systems, you'll just have to learn a new kludge.

In the first place, you may ask for a number and the user will enter two or more separate numbers, a half-dozen random characters, then hit ENTER. This leaves you with more than one character, so there's no point in trying to count ENTERS. Just use cin.ignore, as mentioned, and get the freakin' job done. This thread has gone beyond silliness.
__________________
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 19th, 2007, 7:44 PM   #17
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
So when I enter in the first integer, followed by the Enter key neither one of those actions has any bearing or relation to the first cin.get(). When I enter in the second integer, followed by the Enter key, the first cin.get() processes the Enter keystroke, and then when the program continues on down the page it stops at the second cin.get() and that is what causes the window to stay open, that is until I hit a final Enter key to close the window. That must be how it works.
357mag is offline   Reply With Quote
Old Apr 19th, 2007, 9:00 PM   #18
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I'm going to recommend that you go back to playing the sitar. It's ugly, but it doesn't mess up a lot of people's lives.
__________________
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 20th, 2007, 4:04 AM   #19
Twilight
Programmer
 
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3 Twilight is on a distinguished road
While we're on the topic of input pauses, maybe you could explain something to me DaWei.

I was trying to make a little portable version of system("Pause") I could just toss in where I needed it. After cruising the internet, and playing with stuff for a while, I came up with this:

void LeastSquares::userPause()
{
     std::cin.clear(); 
     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
     std::cout << "Press Enter to continue . . .\n";
     //cin.get();
     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

It works alright on my machine, but I'm not exactly sure why. In all the original versions I saw it had cin.get() uncommented, and didn't include the second ignore statment. But when I did that it was eating up something from the terminal anyway, even though I thought it was ignoring everything, including the last newline it saw.

Worked once I put the other ignore in though, but that was even more surprising.
Twilight is offline   Reply With Quote
Old Apr 20th, 2007, 6:56 AM   #20
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
That form of ignore will only ignore up to the next newline. If there's more trash beyond that, it won't be ignored.
__________________
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
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 5:40 AM
please help me to compile cwl157 C++ 37 Mar 6th, 2006 7:12 PM
replace space with ' * ' TecBrain C++ 15 Apr 13th, 2005 1:32 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 5:12 PM
Whats wrong... brandcolt C++ 6 Mar 1st, 2005 11:37 AM




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

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