cin.getline question (user's input too long)

This is a discussion on cin.getline question (user's input too long) within the C++ Programming forums, part of the General Programming Boards category; Hello everyone! I have the following problem. The user is asked to input some data with the following command: //declarations: ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    6

    cin.getline question (user's input too long)

    Hello everyone!

    I have the following problem. The user is asked to input some data with the following command:

    //declarations:
    char a[81];
    char b[81];
    char c[81];

    //user is asked to input three lines of text:
    cin.getline(a, 81, '\n');
    cin.getline(b, 81, '\n');
    cin.getline(c, 81, '\n');


    Now, the problem occurs if the user's input is longer than 80 characters.

    E.g. if the user inputs a string that is 90 characters long, then he cannot input the preceding lines (b and c). When using:

    cout << a << endl << b << endl << c << endl;

    the string a is shown as string of 80 characters, but the strings b and c are empty.

    Why does this occur?

    How to solve this problem?
    I cannot force user to input a string that is shorter than, let's say, 80 characters

    Thank you in advance!

    Bye!

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    use the ignore() function to ignore any excessive input. Similar, but not same as using fflush or endl with cout. It leaves things to be desired, but at least it works (most of the time).

    #include limits.h

    const int MAXINT = max_int// this syntax is wrong. look up the real syntax by doing a search of the board. I usually use 80 for MAXINT, but that's a hedge because I can't remember the correct syntax.

    char a[81];
    char b[81]

    cin.getline(a, 80, '\n');
    cin.ignore(MAXINT, '\n');
    cin.getline(b, 80. '\n');
    cin.ignore(MAXINT, '\n');

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, 01:20 PM
  2. User determined input question
    By lyoncourt in forum C Programming
    Replies: 8
    Last Post: 09-30-2007, 06:10 PM
  3. Displaying a 'long long' with printf
    By trinitrotoluene in forum C Programming
    Replies: 10
    Last Post: 12-28-2004, 12:32 AM
  4. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 01:13 PM
  5. simple input and string manipulation question
    By Stig in forum C Programming
    Replies: 1
    Last Post: 12-15-2001, 12:33 PM

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