用vs版本时候会有很多错误
switch错误
在vs和vc有提示但是可以通过编译
在gcc不能通过编译
#include <stdio.h>
int main(void) {
int c;
scanf("%d", &c);
switch(c) {
case 1:
int x = 0; //错误!
printf("c=1");
break;
case 2:
printf("c=2");
break;
default:
printf("other");
break;
}
return 0;
}
应该改为
#include <stdio.h>
int main(void) {
int c;
scanf("%d", &c);
switch(c) {
case 1:
{
int x = 0; //错误!
printf("c=1");
}
break;
case 2:
printf("c=2");
break;
default:
printf("other");
break;
}
return 0;
}
不安全函数scanf
在vs当中应该用scanf_s
gets也是一样 应该用get_s
cin>>的返回值
#include
#include
#include <Windows.h>
using namespace std;
int main(void) {
string word;
int count = 0;
int length = 0;
cout << "请输入任意多个单词:";
while (1) {
if ((cin >> word) == 0) { //在 vs中不能通过编译
break;
}
count++;
length += word.length();
}
cout << "一共有" << count << "单词" << endl;
cout << "总长度:" << length << endl;
system("pause");
return 0;
}
把 if ((cin >> word) == 0) 修改为:
if ((bool)(std::cin >> word) == 0) {
或者修改为:
if (!(cin >> word)) {
同理getline的返回值 也是一样的