我正在尝试制作一个使用 MACROS 和数组的程序。它要求输入 1-10 的任何评级并创建图表。 评级 react 10 25 9 50 8 100 …… -1239 0 等。 我遇到了大量的语法错误,无法弄清楚如何让这段代码完全运行。波纹管是我想出的。有谁知道我可以使用我开始的这个通用代码想法修复什么以使输出正确输出。谢谢!
#include <stdlib.h>
#define MAX_RESPONDANTS 20;
#define MIN_RESPONSE_VALUE 0;
#define MAX_RESPONSE_VALUE 10;
#define TERMINATOR -1;
int main(void)
{
const int RESPONSE_VALUES = MAX_RESPONSE_VALUE - MIN_RESPONSE_VALUE
int value;
int loopCount;
// enter rating 1-10
printf ("Please enter a value between %d and %d. Use %d to end input: \n ", MIN_RESPONSE_VALUE, MAX_RESPONSE_VALUE, TERMINATOR);
//printf("Please enter a value between %d and %d. Use %d to end input: ", "%d", MIN_RESPONSE_VALUE);
int ratingCounters[RESPONSE_VALUES] = { 0 };
// loopCount, responses[MAX_RESPONDANTS]; value;
for (loopCount = 0; loopCount < MAX_RESPONDANTS)
{
scanf("%d", &value);
ratingCounters[loopCount] = value;
}
if (value > MIN_RESPONSE_VALUE && value <= MAX_RESPONSE_VALUE)
{
printf("response is","%d" ,RESPONSE_VALUES);
}
else if (value = TERMINATOR)
{
printf("Outside valid range. Please input another number.\n");
}
printf("\n\nRating Number of Responses\n");
printf("------ --------------------\n");
}
return 0;
}
最佳答案
宏定义不是语句,通常末尾没有分号。
在进行任何其他语法检查之前,C 中的宏会逐字扩展。所以当你定义
#define MAX_RESPONDANTS 20;
#define MIN_RESPONSE_VALUE 0;
#define MAX_RESPONSE_VALUE 10;
#define TERMINATOR -1;
然后写
printf ("...", MIN_RESPONSE_VALUE, MAX_RESPONSE_VALUE, TERMINATOR);
它被扩展为
printf ("...", 0;, 10;, -1;);
我想你会同意这是无效语法。
所以你应该从你的宏定义中删除分号:
#define MAX_RESPONDANTS 20
(顺便说一句,正确的拼写是“respondents”。)
我猜你可能已经在线上遇到了类似的错误
const int RESPONSE_VALUES = MAX_RESPONSE_VALUE - MIN_RESPONSE_VALUE
然后您通过删除末尾的分号来“修复”它。但这被扩展为
const int RESPONSE_VALUES = 10; - 0;
MIN_RESPONSE_VALUE
实际上并未用于计算分配给 RESPONSE_VALUES
的值;它出现在它自己的声明中,上面写着“计算 0 的负数,然后不对它做任何事情。”
奖励错误:
ratingCounters
的大小为RESPONSE_VALUES
,在本例中为 10,但您在其中存储了MAX_RESPONDANTS
值,即20.在行
printf("response is","%d",RESPONSE_VALUES);
中,前两个参数应该是一个字符串。就目前而言,该行将只打印response is
。在
else if (value = TERMINATOR)
行中,您几乎肯定是指==
。
关于c - 我如何使用数组和 MACRO 创建一个调查,告诉 C 中的评级和响应数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35382739/