为什么这个循环不起作用?
Why doesn't this loop work?
为什么这个循环不起作用?当答案不是 y 或 n 时,它会产生分段错误,但我认为我已经解决了这个问题,但它没有用。我该如何解决这个问题才能停止分段错误?我很困惑为什么它不起作用。
int main()
{
string yes = "y";
string uyes = "Y";
string no = "n";
string uno = "N";
string answer = " ";
do
{
system("CLS");
cout << "Welcome to the Red Fern Shipping Company where we ship your packages your way!\n";
cout << "Do you want to ship a parcel? Y or N?\n";
cin >> answer;
do
{
if (answer == no || answer == uno)
{
cout << "Thank you! Come again!\n";
system("PAUSE");
return 0;
}
else if (answer == yes || answer == uyes)
{
break;
}
else
{
cin.clear();
cin.sync();
cout << "That answer is invalid and cannot work. Please enter a 'y' for yes or 'n' for no.\n";
}
} while (answer != yes && answer != uyes && answer != no && answer != uno);
} while (answer == yes || answer == uyes);
system("PAUSE");
return 0;
}
'answer' 的值将永远不等于 yes/no 如果它是一次,因为 'cin >> answer' 没有在内部 do-while 中声明。
问题是内循环是无限的
do
{
if (answer == no || answer == uno)
{
cout << "Thank you! Come again!\n";
system("PAUSE");
return 0;
}
else if (answer == yes || answer == uyes)
{
break;
}
else
{
cin.clear();
cin.sync();
cout << "That answer is invalid and cannot work. Please enter a 'y' for yes or 'n' for no.\n";
}
} while (answer != yes && answer != uyes && answer != no && answer != uno);
循环变量answer 内没有得到更新。您应该插入语句
std::cin >> answer;
在此内部循环中。
除了一个语句外,我会将所有 if 语句放在内部循环之外。例如
bool valid_answer;
do
{
cin >> answer;
valid_answer = answer == yes || answer == uyes ||
answer == no || answer == uno;
if ( !valid_answer )
{
cin.clear();
cin.sync();
cout << "That answer is invalid and cannot work. Please enter a 'y' for yes or 'n' for no.\n";
}
} while ( !valid_answer );
if (answer == no || answer == uno)
{
cout << "Thank you! Come again!\n";
system("PAUSE");
return 0;
}
为什么这个循环不起作用?当答案不是 y 或 n 时,它会产生分段错误,但我认为我已经解决了这个问题,但它没有用。我该如何解决这个问题才能停止分段错误?我很困惑为什么它不起作用。
int main()
{
string yes = "y";
string uyes = "Y";
string no = "n";
string uno = "N";
string answer = " ";
do
{
system("CLS");
cout << "Welcome to the Red Fern Shipping Company where we ship your packages your way!\n";
cout << "Do you want to ship a parcel? Y or N?\n";
cin >> answer;
do
{
if (answer == no || answer == uno)
{
cout << "Thank you! Come again!\n";
system("PAUSE");
return 0;
}
else if (answer == yes || answer == uyes)
{
break;
}
else
{
cin.clear();
cin.sync();
cout << "That answer is invalid and cannot work. Please enter a 'y' for yes or 'n' for no.\n";
}
} while (answer != yes && answer != uyes && answer != no && answer != uno);
} while (answer == yes || answer == uyes);
system("PAUSE");
return 0;
}
'answer' 的值将永远不等于 yes/no 如果它是一次,因为 'cin >> answer' 没有在内部 do-while 中声明。
问题是内循环是无限的
do
{
if (answer == no || answer == uno)
{
cout << "Thank you! Come again!\n";
system("PAUSE");
return 0;
}
else if (answer == yes || answer == uyes)
{
break;
}
else
{
cin.clear();
cin.sync();
cout << "That answer is invalid and cannot work. Please enter a 'y' for yes or 'n' for no.\n";
}
} while (answer != yes && answer != uyes && answer != no && answer != uno);
循环变量answer 内没有得到更新。您应该插入语句
std::cin >> answer;
在此内部循环中。
除了一个语句外,我会将所有 if 语句放在内部循环之外。例如
bool valid_answer;
do
{
cin >> answer;
valid_answer = answer == yes || answer == uyes ||
answer == no || answer == uno;
if ( !valid_answer )
{
cin.clear();
cin.sync();
cout << "That answer is invalid and cannot work. Please enter a 'y' for yes or 'n' for no.\n";
}
} while ( !valid_answer );
if (answer == no || answer == uno)
{
cout << "Thank you! Come again!\n";
system("PAUSE");
return 0;
}