-1

If the input is smaller than 128 characters, I have no problems. But if the input is longer than 128 characters, the loop is passing through all the time and I get spammed with: ERROR: You must type something. I can't do something until I close the console and finish the program.

It seems like the cin.getline() function is not called and the command variable is always empty.

My plan was, that the command was too long and I want a new input...

Where is the problem???

...

#define MAX_COMMAND_LENGTH 128


int main()
{
    char command[MAX_COMMAND_LENGTH];

    while (1)
    {
        cout << "\nSend command: ";

        cin.getline(command, MAX_COMMAND_LENGTH);


        if ((strlen(command) == 0))
        {
            cout << "ERROR: You must type something." << endl;
            continue;
        }
        else if (strlen(command) > MAX_COMMAND_LENGTH - 2)
        {
            cout << "ERROR: The command may only be 128 characters long." << endl;
            continue;
        }

        ...
    }
    return 0;
}

I also tried something with: cin.ignore(MAX_COMMAND_LENGTH, '\n');, but it does not work :S

  • Provide an example complete with input and expected output. Or just use a `std::string` instead. – user657267 Mar 12 '15 at 05:50
  • possible duplicate of [Program skips cin.getline()](http://stackoverflow.com/questions/11835226/program-skips-cin-getline) – Ab5 Mar 12 '15 at 06:10

2 Answers2

0

Your problem is that stream's error flags get set when getline finds more characters than the maximum set. (see the description at std::basic_istream::getline

count-1 characters have been extracted (in which case setstate(failbit) is executed).

and

If the function extracts no characters (e.g. if count < 1), setstate(failbit) is executed.

The following worked for me: (sorry, changed the max to 10... saved some typing)

#define MAX_COMMAND_LENGTH 10
#include <iostream>
#include <cstring>


using namespace std;


int main()
{
    char command[MAX_COMMAND_LENGTH+1];

    while (1)
    {
        cout << "\nSend command: ";

        cin.getline(command, MAX_COMMAND_LENGTH);
        command[MAX_COMMAND_LENGTH]='\0';


        if ((strlen(command) == 0))
        {
            cout << "ERROR: You must type something." << endl;
            cin.clear(); // <--- reset the flags
            continue;
        }
        else if (strlen(command) > MAX_COMMAND_LENGTH - 2)
        {
            cout << "ERROR: The command may only be 10 characters long." << endl;
            cin.clear(); // <--- reset the flags
            cin.ignore(MAX_COMMAND_LENGTH,'\n'); // <-- skip unred chars
            continue;
        }
        cout << "Command received is: " << command << endl;
    }
    return 0;
}

The output is:

Send command:
ERROR: You must type something.

Send command: 1
Command received is: 1

Send command: 12
Command received is: 12

Send command: 123
Command received is: 123

Send command: 12345678
Command received is: 12345678

Send command: 1234567890
ERROR: The command may only be 128 characters long.

Send command: 12345678901
ERROR: The command may only be 128 characters long.

Send command: 12345678
Command received is: 12345678

Send command: 1234
Command received is: 1234

Send command:
jsantander
  • 4,972
  • 16
  • 27
0

You need to read the documentation for getline more carefully.

It says that getline will read characters from the stream

until any of the following occurs (tested in the order shown):

  • end of file condition occurs in the input sequence (in which case setstate(eofbit) is executed)

  • the next available character c is the delimiter, as determined by Traits::eq(c, delim). The delimiter is extracted (unlike basic_istream::get()) and counted towards gcount(), but is not stored.

  • count-1 characters have been extracted (in which case setstate(failbit) is executed).

You will be hitting the last condition, so the failbit will be set. Once this is set you won't be able to proceed until you clear the error condition by calling clear.

Also note that you don't need to check the length of the returned string in the way that you are. You only need to check the failbit to determine if the buffer was exceeded.

Finally note that you can find out how many characters were actually read using gcount()

harmic
  • 28,606
  • 5
  • 67
  • 91