严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C2446 “==”: 没有从“const char *”到“int”的转换 test1 d:\vs-c+±code\test1\test1\test.cpp 11
错误 C2446 “!=”: 没有从“const char *”到“int”的转换 test1 d:\vs-c+±code\test1\test1\test.cpp 10
解决方法:
将“.”改为单引号‘.’
错误代码:
#include <iostream>
using namespace std;
int main() {
using std::cin;
using std::cout;
char ch;
int spaces = 0;
int total = 0;
cin.get(ch);
while (ch != ".") {
if (ch == " ")
++spaces;
++total;
cin.get(ch);
}
cout << spaces << " spaces, " << total;
cout << " characters total in sentence\n";
return 0;
}
正确代码:
#include <iostream>
using namespace std;
int main() {
using std::cin;
using std::cout;
char ch;
int spaces = 0;
int total = 0;
cin.get(ch);
while (ch != '.') {
if (ch == ' ')
++spaces;
++total;
cin.get(ch);
}
cout << spaces << " spaces, " << total;
cout << " characters total in sentence\n";
return 0;
}
输出结果:
The balloonist was an airhead
with lofty goals.
6 spaces, 46 characters total in sentence