enum in a structure doesn't work passed to a function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tayyab6
    New Member
    • Feb 2012
    • 1

    enum in a structure doesn't work passed to a function?

    I have made a program in C++ which does not accept the value of a an enumeration variable defined within a structure, when I pass the structure into a function.
    It does not even accept the value YES or NO in the main function.

    Help me out please. :)

    Code:
    #include <iostream>
    using namespace std;
    
    struct student
    {
    	char name[20];
    	int age;
    	enum {YES,NO} admit;
    	union 
    	{
    		int FSCMarks;
    		char ALMarks;
    
    	} marks;
    	int FSCcheck;
    	int ALcheck;
    
    };
    
    void computeEligibility(struct student);
    
    
    void main()
    {
    	int check,n;
    	cin>>n;
    
    	cin.ignore();
    
    	struct student *s=new struct student [n];
    
    	for (int i=0; i<n; i++)
    	{
    		cout<<"Enter name: ";
    		cin.getline(s[i].name,100,'\n');
    		cout<<"Enter age: ";
    		cin>>s[i].age;
    		cout<<"Enter 1 if FSc and 2 if A Level: ";
    		cin>>check;
    
    
    		if (check==1)
    		{
    			cout<<"Enter percentage in FSc: ";
    			cin>>s[i].marks.FSCMarks;
    			s[i].FSCcheck=1;
    
    		}
    
    		if (check==2)
    		{
    			cout<<"Enter grade in A Levels: ";
    			cin>>s[i].marks.ALMarks;
    			s[i].ALcheck=1;
    
    		}
    
    		computeEligibility(s[i]);
    	}
    
    }
    
    void computeEligibility(struct student s)
    {
    	if (s.FSCcheck==1)
    	{
    		if(s.age<35 && s.marks.FSCMarks>85)
    			s.admit=YES;		
    		else
    			s.admit=NO;
    	}
    
    	if (s.ALcheck==1)
    	{
    		if(s.age<35 && s.marks.ALMarks=='A')
    			s.admit=YES;
    		else
    			s.admit=NO;
    	}
    
    	if(s.admit==YES)
    		cout<<"Eligible.\n";
    
    	if(s.admit==NO)
    		cout<<"Not Eligible.\n";
    
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    An enum is not a type. What it is a named integer value. Like you name 3 RED and 4 BLUE. Then you use RED and BLUE in your code instead of 3 and 4

    So an enum like this:

    Code:
    enum Color {RED=3,BLUE=4};
    just says that RED can be used instead of 3 and BLUE can be used instead of 4 when using variables typed as enum Color.

    Then in a struct:
    Code:
    struct MyStruct
    {
       enum Color var;
    };
    var is an int with a value of 3 or 4.

    You set the value this way:

    Code:
    struct MyStruct data;
    data.var = RED;   /* same as coding data.var = 3

    Comment

    Working...