0

I was practicing the use of getline() in my code. But in the second while loop, I can't enter an input. It jumps to "Press [1] to Continue". I'm thinking if I can only use getline() once. Please help me to solve the bug

string name[100];
string address[100];
int timesName = 0;
int timesAddress = 0;
int option;
int i = 1, j = 1;

while (i != 0) {
  cout << "Enter Name: " << endl;
  getline(cin, name[timesName]);
  cout << "Press [1] to Continue" << endl;
  cout << "Press [2] to Stop" << endl;
  cin >> option;

  if (option == 1) {

    i = 1;
    timesName++;
  } else {
    i = 0;
  }
}

while (j != 0) {                     // SECOND WHILE LOOP
  cout << "Enter Address: " << endl; // this is where the bug occur
  getline(cin, address[timesAddress]);

  cout << "Press [1] to Continue" << endl;
  cout << "Press [2] to Stop" << endl;
  cin >> option;

  if (option == 1) {

    j = 1;
    timesAddress++;
  } else {
    j = 0;
  }
}
Biffen
  • 5,354
  • 5
  • 27
  • 32
kenmark
  • 1
  • 2
  • Depending on your use-case scenario, I recommend either not mixing `getline` and `>>` stream input, or being very careful, or using something that allows for more interactivity like [ncurses](https://invisible-island.net/ncurses/announce.html). – Eljay Mar 16 '21 at 15:32

0 Answers0