[英]How do I fix error "use of undeclared identifier n" in C?
I'm sure there are probably tons of syntax/other errors, but I'm trying to figure out the two that it's picking up on.我确定可能有大量的语法/其他错误,但我正在尝试找出它正在接受的两个错误。 I'm very new to this so I don't really know how to fix undeclared identifiers.我对此很陌生,所以我真的不知道如何修复未声明的标识符。 Note that #include <cs50.h>
is just CS50's library.请注意, #include <cs50.h>
只是 CS50 的库。
#include <cs50.h>
#include <stdio.h>
int main (void)
{
int add, fee, disc;
printf("For rate with tax and Security Deposit, type y. For 10 percent off, type n:");
string name = GetString();
if (name == y)
{
printf("PreTax Amount: ");
scanf("%d", &fee);
printf("Okay. I will add the 10 percent tax to %d.\n ", fee);
add = (1.1 * fee);
printf("Plus Tax Amount = %d\n", add);
printf("Security Deposit = 1000 dollars\n");
printf("Total = (%d + 1000)", add);
}
else if (name == n)
{
printf("PreTax Amount: ");
scanf("%d%d", &fee, &disc);
printf("Okay. I will minus the 10 percent discount to %d and then add tax.\n ", fee);
add = (0.9 * fee);
disc = (add * 1.1);
printf("Minus Discount Amount plus tax = %d\n", disc);
printf("Security Deposit = 1000 dollars\n");
printf("Total = (%d + 1000)", disc);
}
return 0;
}
errors:错误:
ContractualHelper.c:10:17: error: use of undeclared identifier 'y'
if (name == y)
^
ContractualHelper.c:22:22: error: use of undeclared identifier 'n'
else if (name == n)
^
2 errors generated.
y
and n
are undeclared because they do not have definitions. y
和n
未声明,因为它们没有定义。
int y;
is a declaration, which would get rid of that error.是一个声明,它将消除该错误。
However, your code但是,您的代码
if (name == y)
is comparing a variable name
to a variable y
, and I think what you want to do is see if name
contains the string y
.正在将变量name
与变量y
进行比较,我认为您想要做的是查看name
包含字符串y
。
How to do that comparison is another issue.如何进行比较是另一个问题。
Since you want just to compare with either 'Y' or 'N' I suggest you use char
instead of string
.由于您只想与 'Y' 或 'N' 进行比较,我建议您使用char
而不是string
。 so : char name;
所以: char name;
scanf("%c", &name);
than when comparing use :比比较使用时:
if (name == 'Y') ...
ADD添加
you also should consider wrong inputs from user so add another else
like :你也应该考虑从用户输错,从而再添else
,如:
#include <stdio.h>
#include <string.h>
main()
{
char name;
scanf("%c", &name);
if (name == 'y')
printf("Yes");
else if (name == 'n')
printf("No");
else
printf("wrong input");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.