下面是我用 cin 做的 while 循环 有一点问题,当用户输入全名然后按回车。指针将转到下一行。直到用户再次按回车键,然后它提示电子邮件地址,我如何在下面的代码中输入全名后立即提示电子邮件地址
终端外观:
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=="");