菜单中的字母选项在 C 程序中被声明为未声明

发布于 2024-12-01 16:28:53 字数 1146 浏览 1 评论 0原文

我认为我的问题很简单,但我没有看到它。我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

红玫瑰 2024-12-08 16:28:53

像这样的代码:

if (dopt == a || A)

应该这样写:

if (dopt == 'a' || dopt == 'A')

因为 a 是变量或函数的名称(不存在),而 'a'字符文字

或者,您可以考虑使用 switch 块:

switch (dopt)
{
case 'a':
case 'A':
    dbref();
    break;
case 'b':
case 'B':
    aart();
    break;
/* etc. */
default:
    fprintf(stderr, "Unrecognised option!\n");
    return 1;
}

Code like this:

if (dopt == a || A)

should be written something like this:

if (dopt == 'a' || dopt == 'A')

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:

switch (dopt)
{
case 'a':
case 'A':
    dbref();
    break;
case 'b':
case 'B':
    aart();
    break;
/* etc. */
default:
    fprintf(stderr, "Unrecognised option!\n");
    return 1;
}
兰花执着 2024-12-08 16:28:53

a'a' 不同

  • a 是标识符
  • 字符

'a' 是您想要的 如果 char 变量 dopt 的内容是任何字符,则匹配。所以需要比较字符的ASCII值,可以通过将字符放在单引号内来找到。

因此,

if (dopt == a || A)
   { dbref();}

aA 被视为两个单独的变量(名称),未声明(至少在本地)。

因此应该是

if (dopt == 'a' || 'A')
    { dbref();}

这里 'a''A' 是字符常量而不是变量名。

但是 'a' || 'A' 始终为 1,因为 || 是逻辑 OR 运算符。因此,dopt 将始终为假(几乎)。但是,如果您想实现如果 dopt'a''A' 的效果,则调用 dbref ()< /code> 那么你需要执行以下操作:

if ((dopt == 'a') || (dopt == 'A'))
    { dbref();}

或者也

if (toupper (dopt) == 'A') // similar with tolower ()
    { dbref();}

a is not the same as 'a'

  • a is an identifier
  • 'a' is a character

You 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

if (dopt == a || A)
   { dbref();}

a and A are treated as two separate variables (names), which are not declared (at least locally) .

Thus it should be

if (dopt == 'a' || 'A')
    { dbref();}

Here 'a' and 'A' are character constants and not variable names.

BUT 'a' || 'A' is always 1 because || is logical OR operator. Therefore dopt will always be false (almost). But if you want to make the effect that if dopt is either 'a' or 'A' then call dbref () then you need to do the following:

if ((dopt == 'a') || (dopt == 'A'))
    { dbref();}

or also

if (toupper (dopt) == 'A') // similar with tolower ()
    { dbref();}
把时间冻结 2024-12-08 16:28:53

dopt == a || A 在 C 中不起作用。您想要的是

dopt == 'a' || dopt == 'A'

在 C 中,您必须用 ' 将字符文字括起来(否则它们会被解释为变量)。您也不能组合逻辑或,而必须每次都输入 dopt ==

Writing dopt == a || A does not work in C. What you want is

dopt == 'a' || dopt == 'A'

In 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 the dopt == each time.

杀お生予夺 2024-12-08 16:28:53

您需要在选项检查中引用您的字母,否则它们将被视为变量,并且将无法编译,因为它们不存在。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文