I am getting this error and dont know what to do :
error: request for member 'Number' in something not a structure or union 22 | if(answer==user.choice.Number)
The program chould check if the user input is equal to one of the enum elements and then print the number or the string in the union
#include <stdio.h>
#include <string.h>
typedef enum{
Number = 1,
String = 2,
}eenum;
union data{
int x;
char str[10];
eenum choice;
};
void display(union data user)
{
int answer;
puts("Enter1 or 2:");
scanf("%d",&answer);
if(answer==user.choice.Number)
{
user.x = 5;
printf("%d\n",user.x);
}
else{
strcpy(user.str,"hello");
printf("%s\n",user.str);
}
}
int main() {
union data user;
display(user);
return 0;
}
CodePudding user response:
Enums treated as global identifiers. You should access your enum
like that:
enum input_type {
NUMBER = 1,
STRING = 2
}
...
if (answer == NUMBER) { ...
And for improve your understandings about enum, you should read this.