c - 即使我在顶部写了 char 答案,为什么我仍然第一次使用此函数错误?
问题描述
我尝试了我发现的每一个解决方案来解决这个问题,但没有帮助我。我也试过不做,while (true)
但也做不到。我的错误:
error: ‘answer’ undeclared (first use in this function)
30 | }while (answer == 'n' || answer == 'N');
| ^~~~~~
.
#include <stdio.h>
int main ()
{
do {
double length;
char unit;
char answer;
printf("Enter the length: ");
scanf("%lf%c", &length, &unit);
if (unit == 'i' || unit == 'I')
{
printf("%1.2lf i = %lf c \n", length, 2.54*length);
}
else if (unit == 'm' || unit == 'M')
{
printf("%1.2lf i = %lf c \n", 39.3701*length, 100*length);
}
else if (unit == 'c' || unit == 'C')
{
printf("%1.2lf i = %lf c \n", length/2.54, length);
}
else
{
printf("0 i = 0 c");
}
printf("Do you want to start over? y/n \n");
scanf("%c", & answer);
answer = getchar();
}while (answer == 'n' || answer == 'N');
return 1;
}
解决方案
char answer;
位于do { ... } while();
循环内,范围仅限于大括号内。因此,您无法answer
在while()
有条件的情况下访问。它在大括号之外。
推荐阅读
- python - 为什么我只能得到谷歌地图的第 6 个结果(刮)?
- eslint - WebStorm 不会自动修复 ESLint 规则
- php - 在 w3scool 中找到工作的 php 脚本,但在我的网站上无法正常工作
- docker - 如何为 docker-compose 容器分配主机名以进行开发
- go - VS Code:设置为 not 时转到符号扫描 goroot 和导入
- python - 水平连接数据帧
- java - Angular 和 Spring 启动:请求的资源上不存在“Access-Control-Allow-Origin”标头。如何通过角度和弹簧靴解决它
- c++ - 程序展示已售出超过 500 个茶包的茶叶品牌 C++
- typescript - 打字稿嵌套泛型
- java - Spring WebClient 中带有 json 模式 URL 参数的 ContentType 标头