cin.getline() "saving unused input"

Hello,

I've started C++ just a week ago, and I'm having the following problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void write_record()
{
    int record_number;

    fstream database("8.4.3.txt", ios::binary | ios::out | ios::in);
    if(!database)
        {
            cout<< "File cannot be open.\nReturning to Menu.";
            return;
        }

    cout<<"Enter record number: ";
    record_number=get_int(0);

    database.seekp(record_number*record_size); //Skips record_number*record_size bytes

cout<<"Make: ";
    cin.getline(make, 20);
    cout<<"Year: ";
    cin.getline(year,5);
    cout<<"Mileage: ";
    mileage=get_int(0);

    database.write(make,21);
    database.write(year,6);
    database.write(reinterpret_cast<char*>(&mileage),sizeof(int));


    database.close();
}


So the thing is:

On the bolded code I'm asking the user to input make and year. Now if the user types an year with more than 4char (year -> char year[6]), the mileage assigment prompt is not prompted to the console.

I'm guessing that the extra characters are being automatically sent to the mileage var. How can I prevent this from happen?

Thank's in advance and sorry for my bad english but I'm Portuguese,

Hugo Ribeira

P.S.: Also when I use getline(str, max_len) does maxlen already take one byte for the null? I'm learning with Cpp without fear and it says to set array size to plus one than max_len.

However in this code I set it max_len=4 and array_size=5 and when I inputed a four char year, the 4th char wasn't beeing included in the year array.
http://www.cplusplus.com/reference/iostream/istream/getline/
Characters are extracted until either (n - 1) characters have been extracted or the delimiting character is found
The ending null character that signals the end of a c-string is automatically appended to s after the data extracted.

I'm guessing that the extra characters are being automatically sent to the mileage var. How can I prevent this from happen?
Ignore the rest of the line cin.ignore(numeric_limits<streamsize>::max(), '\n');
Thank's for the help, but still not working though.

I've changed my code to:

1
2
3
4
5
6
7
    cout<<"Make: ";
    cin.getline(make, 20);
    cout<<"Year: ";
    cin.getline(year,5);
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cout<<"Mileage: ";
    mileage=get_int(0);


also I had to include <limits> header because my compiler was complaining about numeric_limits not beeing defined in the scope.

I never get a chance to input mileage, if I input a string larger than 4chars to year.

EDIT: I'm using Code Blocks with the standard compiler, if this helps.


EDIT2: Finally solved it.

changed my code to:


1
2
3
4
5
6
7
8
    cout<<"Make: ";
    cin.getline(make, 20);
    cout<<"Year: ";
    cin.getline(year,5);
    cin.clear();
    cin.sync();
    cout<<"Mileage: ";
    mileage=get_int(0);


Can you still explain me thought, why the first aprouch failled?

EDIT: I've discovered that this problem happens whenever I input something that goes out of the getline boundaries. So should I add cin.clear() and cin.sync() everytime I try to read a string with getline?

Is this the optimal way, or is there a more elegant approuch to it?

Sorry for all the questions but I'm just starting.
Last edited on
Errors are signaled by modifying the internal state flags:
failbit: This is also set if the function stops extracting because n-1 characters were extracted (n including the terminating null-character).

I didn't know that that was an error, sorry. It seems that istream::get does not report that as an error, however it will not discard the delimiter (so you will need to ignore it too).

Sorry I cannot be helpful. Never really care about input, and used std::string instead of char*
Reading the whole line with getline( cin, s ); and then validated it.
No worries problem solved now.

Thank's,

Hugo Ribeira
Topic archived. No new replies allowed.