Two problems with std::cin.getline()

This is a discussion on Two problems with std::cin.getline() within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by tabstop Well, but the whole point of ignore is that the only place I know the input ...

  1. #31
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by tabstop View Post
    Well, but the whole point of ignore is that the only place I know the input isn't going is adminPassword.
    Oh ok, gotcha. I see now.

  2. #32
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Quote Originally Posted by master5001 View Post
    Thank you Its nice to have someone second that opinion.
    What would that do with the excess input in std::cin? Just silently discard it?

  3. #33
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yep.

  4. #34
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You are free to look at this.

  5. #35
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by maxhavoc View Post
    What would that do with the excess input in std::cin? Just silently discard it?
    Well, there wouldn't be any excess input. A string can be whatever number of characters it needs to be; why stop at 16?

    And of course, if you need to interface with char *-expecting functions, there is the .c_str() member function of a string.

  6. #36
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I don't normally tell people to do this, but the only reason I make an exception today is because this is the 36th post on this thread. And at this point you are just now starting to take the very first suggestion you were given all the way back in post #2 on this thread; feel free to kick yourself.

  7. #37
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    Quote Originally Posted by maxhavoc View Post
    master5001, the problem with that approach is that allows ONLY 16 characters, not up to and including 16 characters.
    This may be another option for you. I'm not gonna bother explaining too much, since this is really simple code.

    If you have specific needs then roll your own. The standard IO functions are building blocks, not your personal toolbox of goodies.
    Code:
    #include <iostream>
    #include <ostream>
    #include <cstring>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main ()
    {
        const std::streamsize MAX = 17;
        char buffer[MAX];
    
        const std::streamsize oldw = cin.width();
        cin.width( MAX );
        cout << "Enter 16-character phrase: ";
        cin >> buffer;
        if ( cin.peek() != '\n' || std::strlen( buffer ) < MAX - 1 ) {
            cin.ignore( (unsigned)-1 ); //ignore any extraneous stuff
            cout << "Invalid phrase, try again\n";
        }
        else {
            cin.ignore(); //ignore newline
            cout << "You entered \"" << buffer << "\"" << endl;
        }
        cin.width( oldw );
        //copy buffer, do whatever
    }
    Last edited by whiteflags; 10-15-2008 at 07:58 PM.

  8. #38
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Quote Originally Posted by tabstop View Post
    Well, there wouldn't be any excess input. A string can be whatever number of characters it needs to be; why stop at 16?

    And of course, if you need to interface with char *-expecting functions, there is the .c_str() member function of a string.
    Is there a way to limit the number of characters getline accepts? I know that std::string simply grows as large as it needs, but this could lead to denial of service attack through resource exhaustion if there is no way to limit. Also, is there a way to get the count of elements actually read?

  9. #39
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by maxhavoc View Post
    Is there a way to limit the number of characters getline accepts? I know that std::string simply grows as large as it needs, but this could lead to denial of service attack through resource exhaustion if there is no way to limit. Also, is there a way to get the count of elements actually read?
    gcount still exists.

  10. #40
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Quote Originally Posted by tabstop View Post
    gcount still exists.
    String does not have a gcount function. When I try to use std::cin.gcount it returns -1.

  11. #41
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by maxhavoc View Post
    String does not have a gcount function. When I try to use std::cin.gcount it returns -1.
    In that case, use the .size() member function of the string.

  12. #42
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Quote Originally Posted by tabstop View Post
    In that case, use the .size() member function of the string.
    Ok, that will work so long as I can restrict the number of characters read in from std::cin during the getline() call. Do you know how I can do that?

  13. #43
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by maxhavoc View Post
    Ok, that will work so long as I can restrict the number of characters read in from std::cin during the getline() call. Do you know how I can do that?
    All I know is that you can't. (TBH, I don't see worrying about a DOS as useful here, since if they can throw 4G characters at your C++ program, they can throw 4G characters at your C program just as well with the same result; you're going to run into the maximum length of stdin well before that anyway, I should think.)

  14. #44
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Okay, thanks for the help everyone. I think the lesson I'm taking home from this is don't program in C++ anymore if I ever want to have secure console input.

  15. #45
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    Did I miss something important? Why was my snippet insufficient to your needs?

Page 3 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

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