1

I'm having a problem where my for-loop skips over the getline function. If I replace it with std::cin then it works, so I think it's something related to what I inputted in getline.

Here's my code.

void setLocations(int amount) {
    locations = new std::string[amount];
    locations[0] = startingLocation;

    // starts at 1 because we want to skip first index. The amount is set at 2 by default, so the loop should iterate at least once.
    for (int x = 1; x < amount; x++)
        std::getline(std::cin, locations[x]);    
}
Jacob Macallan
  • 929
  • 2
  • 7
  • 26

1 Answers1

2

May be you use 'std::cin>>someVar' before 'setLocations' function call, which doesn't consume newline. To resolve, use this code segment before 'for' loop

std::cin.ignore(1, '\n');
ashiquzzaman33
  • 5,611
  • 5
  • 30
  • 42