cin.getline() skipping input!

Hey, I'm still relatively new to c++. I have a definition of cin.getline(array,arraysize);

"This call reads input until the first newline or until arraysize characters have been read, whichever comes first. Any characters not read are left in cin for the next read to pick up."

Here is part of my code;

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
      cin.getline(e.address_1, MAX);

      cout  << "Please enter the second line of " << f << ' ' << s

                  << "'s address (return if blank)." << endl;

      cin.getline(e.address_2, MAX);

      cout  << "Please enter the third line of " << f << ' ' << s

                  << "'s address (return if blank)." << endl;

      cin.getline(e.address_3, MAX);

 

      cout  << "Please enter " << f << ' ' << s

                  << "'s postcode (return if blank)." << endl;

      cin.getline(e.postcode, MAX);

 

      cout  << "Please enter " << f << ' ' << s

                  << "'s email address (return if blank)." << endl;

      cin.getline(e.email, MAX);




where e.address1, 2 and 3 are arrays and MAX is the maximum arraysize.


When asked on the console to input the first e.address1, if I input 9 characters then it's fine. It inputs those 9 characters and an extra terminating zero in to e.address1, counting to 10 values. It will then ask me to input in to e.address2, and if this is again less than 10 values it will ask for e.address3, and so on. But if I input 10 characters in to e.address1 some crazy things happen.

From the definition of cin.getline() above, I would expect the first 9 characters to be read in to e.address1 and then a terminating zero to be added to the array. Then I would expect the remaining 10th character left in the cin to automatically be entered in to e.address2. Except it doesn't, it adds the first 9 characters in to e.address1, adds the terminating zero, but then inputs nothing in to e.address2, then it automatically adds nothing to e.address3, and so on. It never re-asks me to input in to the console at a later time.

As an example I mean this.

I cin "Hellotherebrothers"

cout << e.address1 gives the output "Hellother"

cout << e.address2 gives no output

cout << e.address3 gives no output

similarly e.email, e.postcode give no output.

So once I pass the maximum arraysize, it inputs all it can in to e.address1 and then never inputs anything in to the follow cin variables? And also never asks me to input anything in again?
Last edited on
Topic archived. No new replies allowed.