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.
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;
}
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();}
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.
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.
精彩评论