#include <string>
#include <algorithm>
#include <iostream>
#include <conio.h>
#include <cctype>
using namespace std;
int main()
{
{
system("cls");
cout << "\nEnter a palindrome." << endl;
std::string str;
getline(std::cin, str);
if( equal(str.begin(), str.end(), str.rbegin()) )
std::cout << "is a palindrome!\n";
else
std::cout << "is not a palindrome!\n";
cout << "\nGo again (Y/N)?" << endl;
} while (toupper(_getch()) !='N');
return 0;
}
如何调整此代码以使用&#34; \ n再次使用Y(N / N)?&#34;?
以及如何在同一行设置答案,如&#34; DND是一个回文。&#34; 我目前的答案是
&#34; DND 是一个回文。&#34;
谢谢
答案 0 :(得分:1)
int main()
{
{
应该是这样的:
int main()
{
do { // <==== see added keyword?
如上所述,您必须在一组花括号中运行嵌套代码块。然后进入没有身体的while循环。即它做到了这一点:
int main()
{
// this just started a nested scope block
{
system("cls");
cout << "\nEnter a palindrome." << endl;
std::string str;
getline(std::cin, str);
if( equal(str.begin(), str.end(), str.rbegin()) )
std::cout << "is a palindrome!\n";
else
std::cout << "is not a palindrome!\n";
cout << "\nGo again (Y/N)?" << endl;
}
// and now the nested scope block is closed
// now we enter a completely unrelated while-loop with no body.
while (toupper(_getch()) !='N');
return 0;
}
并打印您提到的声明作为输出添加
std::cout<<endl<<str<<" ";
在getline
陈述之后立即。