我制作了一台“电脑”。我conctructor看起来是这样的:会员功能不会执行其整个代码
PC::PC()
{
cout << "Would you like to turn the pc on? type y for yes." << endl;
a = getchar();
while (a != 'y')
{
cout << "If you dont turn it on, then nothing will happen. Loser." << endl;
a = getchar();
}
}
然后,如果你按y键,你将被发送到下一个步骤是函数PC :: PCON看起来像这样:
void PC::pcOn()
{
for (auto i = 0; i < 3; i++)
{
cout << "----------------------------------------" << endl;
}
cout << "--------- What is your name? -----------" << endl;
changeName();
for (auto i = 0; i < 3; i++)
{
cout << "----------------------------------------" << endl;
}
for (auto i = 0; i < 5; i++)
{
cout << "**" << endl;
Sleep(100);
}
cout << "Welcome " << name << " to the future of computing." << endl << endl;
cout << "This computer program can do a lot of things for you" << endl << "it is a good calculator, try to type \"calculater\"" << endl;
}
但是当我有在构造函数中的while循环来获取y,changeName();不会工作,但如果我删除,changeName函数工作得很好,它需要我的输入就好了。
的changeName()的代码看起来是这样的:
void PC::changeName()
{
string _name;
getline(cin, _name);
name = _name;
}
我已经使用Visual Studio的调试器,看看为什么我不会正确地调用它尝试过,但可惜没有希望。 奇怪的是,如果构造函数中的while循环不存在,该函数可以正常工作。
可能的复制来完成:为什么标准::函数getline()格式化提取后跳过输入?](HTTP: //stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – NathanOliver