为什么getline()函数没有在我的代码的用户定义函数中接受用户输入?

时间:2019-10-22 01:06:18

标签: c++ string

这是我的代码。当我运行我的代码并选择='n'时,执行createAccount()函数,但它不等待用户输入,否则显示此输出Code Output image

#include <iostream>

using namespace std;

string customerName;
string username;
string password;
double withdrawMoneyAmount;
double dipositMoneyAmount;

void createAccount();

int main()
{
cout << "Welcome to X Bank" << endl;
cout << "Do you have any Account here? (y/n) -> ";
char choice;
cin >> choice;

switch (choice)
{
case 'y':
    // something will happen
    break;
case 'n':
    createAccount();
    break;
default:
    cout << "Please enter option correctly";
    break;
}
}

void createAccount()
{

cout << "Enter your name ";
getline(cin, customerName);
cout << customerName;
}

0 个答案:

没有答案