以下は、cin を使用した私の do while ループです。ユーザーが Fullname を入力して Enter キーを押すと、少し問題が発生します。ポインタは次の行に移動します。ユーザーがもう一度Enterキーを押すまで、電子メールアドレスが表示されます。以下のコードにフルネームを入力した直後に電子メールアドレスを表示するにはどうすればよいですか
端末の外観:
Full Name: John
Email Address: some_email@gmail.com
私のtest.cppコード:
emailAddress= "";
fullname = "";
counter = 0;
if(department_selection!="")
{
while(fullname=="")
{
if(counter>0)
{
//so it wont print twice
cout << "Full Name: ";
}
getline(cin,fullname);
cin.clear();
cin.ignore();
counter++;
}
counter = 0;
while(emailAddress=="")
{
if(counter>0)
{
//so it wont print twice
cout << "Email Address: ";
}
getline(cin,emailAddress);
cin.clear();
cin.ignore();
counter++;
}
}// if department selection not blank
まだ同じ問題。一度タブで入力する必要があります。その後、電子メール アドレスの入力を求められます。
最新の更新: 管理して修正します。コードを変更すると、次のバージョンになります。
do
{
if(counter==0)
{
//so it wont print twice
cout << "Full Name: ";
}
if(counter>1)
{
//so it wont print twice
cout << "Full Name: ";
}
getline(cin,fullname);
counter++;
} while (fullname=="");
counter = 0;
do
{
if(counter==0)
{
//so it wont print twice
cout << "Email Address: ";
}
if(counter>1)
{
cout << "Email Address: ";
}
getline(cin,emailAddress);
counter++;
} while (emailAddress=="");