Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

c++ - I am trying to write this program but it keeps on displaying an a error everything is mentioned below can anyone check

Question:

Write a C++ program that stores and siplays following employee data of a company in structures:

  • Employee ID (int)
  • Employee Name (string)
  • Email (string)
  • Designation (char)
  • Salary (float)
  • Projects (array of strings)

Your code must contain following:

  • Pointer to structure
  • Array of Structure
  • Structure variable

Source Code:

#include<iostream>
#include<string>
using namespace std;

struct EmployeeData
{
    int Eid;
    string Ename;
    string Eemail;
    string Desig;
    float salary{};
    string projects[10];
    };

int main()
{

    cout<<endl;
    struct EmployeeData *E,d;
    E=&d;
    int i=0;
    int counter=0;
    char a;
    do
    {
        cout<<"
DETAIL OF EMPLOYEE #"<<i+1<<"
"<<endl;
        cout<<"Employee ID: ";
        cin>>(*E).Eid;
        cout<<"Employee Name: ";
        cin>>(*E).Ename;
        cout<<"Employee Email: ";
        cin>>(*E).Eemail;
        cout<<"Designation: ";
        cin>>(*E).Desig;
        cout<<"Salary: ";
        cin>>(*E).salary;
        cout<<"Projects: ";
        cin>>(*E).projects[i];
        cout<<"To print detail of all employees press p: ";
        cin>>a;
        i++;
        counter++;
    }
    while(a!='p');
   cout<<"Employee ID"<<"Name"<<"Email"<<"Designation"<<"Salary"<<"Projects"<<endl;
   {
    cout<<(*E).Eid[i]<<(*E).Ename[i]<<(*E).Eemail[i]<<(*E).Desig[i]<<(*E).salary[i]<<(*E).projects[i]<<endl;
   }
    return 0;
    
}

Errors:

10 15 C:UsershpDesktopUntitled1.cpp [Warning] non-static data member initializers only available with -std=c++11 or -std=gnu++11

10 14 C:UsershpDesktopUntitled1.cpp [Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11

10 15 C:UsershpDesktopUntitled1.cpp [Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11

C:UsershpDesktopUntitled1.cpp In function 'int main()':

48 21 C:UsershpDesktopUntitled1.cpp [Error] invalid types 'int[int]' for array subscript

48 83 C:UsershpDesktopUntitled1.cpp [Error] invalid types 'float[int]' for array subscript


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  1. The first three errors indicate you're trying to brace-initialize the float member without C++11 compilation. Either enable C++11 compilation or change:
float salary{};

To

float salary;
  1. The final two errors indicate you're incorrectly attempting to index something with [i]. Change:
cout<<(*E).Eid[i]<<(*E).Ename[i]<<(*E).Eemail[i]<<(*E).Desig[i]<<(*E).salary[i]<<(*E).projects[i]<<endl;

to

cout<<(*E).Eid<<(*E).Ename<<(*E).Eemail<<(*E).Desig<<(*E).salary<<(*E).projects<<endl;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...