Need help with error thrown by compiler

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akhilandrews
    New Member
    • Jan 2015
    • 8

    Need help with error thrown by compiler

    Error message is identifier expected and declaration terminated incorrectly.

    //to define a class Employee
    #include<iostre am.h>
    #include<stdio. h>
    #include<string .h>
    #include<conio. h>

    class cEmp
    {
    private:

    int eid;
    char ename[10];
    char dname[10];
    float sal;

    public:

    cEmp();
    cEmp(int,char*, char*,float);
    void accept();
    void display();
    float getsal();
    int getid();
    };

    cEmp::cEmp()
    {
    eid=0;
    strcpy(ename,"/0");
    strcpy(dname,"/0");
    sal=0;
    }

    cEmp::cEmp(int, char*,char*,flo at)
    {
    int id;
    float s;
    char n[10],d[10];
    eid=id;
    strcpy(ename,n) ;
    strcpy(dname,d) ;
    sal=s;

    }
    cEmp::void accept() //Error in this line<<<<<<<<<<< <<<<
    {
    cout<<"Enter empoyee ID:";
    cin>>eid;
    cout<<"Enter employee name:";
    gets(ename);
    cout<<"Enter department name";
    gets(dname);
    cout<<"Enter employee salary:";
    cin>>sal;
    }

    cEmp::void display()
    {
    cout<<"\nEmploy ee ID:"<<eid;
    cout<<"\nEmploy ee name:"<<ename;
    cout<<"\nDepart ment name"<<dname;
    cout<<"\nEmploy ee salary"<<sal;
    }

    cEmp::void getid()
    {
    return eid;
    }

    cEmp::void getsal()
    {
    return sal;
    }

    void main()
    {
    clrscr();
    do
    {
    cout<<"\t\tMenu ";
    cout<<"\n\n\t1. \tDisplay all the employees' info.";
    cout<<"\n\n\t2. \tDisplay specific employes' info.";
    cout<<"\n\n\t3. \tDisplay employee with max salary.";
    cout<<"\n\n\tEn ter choice:";
    int n,i,a;
    cin>>a;
    switch(a)
    {
    case 1:
    for(i=0;i<n;i++ )
    c[i].display();
    break;
    case 2:
    cout<<"Enter ID:";
    int x;
    cin>>x;
    for(i=0;i<n;i++ )
    if(c[i].getid()==x)
    c[i].display();
    break;
    case 3:
    float max=0;
    for(i=0;i<n;i++ )
    if(c[i].getsal()>max)
    max=c[i].getsal();
    for(i=0;i<n;i++ )
    if(c[i].getsal()==max)
    c[i].display();
    break;
    }
    cout<<"\nDo you want to continue?(y/n):";
    char y;
    cin>>y;

    }
    while(y=='y');
    {
    getch();
    }
    return();
    }
  • akhilandrews
    New Member
    • Jan 2015
    • 8

    #2
    I can't find any reason for this error as all the braces are in the appropriate positions. please help thanks in advance.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      The name of the function is cEmp::accept().

      So this is correct:

      Code:
      void cEmp::accept()
      {
      etc...
      and not:
      Code:
       cEmp::void accept()
      {
      etc...
      This error occurs in several places. Also, in some places the return value of the function in the class definition does not match the return value in the function definition.

      Hopefully, this is enough to get you unstuck.

      Comment

      • akhilandrews
        New Member
        • Jan 2015
        • 8

        #4
        When I remove void from the function I get a host of errors by the compiler saying that the
        functions are not member functions of the class cEmp
        errors- cEmp::accept()
        1. accept() is not a member of cEmp
        2. display(), "
        3. getid(), "
        4. getsal(), "
        5. undefined symbol c at line 91
        6. undefined symbol y at line 116
        7. expression syntax at line 120

        Comment

        • akhilandrews
          New Member
          • Jan 2015
          • 8

          #5
          also a warning stating that max is assigned to a value that is never used.

          Comment

          • akhilandrews
            New Member
            • Jan 2015
            • 8

            #6
            Code:
            #include<iostream.h>
            #include<stdio.h>
            #include<string.h>
            #include<conio.h>
            
            class cEmp
            {
                 private:
            
                 int eid;
                 char ename[10];
                 char dname[10];
                 float sal;
            
                 public:
            
                 cEmp();
                 cEmp(int,char*,char*,float);
                 void accept();
                 void display();
                 float getsal();
                 int getid();
            };
            
            cEmp::cEmp()
            {
                 eid=0;
                 strcpy(ename,"/0");
                 strcpy(dname,"/0");
                 sal=0;
            }
            
            cEmp::cEmp(int,char*,char*,float)
            {
                 int id;
                 float s;
                 char n[10],d[10];
                 eid=id;
                 strcpy(ename,n);
                 strcpy(dname,d);
                 sal=s;
            
            }
            cEmp::accept()
            {
                 cout<<"Enter empoyee ID:";
                 cin>>eid;
                 cout<<"Enter employee name:";
                 gets(ename);
                 cout<<"Enter department name";
                 gets(dname);
                 cout<<"Enter employee salary:";
                 cin>>sal;
            }
            
            cEmp::display()
            {
                 cout<<"\nEmployee ID:"<<eid;
                 cout<<"\nEmployee name:"<<ename;
                 cout<<"\nDepartment name"<<dname;
                 cout<<"\nEmployee salary"<<sal;
            }
            
            cEmp::getid()
            {
                 return eid;
            }
            
            cEmp::getsal()
            {
                 return sal;
            }
            
            void main()
            {
                 clrscr();
            do
            {
                 cout<<"\t\tMenu";
                 cout<<"\n\n\t1.\tDisplay all the employees' info.";
                 cout<<"\n\n\t2.\tDisplay specific employes' info.";
                 cout<<"\n\n\t3.\tDisplay employee with max salary.";
                 cout<<"\n\n\tEnter choice:";
                 int n,i,a;
                 cin>>a;
                 switch(a)
                 {
            	  case 1:
            		 for(i=0;i<n;i++)
            		 c[i].display();
            		 break;
            	  case 2:
            		 cout<<"Enter ID:";
            		 int x;
            		 cin>>x;
            		 for(i=0;i<n;i++)
            		      if(c[i].getid()==x)
            			   c[i].display();
            		 break;
            	  case 3:
            		float max=0;
            		for(i=0;i<n;i++)
            		     if(c[i].getsal()>max)
            			  max=c[i].getsal();
            		for(i=0;i<n;i++)
            		     if(c[i].getsal()==max)
            		     c[i].display();
            		break;
                 }
                 cout<<"\nDo you want to continue?(y/n):";
                 char y;
                 cin>>y;
            
                 }
                 while(y=='y');
                 {
                 getch();
                 }
                 return();
            }
            here's the code wrapped, I know the indentation is a little off. Thanks in advance, really appreciate it!

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              The return types of several of the member functions have been removed. They need to be put back and they need to match the function prototype in the class declaration. Remember, any time a function returns a value, the function return type cannot be void.

              Where is this c array? :
              Code:
              switch (a)
              		{
              		case 1:
              			for (i = 0; i<n; i++)
              				[B]c[i].[/B]display();
              			break;
              		case 2:
              			cout << "Enter ID:";
              			int x;
              			cin >> x;
              			for (i = 0; i<n; i++)
              			if ([B]c[i[/B]].getid() == x)
              				[B]c[i[/B]].display();
              			break;
              The char y is inside the braces and the while is outside the braces:

              Code:
              		char y;
              		cin >> y;
              
              	} while ([B]y[/B] == 'y'); <---y has been destroyed.
              You cannot use variables inside braces from outside the braces. Instead you define variables outside the braces and use them from inside nested braces. Since the nested braces are still inside the outer braces, the variables can be used because they are still inside the outer braces.

              Comment

              • akhilandrews
                New Member
                • Jan 2015
                • 8

                #8
                Aah got it, thanks again

                Comment

                Working...