菜单中的字母选项在 C 程序中被声明为未声明
我认为我的问题很简单,但我没有看到它。我是 C 编程新手,这是一次尝试,看看我一点一点地吸收了什么。我想我一定没有正确定义我的 char 变量“dopt”。希望你能帮忙。这是代码:
#include <stdio.h>
int dbref();
int aart();
int wgame();
int calc();
int txtoc();
int amin()
{
char dopt;
printf("What should this program have the options of doing?\n");
printf("A) Reference a database?\n");
printf("B) Print ascii art?\n");
printf("C) Make a noun, pronoun, object, verb word game?\n");
printf("D) Being a calculator?\n");
printf("E) creating a text file and save it as a .c file?\n");
printf("F) or should it just terminate?\n");
scanf("%c", &dopt);
if (dopt == a || A)
{ dbref();}
if (dopt== b || B)
{ aart();}
if ( dopt==c || C)
{ wgame();}
if ( dopt==d || D)
{ calc();}
if ( dopt==e || E)
{ txtoc();}
if ( dopt==f || F)
{ return 0;}
return 1;
}
dbref()
{
printf("reference A correct");
return 2;
}
aart()
{
printf("reference B correct");
return 3;
}
wgame()
{
printf("reference C correct");
return 4;
}
calc()
{
printf("reference D correct");
return 5;
}
txtoc()
{
printf("reference E correct");
return 6;
}
作为旁注,函数中的 printf 例程只是为了验证菜单是否正确流动。
I think my problem is something simple, but I'm not seeing it. I'm new to programming in C and this is an effort to see what I've absorbed, bit by bit. I think I must have not properly defined my char variable "dopt". Hope you can help. Here's the code:
#include <stdio.h>
int dbref();
int aart();
int wgame();
int calc();
int txtoc();
int amin()
{
char dopt;
printf("What should this program have the options of doing?\n");
printf("A) Reference a database?\n");
printf("B) Print ascii art?\n");
printf("C) Make a noun, pronoun, object, verb word game?\n");
printf("D) Being a calculator?\n");
printf("E) creating a text file and save it as a .c file?\n");
printf("F) or should it just terminate?\n");
scanf("%c", &dopt);
if (dopt == a || A)
{ dbref();}
if (dopt== b || B)
{ aart();}
if ( dopt==c || C)
{ wgame();}
if ( dopt==d || D)
{ calc();}
if ( dopt==e || E)
{ txtoc();}
if ( dopt==f || F)
{ return 0;}
return 1;
}
dbref()
{
printf("reference A correct");
return 2;
}
aart()
{
printf("reference B correct");
return 3;
}
wgame()
{
printf("reference C correct");
return 4;
}
calc()
{
printf("reference D correct");
return 5;
}
txtoc()
{
printf("reference E correct");
return 6;
}
As a sidenote, the printf routines in the functions are just to verify that the menu is flowing correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像这样的代码:
应该这样写:
因为
a
是变量或函数的名称(不存在),而'a'
是字符文字。或者,您可以考虑使用
switch
块:Code like this:
should be written something like this:
because
a
would be the name of a variable or function (which doesn't exist), and'a'
is a character literal.Alternatively, you could consider a
switch
block:a
与'a'
不同a
是标识符'a'
是您想要的 如果 char 变量dopt
的内容是任何字符,则匹配。所以需要比较字符的ASCII值,可以通过将字符放在单引号内来找到。因此,
a
和A
被视为两个单独的变量(名称),未声明(至少在本地)。因此应该是
这里
'a'
和'A'
是字符常量而不是变量名。但是
'a'
||'A'
始终为 1,因为||
是逻辑 OR 运算符。因此,dopt
将始终为假(几乎)。但是,如果您想实现如果dopt
是'a'
或'A'
的效果,则调用dbref ()< /code> 那么你需要执行以下操作:
或者也
a
is not the same as'a'
a
is an identifier'a'
is a characterYou want to match that if the contents of the char variable
dopt
is any of the characters. So you need to compare the ASCII values of the characters, which can be found by placing the character within a single quotes.Therefore
a
andA
are treated as two separate variables (names), which are not declared (at least locally) .Thus it should be
Here
'a'
and'A'
are character constants and not variable names.BUT
'a'
||'A'
is always 1 because||
is logical OR operator. Thereforedopt
will always be false (almost). But if you want to make the effect that ifdopt
is either'a'
or'A'
then calldbref ()
then you need to do the following:or also
写
dopt == a || A
在 C 中不起作用。您想要的是在 C 中,您必须用
'
将字符文字括起来(否则它们会被解释为变量)。您也不能组合逻辑或,而必须每次都输入dopt ==
。Writing
dopt == a || A
does not work in C. What you want isIn C, you have to enclose character literals with
'
(otherwise they are interpreted as variables). You also can't combine the logical or, but have to type in thedopt ==
each time.您需要在选项检查中引用您的字母,否则它们将被视为变量,并且将无法编译,因为它们不存在。
You want to quote your letters in the option check, otherwise they're treated as variables, and will fail to compile because they don't exist.