"コンピュータ"を作成しました。私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();
}
}
を次にあなたがこのようになり、機能PC :: PCONで次のステップに送信されますY押した場合:私は持っているが
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ループが存在しない場合に関数がうまく動作することです。
可能な複製を使用して行うことができます:[なぜはstdん:: getlineの()は、フォーマットされた抽出後の入力を省略?](HTTP: //stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – NathanOliver