如何使用 getline() 函数

问题描述

我正在练习在我的代码中使用 getline()。但是在第二个 while 循环中,我无法输入。它跳转到“按 [1] 继续”。我在想我是否只能使用 getline() 一次。请帮我解决问题

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;
  }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)