How do I solve this error???

I have been trying to compile this for a while but it keeps saying:-

[Error]C:\User\HP\Documents\C-Free\Temp\Untitles3.cpp:54: error: no match for 'operator[]' in 'emp[i]'
[Error]C:\User\HP\Documents\C-Free\Temp\Untitles3.cpp:55: error: no match for 'operator[]' in 'emp[i]'
[Error]C:\User\HP\Documents\C-Free\Temp\Untitles3.cpp:56: error: no match for 'operator[]' in 'emp[i]'

Plese help..
i have practical test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
#include<stdio.h>
struct employee
{
 char nam[12], dsg[12];
 float bp, da, hra, gp, pf, it, np;
};
employee emp;
int n;
void input()
{
 cout<<"Enter no. of employee -";
 cin>>n;
 for(int i=0;i<n;i++)
 {
  cout<<"Name of Employee -";
  cin>>emp[i].nam;
  cout<<"Designation -";
  cin>>emp[i].dsg;
  cout<<"Basic Pay -";
  cin>>emp[i].bp;
 }
}
void cal()
{
 for(int i=0;i<n;i++)
 {
  if(emp[i].bp<=3500)
  emp[i].da=(75*emp[i].bp)/100.0;
  else if(emp[i].bp>3500&&emp[i].bp<=6000)
  emp[i].da=(60*emp[i].bp)/100.0;
  else if(emp[i].bp>6000)
  emp[i].da=(25*emp[i].bp)/100.0;
  if(emp[i].bp<=2500)
  emp[i].hra=300;
  if(emp[i].bp>2500&&emp[i].bp<=5000)
  emp[i].hra=500;
  if(emp[i].bp>5000)
  emp[i].hra=750;
  emp[i].gp=emp[i].bp+emp[i].da+emp[i].hra;
  emp[i].pf=(10*emp[i].bp)/100.0;
  emp[i].it=(15*emp[i].gp)/100.0;
  emp[i].np=emp[i].gp-(emp[i].it+emp[i].pf);
 }
}
void display()
{
 cout<<"\n\nNAME"<<setw(6)<<"DESIGN"<<setw(10)<<"BP"<<setw(8)<<"DA"<<setw(10)<<"HRA"<<setw(10)<<"PF"<<setw(10)<<"IT"<<setw(10)<<"GP"<<setw(11)<<"NET PAY\n\n";
 for(int i=0; i<n; i++)
 {
  cout<<emp[i].nam<<setw(6)<<emp[i].dsg;
  cout<<setw(10)<<emp[i].bp<<setw(8)<<emp[i].da<<setw(10)<<emp[i].hra<<setw(10)<<emp[i].pf<<setw(10)<<emp[i].it;
  cout<<setw(10)<<emp[i].gp<<setw(11)<<emp[i].np<<'\n';
 }
} 
int main()
{
 input();
 cout<<"\n\t\t\t\tEMPLOYEE SALARY CALCULATION\n"; 
 cal();
 display();
 getch();
}
  
Last edited on
emp is a single object. Why are you trying to use the [] operator with it, as if it were an array?
Last edited on
The variable emp[i] doesn't mean anything because emp is a single employee, given the declaration: employee emp;

What you want is a collection of employees. Do you know how to use arrays or vectors?
so if i decrale it as
employee emp[10];
Is it gonna work???
That will work, as long as the user doesn't enter more than 10 for the number of employees.
Ty... :)
It is working now
Actually I dint notice tht i had declared it as a variable.. :P Wht I wanted was an array
Last edited on
Topic archived. No new replies allowed.