cin problem

This is a discussion on cin problem within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm having another problem, this time with "std::cin". Maybe I don't know how it really works out, but I ...

  1. #1
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114

    cin problem

    Hi, I'm having another problem, this time with "std::cin". Maybe I don't know how it really works out, but I got a problem with emptying the function's buffer or whatever. So basically, I have this code, that checks for if any key is pressed at X moment, then I wait until none of the keyboard keys are down. Right after that I use cin to catch some data...

    Code:
    //Wait until no key is being down...
    void Wait()
    {
    	for (;;)
    	{
    		bool	Waiting = true;
    		for (UCHAR i = 0; i < 255; i++)
    		{
    			if (GetAsyncKeyState(i))
    			{
    				Waiting = false;
    				break;
    			}
    		}
    		if (Waiting) return;
    	}
    }
    
    //Just wait until the "Key" gets pressed...
    void Wait_For_Key(UCHAR Key)
    {
    	for (;;)
    	{
    		if (GetAsyncKeyState(Key)) return;
    	}
    }
    
    //here is the code, under main()...
    //Let's say it just wait until the user presses "A"...
    Wait_For_Key(65);
    //Now wait until all keys, even "A", are released...
    Wait();
    //Affect something to a variable...
    std::cin >> some_var;
    So the problem with this, but in fact, it's more of an annoyance, is that what I expect to happen doesn't happen. By this I mean that I suppose that "cin" shouldn't be receiving data when it is called, because previously I made sure that "Wait()" would wait for all keys to be released. But to my misunderstanding, when cin is finally called (when I have released "A"...), there's an "A" displayed in the console, and I think it shouldn't be there, as I wasn't pressing any key while cin was called...

    I tried to call cin.clear() but it doesn't work on that... Do you understand?

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    cin justs gets data in the input buffer ( what you type ) and you have to hit enter for it to get data, and for it to get that data automatically you could use cout to write the data to the screen then simulate the enter key so it recieves the data then clear the screen instantaneously, and it should work, that is if I understand your problem right

  3. #3
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    I haven't thougth about that! Well, I'll try it tomorrow, but looking at the cin's structure, it look like "cin" is a kind of file. To explain this, let say you have an "std::fstream" object, you can see that its method are quite too much similar to those of "std::cin", which makes me believe that "cin" is actually a file, since you can do "cin.read()" such as "File_Var.read()"... Maybe there is some other way to clear cin...


    EDIT: Finally I deceided to try it now, but it didn't worked!

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    you should try to call cin.sync() after cin.clear()
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by mikahell
    I haven't thougth about that! Well, I'll try it tomorrow, but looking at the cin's structure, it look like "cin" is a kind of file. To explain this, let say you have an "std::fstream" object, you can see that its method are quite too much similar to those of "std::cin", which makes me believe that "cin" is actually a file, since you can do "cin.read()" such as "File_Var.read()"... Maybe there is some other way to clear cin...
    Close, both fstream and cin are streams; they both inherit from std::istream.

    The bad news -- there's no true way to do this that always works on all compilers.

    The somewhat good news -- IF you are not releasing the source code and IF you are not moving compilers and IF your compiler supports it, you can often use cin.flush() which, although the behavior is totally undefined by C++ standards, is typically implemented as a clear of the input stream.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    use

    Code:
    cin.ignore(1000, '\n');
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    The somewhat good news -- IF you are not releasing the source code and IF you are not moving compilers and IF your compiler supports it, you can often use cin.flush() which, although the behavior is totally undefined by C++ standards, is typically implemented as a clear of the input stream.
    No, do not use cin.flush(), it simply is not for input streams.
    It should be possible to use cin.sync(), though Prelude has argued that its behaviour for use in 'flushing' the input stream may not be guaranteed due to ambiguity in the wording of the standard.

    Alternatively, after including <limits>, this would be guaranteed to effectively 'flush' the standard input stream:
    Code:
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by Ideswa
    use

    Code:
    cin.ignore(1000, '\n');
    Best to stay away from magic numbers
    Code:
    #include <limits>
    
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
    EDIT: Curses... foiled again.
    Last edited by SlyMaelstrom; 08-22-2006 at 02:33 AM.
    Sent from my iPadŽ

  9. #9
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    324
    Quote Originally Posted by mikahell
    I haven't thougth about that! Well, I'll try it tomorrow, but looking at the cin's structure, it look like "cin" is a kind of file. To explain this, let say you have
    Linux treats pretty much everything as a file, from console output to the hard disk to network communications, from what I understand, which isn't that much.

  10. #10
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    Well, I'm not using Linux, but BTW it should work in the same way with Windows...

    But I tried putting this in my code, but still the "A" appears...

    Code:
    Wait();
    
    //Trying something
    std::cin.clear();
    std::cin.sync();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    
    //Affect something to a variable...
    std::cin >> some_var;
    I'm all confused, but I would really like to be able to see how that cin's code works out in the background...

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    The use of sync, flush, or ignore do not affect whether the character is displayed in the console, they only affect whether that character will be in the input buffer that you read from with cin.

    If you don't want the 'A' to appear and you don't want it to be a part of the input, you have to use platform-dependent code. Perhaps kbhit() or getch() will help, although I don't have experience with either. Also, since you are already using GetAsyncKeyState, I would imagine there would be another similar windows-specific function to help you.

  12. #12
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    Okay I will try with "getch()", though it is used to only get 1 char... And I need to "cin" a number...

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Well after you use getch() to get the A, perhaps you can use cin to get the number. Or, just use getch to get each character into a string and parse the string as you see fit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with cin. Please help me.
    By Antigloss in forum C++ Programming
    Replies: 17
    Last Post: 06-06-2005, 09:50 AM
  2. Input File HELP, weird problem
    By gravity-1 in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2005, 07:43 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Problem with cin
    By ErionD in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 10:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21