That discards at most 128 unread characters from the input stream up to and including the first newline. (If you are entering input from the console, there probably can't be unread characters beyond the first newline.)
That discards at most 128 unread characters from the input stream up to and including the first newline. (If you are entering input from the console, there probably can't be unread characters beyond the first newline.)
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Your analysis is on the right track, but your solution is not particularly good. The indiscriminate use of std::cin.get() is unnecessary at many points. Where it is necessary (and it is not necessary in this program), it would be better to use std::cin.ignore() instead (possibly supplying the number of characters to ignore), or the std::ws manipulator.Originally Posted by vampirekid.13
As a way to prevent the closing of the console window, it is a poor man's solution compared to what matsp, anon and JVene have suggested.
EDIT:
That would be wrong, since it means that if a user enters the numbers all on a single line, only the first number will be read, unless enough numbers were added to be beyond 128 characters in length.Originally Posted by anon
Last edited by laserlight; 05-06-2009 at 11:07 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I meant these. Naturally don't empty the buffer if you don't want it to be empty.the other thing is right before return 0; do another cin.get(); to keep the console up. if it still closes add another cin.get(); before return 0;
Code://not the right way cin.get(); cin.get(); cin.get(); return 0;
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
im no expert, shoot i only played w/ c++ for a few weeks, just doing my best to help.
i still dont fully understand how cin.ignore works.
what if someone inputs 10 chars with spaces between them instead of pressing enter?
also, in VC++ the console closes after printing the output w/o having a system("pause"); or cin.get(); before return 0;
Thanks for your reply "vampirekid.13". I have added cin.get(); after every cin >> and it works perfectly now.Thanks again everybody for your help.
cin.ignore empties the stream so that those characters are not there anymore. Without that following input functions like cin >>, cin.get, getline etc keep reading those characters as each of them should without pausing for further input until there is nothing left to read.i still dont fully understand how cin.ignore works.
what if someone inputs 10 chars with spaces between them instead of pressing enter?
I think a shortcut was already mentioned (Ctrl + F5?).also, in VC++ the console closes after printing the output w/o having a system("pause"); or cin.get(); before return 0;
Those artificial means to keep windows open are just a crutch. It is important to know about cin.ignore though.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.