微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 getline() 函数

如何解决如何使用 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 举报,一经查实,本站将立刻删除。