我编写了这段代码,但它没有给出操作符,也没有匹配循环行中的操作符错误。
这段代码有什么问题?
确切的错误:
第15行:没有operator++(int)' declared for postfix
++',尝试使用前缀运算符,15与'++ day'中的'operator ++'不匹配
代码:
enum days { Mon=1,Tue,Wed,Thu,Fri,Sat,Sun};
int main()
{
enum days day;
const char *dayName[]= {"Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday","Sunday"};
for(day=Mon;day<=Sun;day++);
printf("%2d %11s\n",day,dayName[day]);
getch();
return 0;
}
答案 0 :(得分:3)
这是一个完整的陈述
for(day=Mon;day<=Sun;day++);
// ^ <== complete statement
// anything after this is not part of the loop
答案 1 :(得分:1)
此代码适合我,并在整个工作日打印。
在这里试试http://www.compileonline.com/compile_c_online.php
编辑:解释
首先从0开始一个标签,所以我将枚举更改为0(默认值)
然后我重写条件以打印所有值,因为在你的代码中你从1到8然后走到标签之外(日必须从0到6)。
我添加括号以正确执行循环
#include<stdio.h>
#include<stdlib.h>
enum days { Mon=0, Tue, Wed, Thu, Fri, Sat, Sun}; //start to 0
int main()
{
enum days day;
const char *dayName[] = {
"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"
};
for(day = Mon; day <= Sun; day++){// rewrite condition and add bracket
printf("%2d %11s\n", day+1, dayName[day]);
}
return 0;
}