-1

sorry I am relatively new to c++ and am currently stuck. The point of the application is to have the user enter the number of employees they have and then information about their employees including the hours they worked and their pay rate. After that that application to print out all the information and then give them each employees gross pay. I thought I had everything set up correctly but am getting an error on line 26 it is saying "expression must have constant value". Any tips or advice would be appreciated.

#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
#include<iomanip>

using namespace std;

struct Employee
{
    int id;
    string fName;
    string lName;
    int pay;
    int hours;
};

int main() {

    int i, n;

    cout << "Enter number of employees";
    cin >> n;

    Employee Emp[n];

    for (i = 0; i < n; i++) {

    cout << "Enter Employee ID: ";
    cin >> Emp[i].id;
    cout << "Enter First Name: ";
    cin >> Emp[i].fName;
    cout << "Enter Last Name: ";
    cin >> Emp[i].lName;
    cout << "Enter in Pay Rate: ";
    cin >> Emp[i].pay;
    cout << "Enter in Hours: ";
    cin >> Emp[i].hours;
    }

    cout << "\n*** Employee Details ***";
    cout << "ID" << setw(15) << "First Name" << setw(10) << "Last Name" << setw(10) << "Pay" << setw(10) << "Hours" << setw(10) << "Gross Pay";

    for (i = 0; i < n; i++) {
        cout << "\n" << Emp[i].id << setw(15) << Emp[i].fName << setw(10) << Emp[i].lName << setw(10) << Emp[i].pay << setw(10) << Emp[i].hours << setw(10) << Emp[i].pay*Emp[i].hours;
    }

    _getch();
    return 0;
}
MattHogen
  • 3
  • 3
  • Array must have fixed size. Read more about arrays in C++. – CroCo Mar 08 '19 at 21:57
  • You might want to take a different approach such as using `malloc` to initialize your array of employees. The [] syntax expects a constant at compile time. – Abass Sesay Mar 08 '19 at 21:58
  • [Prefer `new` to `malloc`](https://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-vs-new). [Prefer just about anything else to `new`](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new). In this case a [`std::vector` seems appropriate](https://en.cppreference.com/w/cpp/container/vector). – user4581301 Mar 08 '19 at 22:17
  • Sumplementary reading: [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – user4581301 Mar 08 '19 at 22:19

2 Answers2

1
Employee Emp[n];

In C/C++ you can't declare dynamic-size arrays like this.

See this question - How to create a dynamic array of integers

Or better, use an std::vector instead.

battlmonstr
  • 3,898
  • 1
  • 18
  • 29
0

C++ standard requires you to provide an array size known at compile time. Therefore to acquire what you want you need to use dynamic memory allocation i.e. allocate an array on heap depending upon the n being entered by the user. The following demonstrates this method.

#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
#include<iomanip>

using namespace std;

struct Employee
{
    int id;
    string fName;
    string lName;
    int pay;
    int hours;
};

int main() {

    int i, n;

    cout << "Enter number of employees";
    cin >> n;

    auto *Emp = new Employee[n];

    for (i = 0; i < n; i++) {

    cout << "Enter Employee ID: ";
    cin >> Emp[i].id;
    cout << "Enter First Name: ";
    cin >> Emp[i].fName;
    cout << "Enter Last Name: ";
    cin >> Emp[i].lName;
    cout << "Enter in Pay Rate: ";
    cin >> Emp[i].pay;
    cout << "Enter in Hours: ";
    cin >> Emp[i].hours;
    }

    cout << "\n*** Employee Details ***";
    cout << "ID" << setw(15) << "First Name" << setw(10) << "Last Name" << setw(10) << "Pay" << setw(10) << "Hours" << setw(10) << "Gross Pay";

    for (i = 0; i < n; i++) {
        cout << "\n" << Emp[i].id << setw(15) << Emp[i].fName << setw(10) << Emp[i].lName << setw(10) << Emp[i].pay << setw(10) << Emp[i].hours << setw(10) << Emp[i].pay*Emp[i].hours;
    }
    delete [] Emp;
    return 0;
}
aep
  • 1,393
  • 9
  • 18