Thread: Default string with cin.getline()?

  1. #16
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Eibro
    Grrrrr I can't believe I left it in there Now i'll have to go back and edit it...
    Does the updated code compile on codewarrior?
    Yes, it does. No errors and no warnings.

    Originally posted by Eibro

    Hmm, this wouldn't work for ofstream arguments would it? If not, cerr would be one other valid argument to the function.
    Actually cerr/clog would be more valid than cout. cerr is guaranted to point to the screen, whereas cout can point to a file. There's no harm in keeping the ostream parameter, but you could switch the position of the two parameters and make cout/cerr/clog the default.

    If you use this method with an ofstream object, you'll fill the file with strange characters ('\b').
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  2. #17
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Heh, I'm having too much fun with this :)
    It's been WAAAY overdone, but I've improved the code significantly:
    Code:
       std::string GetInputDefault(const std::string &defaultin, std::ostream &out = std::cout)
       {  
          std::vector<char> inputvec(defaultin.begin(), defaultin.end());
          out << defaultin;
          
          while (true)
          {
             int in = getch(); // Get keycode
             switch (in)
             {
             case 13: // Enter
                return std::string(inputvec.begin(), inputvec.end());
             case 8:  // Backspace
                if (inputvec.size() != 0)
                {
                   inputvec.pop_back(); // Remove last character entered
                   out << "\b \b" << std::flush; // Remove from screen
                }
                break;
             default:
                inputvec.push_back(in); // Insert character
                out << static_cast<char>(in) << std::flush; // Print
             }
          }
       }
    edit: Thanks Sang-Drax
    Last edited by Eibro; 02-01-2003 at 03:59 PM.

  3. #18
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    To make it work in CodeWarrior: add std::flush to every out-statement.
    It seems that CodeWarrior doesn't automatically flush the input buffer when using conio-functions.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM