Stuck on Input problem

This is a discussion on Stuck on Input problem within the C++ Programming forums, part of the General Programming Boards category; I'm having problems getting input in my console app. cin.getline is leaving characters in the input buffer if the user ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    203

    Stuck on Input problem

    I'm having problems getting input in my console app. cin.getline is leaving characters in the input buffer if the user inputs more than what getline is set to recieve. I looked at how to clear the buffer in the faq and from searching the board, but none of them seem to work properly. I believe the issue is that all other input I have uses ReadConsoleInput. I'm trying to get the user to input a name that can contain spaces, so I want them to be able to see what they type and be able to correct typos with backspacing. I've tried using ReadConsole and it almost works, but it ruins my entire input and output afterwards. I've also tried various ways with ReadConsoleInput, but haven't had any success that way either. I'm putting together a sample program with only the needed coding to show what's happening, so i'll attach it when i get it done if it's needed to help.

    edit: Added cpp file with sample code. I'm using MSVC++ 2005 express. It has memory leaks in that sample, but i didn't include my class which is where i'm storing the name.
    Attached Files Attached Files
    Last edited by Syneris; 01-23-2006 at 09:40 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Use getline with a string?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    char array and cin

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    I haven't used strings at all, just char array type strings. I'm not sure where to begin with them. None of the books i've read have mentioned them, and I haven't taken the time to find a page that shows how to use them.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
       std::string s;
       std::cout << "enter a string: ";
       if ( std::getline(std::cin, s) )
       {
          std::cout << "s = \"" << s << "\"\n";
       }
    }
    
    /* my output
    enter a string: All work and no play makes Jack a dull boy.
    s = "All work and no play makes Jack a dull boy."
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with keyboard input
    By fighter92 in forum Game Programming
    Replies: 6
    Last Post: 03-20-2009, 09:41 AM
  2. Problem reading input
    By gp364481 in forum C Programming
    Replies: 3
    Last Post: 09-24-2008, 05:10 PM
  3. file input output problem..
    By epidemic in forum C++ Programming
    Replies: 10
    Last Post: 12-03-2006, 02:55 AM
  4. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 12:50 AM
  5. Problem with text input
    By newbie in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2002, 03:44 PM

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