getline(); only executes once

Hello, this is my first time using getline() to read in a string, I normally just use Cin, but that doesn't work if there's a space. The issue I'm having is that it will only ask for the name of the first player. After that, it will ask for the age of players 1-5, but their names are just skipped over and left as a blank line.

I am not very experienced, but from my research I gathered that it has something to do with a conflict between cin and getline. It seems like something is being left in the buffer, or stream, I don't have a clear grasp of what that means and how to fix it.

I would really appreciate if someone could point me in the right direction, all of the resources I've found explain it in a very advanced way.

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
#include <iostream>
#include <string>                 // string class, getline??
#include <limits>                 // numeric_limits (bad input)
using namespace std;

#define ROSTER_SIZE 5

int main(){

   struct team_Info{
      int age;
      int roster_num;
      string name;
   } member[ROSTER_SIZE];

// Prompts for and reads in names
   for (int i=0; i<ROSTER_SIZE; i++){
      cout << "Enter a name for player #" << i+1 << ": ";
      getline(cin, member[i].name);
// Prompts for, reads in, and checks valid ages.
      while (cout << "Enter an age: " && !(cin >> member[i].age)) {
         cin.clear(); //clear bad input flag
         cin.ignore(numeric_limits<streamsize>::max(), '\n'); //discard input
         cout << "Invalid input; please re-enter.\n";
      }
   }
}
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/197263/
Topic archived. No new replies allowed.