![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 | |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 213
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. |
|
|
|
|
|
|
#12 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
'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 |
|
|
|
|
|
#13 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#14 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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? |
|
|
|
|
|
#15 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#16 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#17 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#18 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#19 |
|
Programmer
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#20 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| 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 |