Bizarre cin.get() problems (I suspect)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • foahchon
    New Member
    • Aug 2006
    • 13

    Bizarre cin.get() problems (I suspect)

    Hi, I'm trying to write a simple program (or portion of a program), that will re-prompt a user for input each time the user enters invalid input, and then exit once the user has entered valid input. Here's what I have so far in main():

    int main()
    {
    char input[SIZE];

    cout << "Enter a string: ";

    while (!InputString(i nput))
    {
    cout << "Invalid string entered. Try again: " << endl;
    }

    system("pause") ;
    return 0;
    }

    And here's the body of the InputString() function:

    bool InputString(cha r str[SIZE])
    {
    char ch;
    int x = 0;

    while ((ch = cin.get()) != '\n')
    {
    if (x != (SIZE - 1))
    {
    str[x++] = ch;
    }
    else
    {
    str[x] = '\0';
    return false;
    }
    }

    str[x] = '\0';

    return true;
    }

    The odd thing that ends up happening is, "Invalid string entered. Try again: " is displayed three times, and then the program exits as if valid input was entered. Anyone know what's going wrong? Thanks.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    what is the value of SIZE ? the program will work OK so long as less than SIZE characters are entered on a line. If more are entered it will keep reading the rest of the input stream until it finds less than SIZE characters on a line. Try using cin.ignore() to remove any characters on a line if input fails, e.g. also added a statement to print input if it is OK
    Code:
    int main()
    {
    char input[SIZE];
    
    cout << "Enter a string: ";
    
    while (!InputString(input))
    {
    cout << "Invalid string entered. Try again: " << endl;
    cin.ignore(1000,'\n');  // ignore rest of characters on the line
    }
    cout << "input " << input << endl;
    system("pause");
    return 0;
    }

    Comment

    • foahchon
      New Member
      • Aug 2006
      • 13

      #3
      Thanks a lot for your reply. I figured I might have to use cin.ignore(), I just wasn't sure where or how. The value of the SIZE constant is 101. The maximum number of characters allowed in order for a string to be valid is 100, plus a terminating null character. Should I use the SIZE constant in the cin.ignore() call, instead of 1000, which seems to be generic?

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        [QUOTE=horace1]ignore() extracts and discard characters
        Code:
        cin.ignore(int length, char t = EOF)
        Extracts and discards up to length characters from cin or until the termination character t or EOF is found (the termination character is extracted).

        I usually just put a large number for length (to make sure all excess characters are removed) but the recommended way is
        Code:
          cin.ignore(numeric_limits<int>::max(), '\n');
        where numeric_limits< int>::max() returns the maximum finite value of an int and the termination character is newline

        Comment

        Working...