我的变量不起作用,不断收到 'expression result unused' 错误
My variables aren't working, keep recieving 'expression result unused' error
我目前正在做一个项目,需要我制作一个由 # 标记组成的右对齐三角形。我收到来自我的变量 'space' 的错误。谁能告诉我为什么会收到此错误消息?
#include <cs50.h>
#include <stdio.h>
int get_height(void);
int lineno;
int column_fill;
int main(void)
{
int height = get_height();
int space = height;
for (lineno = 1; lineno <= height; lineno++ )
{
for (space; space > 0; space--)
{
printf(".");
}
for (column_fill = 1; column_fill <= lineno; column_fill++)
{
printf("#");
}
printf("\n");
}
}
int get_height(void)
{
int height;
do
{
height = get_int("Height: ");
}while(height < 0 || height > 9);
return height;
}
我收到的错误是:
mario.c:13:14: 错误:表达式结果未使用 [-Werror,-Wunused-value] for (space; space > 0; space--) ^~ ~~~ 产生 1 个错误。 : 目标配方 'mario' 失败 make: *** [mario] 错误 1
在 for (space; space > 0; space--)
中,第一个 space
没有做任何事情,编译器会警告您。
通常,for
语句中的第一项是执行某些操作的表达式,例如赋值 space = height
,或者是要在循环中使用的一个或多个对象的声明,例如 int space = height
。将您的代码更改为其中之一,编译器将停止抱怨。
您可能应该使用后者并删除之前单独声明的 space
,因为:
- 每次循环开始时,您都需要重置
space
。
- 最好尽可能保持本地声明,以避免出错的机会。
我目前正在做一个项目,需要我制作一个由 # 标记组成的右对齐三角形。我收到来自我的变量 'space' 的错误。谁能告诉我为什么会收到此错误消息?
#include <cs50.h>
#include <stdio.h>
int get_height(void);
int lineno;
int column_fill;
int main(void)
{
int height = get_height();
int space = height;
for (lineno = 1; lineno <= height; lineno++ )
{
for (space; space > 0; space--)
{
printf(".");
}
for (column_fill = 1; column_fill <= lineno; column_fill++)
{
printf("#");
}
printf("\n");
}
}
int get_height(void)
{
int height;
do
{
height = get_int("Height: ");
}while(height < 0 || height > 9);
return height;
}
我收到的错误是: mario.c:13:14: 错误:表达式结果未使用 [-Werror,-Wunused-value] for (space; space > 0; space--) ^~ ~~~ 产生 1 个错误。 : 目标配方 'mario' 失败 make: *** [mario] 错误 1
在 for (space; space > 0; space--)
中,第一个 space
没有做任何事情,编译器会警告您。
通常,for
语句中的第一项是执行某些操作的表达式,例如赋值 space = height
,或者是要在循环中使用的一个或多个对象的声明,例如 int space = height
。将您的代码更改为其中之一,编译器将停止抱怨。
您可能应该使用后者并删除之前单独声明的 space
,因为:
- 每次循环开始时,您都需要重置
space
。 - 最好尽可能保持本地声明,以避免出错的机会。