我是 C++ 新手,因为我的第一门计算机语言是 Python,所以我真的不明白我在这里做错了什么。
此代码的目的是找出长度为 8 的字符串中包含多少特定字母,但我不断收到以下错误:
<块引用>ISO C++ 禁止指针和整数之间的比较 [-fpermissive]
#include <iostream>
using namespace std;
int main() {
int cnt = 0;
string temp;
cin >> temp; // the input string will be of length 8
for (int i = 0; i < 8; i++) {
if (temp[i] == "F") {
cnt += 1;
};
};
cout << cnt << endl;
return 0;
}
当我在代码中打印出 temp[i]
时,我可以确认 temp[i]
被打印为单个字符,我相信可以将其与另一个字符进行比较,在本例中为字符 {{ 1}}。
我一直试图找出发生这种情况的原因,但最终还是来到这里寻求帮助。
答案 0 :(得分:1)
if (temp[i] == "F") {
应该是 if (temp[i] == 'F') {
。
'F'
是一个字符 (char
)"F"
是一个 C 字符串(const char[2]
衰减为 const char*
)。