请给我建议C++回文循环
Please give me advise for C++ Palindrome loop
#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;
}
如何调整此代码以工作"Go Again(是/否(?"?
以及如何在同一行上设置答案,例如" DND 是一个回文。我目前的答案将是
"免打扰是一个回文。
谢谢
你忘了什么。这:
int main()
{
{
应该是这样的:
int main()
{
do { // <==== see added keyword?
正如所写的那样,您的代码必不可少,曾经在一组大括号中运行过嵌套代码块。然后进入了一个没有身体的循环。即它做到了:
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
声明。
相关文章:
- C++:正在检查LinkedList中的回文-递归方法-错误
- Usaco第1.6节主要回文
- 最大的回文产品 - 程序未运行,编写解决方案但无法理解问题
- 将 S1 转换为回文,并将 S2 作为其子字符串
- 数回文词
- 最长的回文子串(C++帮助)
- 有人可以详细解释这个回文代码是如何工作的吗?
- 回文递归版本
- 如何检查C ++ STL列表是否为回文?
- 回文数在 1 到 10000 之间
- C++ - 检查结构数据类型中的单词是否为回文
- 是回文作业练习
- 回文递归不停止
- 整数数组中最长的回文
- 回文测试
- 最大的回文产品(Project Euler)——C++
- C++ 回文程序总是给出 0(假)作为输出问题;我的代码哪里有问题?
- 递归回文问题的时间复杂度,C++
- 程序以查找给定字符串中回文的子字符串的数量
- 递归回文检查,不使用向量、大小或其他参数