【发布时间】:2016-02-01 15:58:10
【问题描述】:
我编译了以下代码,但我遇到了一个严重的问题。 当我在 Visual Studio 2015 中编译此代码时,它运行良好。 但是,当我在 Dev C++ 中编译此代码时,我认为它不会打印“是”作为答案。
例如,当我输入类似的字词时,
- 哈哈
- 我看到的是汽车还是猫?
- abcdefghiihgfedcba
这些输入必须返回 yes,但在 dev c++ 中返回 no。
为什么会出现这个问题?
#include <iostream>
#include <string>
using namespace std;
bool is_palindrome(char input[], int numOfSlots);
int main(void) {
char text[256], fixed[256];
cin.getline(text, sizeof(text), '\n');
for (int i = 0; i < sizeof(text); i++) {
text[i] = toupper(text[i]);
}
int j = 0;
for (int i = 0; i < sizeof(text); i++) {
if ((text[i] >= '0' && text[i] <= '9') || (text[i] >= 'A' && text[i] <= 'Z')) {
fixed[j] = text[i];
j++;
}
}
fixed[j] = '\0';
string s_fixed = fixed;
if (is_palindrome(fixed, s_fixed.length()) == true) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
bool is_palindrome(char input[], int numOfSlots) {
int i = 0;
while (i < numOfSlots / 2)
{
if (input[i] != input[(numOfSlots - 1) - i])
return false;
i++;
}
return true;
}
【问题讨论】:
-
您可以简单地添加一些诊断输出以查看正在发生的比较以及他们的答案是什么。这比发布您的程序并希望其他人愿意为您执行此操作要高效得多。
-
听起来您可能需要学习如何使用调试器来单步调试您的代码。使用好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏差在哪里。如果您要进行任何编程,这是必不可少的工具。进一步阅读:How to debug small programs
-
如果你被允许使用
std::string,你为什么在那里使用char[]?
标签: c++ compiler-errors