Loading 3 lines using getline and cin(object prog., for, functions)

Hello. This is my first post on this forum. Pls let me know If i posted it on the wrong section.

I am doing exercise in C++. Program suppose to load your name, surname and age. Name and surname should be loaded by getline, and the program should get sure that age is loaded correctly.
The problem is that when loading second person, the program skips the name and goes to surname automatically. I've tried few things and cant figure out what is wrong with it. This is log of the program:

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
#include <limits>
using namespace std;

void loadPerson(string & name, string & surname, int & age)
{
   cout << "Name: ";
   getline(cin, name);
   cout << "Surname: ";
   getline(cin, surname);
   cout << "Age: ";
   while(!(cin >> age))
   {
   cin.clear();
   cin.ignore(numeric_limits <streamsize>::max(), '\n');
   }
}

void printPerson(string & name, string & surname, int & age)
{
    cout << "Name: " << name<< endl;
    cout << "Surname: " << surname<< endl;
    cout << "Age: " << age << endl;
}

int main()
{
    string name[2];
    string surname[2];
    int age[2];
    for (int i=0; i<2; i++)
    {
         cout << i+1 << " person." << endl;
        loadPerson( name[ i ], surname[ i ], age[ i ] );
    }
    cout << endl;
    for (int i=0; i<2; i++)
    {
        cout << i+1 << " person." << endl;
        printPerson(name[i], surname[i], age[i]);
    }

    return 0;
}


Thanks for any suggestions!
Last edited on
Hello @sosen88.
You are mixing getline(), which removes the newline character from the stream after it has extracted what it wants, and stream extractor >>, which leaves it in ... just to confuse the next call of getline.

I once had a go at suggesting a few different ways around this:
http://www.cplusplus.com/forum/general/252304/#msg1110643

Note that your while{} loop in loadPerson(), which was presumably intended to obviate the problem, won't actually run if cin>> was successful, so the next next call to getline() will pick up a blank. Simply changing
while(!(cin >> age))
to
cin >> age;
would actually cure that, provided you left the remaining lines as they are.



BTW, you appear to have used [output] tags (I think), rather than [code] tags in your post. You want the first item in the format menu. If you put it in the appropriate tags then we would be able to run it in the online compiler without having to download it - answers will be better and quicker.
Your posting area is fine, but if that is indeed your code layout then it would benefit from some indentation to reveal program flow.
Last edited on
I get what was wrong now.
But if I remove while from the function, then if I would try to load letter instead of a number, the function would not loop me another time so I can try again and load correctly(number). So the loop needs to stay. I figure that adding another cin.ignore after the loop also solves the problem. But I am not sure if that is the best way to do it.

1
2
3
4
5
6
   while(!(cin >> wiek))
   {
   cin.clear();
   cin.ignore(numeric_limits < std::streamsize >::max(), '\n');
   }
   cin.ignore(1000, '\n');


Thanks for answering!
Last edited on
Hello sosen88,

What you would want is something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cout << " Age: ";
std::cin >> age;

while (!cin)
{
        std::cout << "\n    Invalid input! You entered something other than a number.\n" << std::endl;

	cin.clear();
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

	cout << "Age: ";
	std::cin >> age;
}

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>. 


std::cin >> age; is formatted input, works best with numeric variables, what this means is that "cin" is expecting a number to be entered. Should you enter something other than a number like a letter or punctuation character "cin" will fail setting either the "fail" bit or "bad" bit or both. It also sets the good bit from (1) to (0) which is what the while condition is checking for.

If you bypass the while loop line 15 will clear the input buffer before the next "getline". This does not conflict with the while loop because when you leave the while loop that "cin" will leave the new line character in the input buffer for line 15 to clear.

Hope that helps,

Andy

P.S.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


Last edited on
Topic archived. No new replies allowed.