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.
ais not the same as'a'ais an identifier'a'is a characterYou want to match that if the contents of the char variable
doptis 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
aandAare 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. Thereforedoptwill always be false (almost). But if you want to make the effect that ifdoptis either'a'or'A'then calldbref ()then you need to do the following:or also