Thread: user input pre-enter string?

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    27

    user input pre-enter string?

    Using getline(cin, string) is it possible to pre-enter the string so the user can edit it, a bit like when you press up in the console?

    Code:
    string astring;
    cout<<"Please change this text: ";
    astring = "This is to be in the prompt the users edits.";
    getline(cin, astring); //Prompts user input
    cout<<astring<<endl; //Outputs the edited string
    So the console would give this, the part after the colon being editable:
    Code:
    Please change this text: This is to be in the prompt the users edits.
    Last edited by mikeyp; 11-25-2009 at 12:19 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Yes, but I suppose you'll have to get into platform specific header files to access the right API calls.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    27
    So it's not something where you could simply run a C++ command to auto insert the pre-defined text into the prompt?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I don't think so.

    For example, on Windows I think your starting point should be WriteConsoleInput

    Something like this:

    Code:
    #include <iostream>
    #include <vector>
    #include <cctype>
    #include <windows.h>
    
    void send_to_input_stream(const std::string& msg)
    {
        std::vector<INPUT_RECORD> events;
        for (unsigned i = 0; i != msg.size(); ++i) {
            //sloppily handle characters A-Za-z and space 
            //TODO: do lots of work to handle all characters
            char c = std::toupper(msg[i]);
            if ((c >= 'A' && c <= 'Z') || c == ' ') {
                INPUT_RECORD rec_template;
                //fill in a key-down event
                rec_template.EventType = KEY_EVENT;
                rec_template.Event.KeyEvent.bKeyDown = TRUE;
                rec_template.Event.KeyEvent.wRepeatCount = 1;
                rec_template.Event.KeyEvent.wVirtualKeyCode = c;
                rec_template.Event.KeyEvent.wVirtualScanCode = MapVirtualKey(c, 0);
                rec_template.Event.KeyEvent.uChar.AsciiChar = msg[i];
                rec_template.Event.KeyEvent.dwControlKeyState = std::isupper(msg[i]) ? SHIFT_PRESSED : 0;
                events.push_back(rec_template);
    
                //change to add corresponding key-up event
                rec_template.Event.KeyEvent.bKeyDown = FALSE;
                rec_template.Event.KeyEvent.dwControlKeyState = 0;
                events.push_back(rec_template);
            }
        }
        DWORD written_count;
        WriteConsoleInput(GetStdHandle(STD_INPUT_HANDLE), &events[0], events.size(), &written_count);
    }
    
    int main()
    {
        std::cout << "Please change this text: ";
        send_to_input_stream("This is to be in the prompt the users edits");
        std::string input;
        std::getline(std::cin, input);
        std::cout << input;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    27
    ok, thanks, I'll look into this just as soon as I've finished figuring out printing. :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Replies: 5
    Last Post: 07-05-2005, 12:37 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM