C++结构体员工薪资计算器报错求助:表达式需为常量值
Hey there! Let's break down the error you're seeing and fix your employee management program.
问题根源
The error on line 26 (Employee Emp[n];) happens because standard C++ doesn't support Variable-Length Arrays (VLAs). The variable n is a runtime value (entered by the user when the program runs), but C++ requires array sizes to be compile-time constants—values that are fixed before the program starts.
两种可行解决方案
Here are two common, clean ways to fix this, with the second being the more idiomatic C++ approach:
方法1:手动动态内存分配 (new/delete)
We can allocate the array on the heap using new, and remember to free the memory with delete[] to avoid leaks:
#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; // Dynamically allocate array of Employee objects Employee* Emp = new Employee[n]; for (i = 0; i < n; i++) { cout << "\nEnter details for Employee #" << i+1 << ":\n"; cout << "Employee ID: "; cin >> Emp[i].id; cout << "First Name: "; cin >> Emp[i].fName; cout << "Last Name: "; cin >> Emp[i].lName; cout << "Pay Rate: "; cin >> Emp[i].pay; cout << "Hours Worked: "; cin >> Emp[i].hours; } cout << "\n*** Employee Details ***\n"; cout << left << setw(8) << "ID" << setw(15) << "First Name" << setw(12) << "Last Name" << setw(8) << "Pay" << setw(10) << "Hours" << setw(12) << "Gross Pay\n"; for (i = 0; i < n; i++) { cout << left << setw(8) << Emp[i].id << setw(15) << Emp[i].fName << setw(12) << Emp[i].lName << setw(8) << Emp[i].pay << setw(10) << Emp[i].hours << setw(12) << Emp[i].pay * Emp[i].hours << "\n"; } // Free the dynamically allocated memory delete[] Emp; _getch(); return 0; }
方法2:使用std::vector(推荐)
std::vector is a C++ standard library container that acts as a dynamic array. It handles memory management automatically, making it safer and easier to use than manual allocation:
#include <iostream> #include <conio.h> #include <fstream> #include <string> #include<iomanip> #include <vector> // Include vector header 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; // Create a vector with n Employee objects vector<Employee> Emp(n); for (i = 0; i < n; i++) { cout << "\nEnter details for Employee #" << i+1 << ":\n"; cout << "Employee ID: "; cin >> Emp[i].id; cout << "First Name: "; cin >> Emp[i].fName; cout << "Last Name: "; cin >> Emp[i].lName; cout << "Pay Rate: "; cin >> Emp[i].pay; cout << "Hours Worked: "; cin >> Emp[i].hours; } cout << "\n*** Employee Details ***\n"; cout << left << setw(8) << "ID" << setw(15) << "First Name" << setw(12) << "Last Name" << setw(8) << "Pay" << setw(10) << "Hours" << setw(12) << "Gross Pay\n"; for (i = 0; i < n; i++) { cout << left << setw(8) << Emp[i].id << setw(15) << Emp[i].fName << setw(12) << Emp[i].lName << setw(8) << Emp[i].pay << setw(10) << Emp[i].hours << setw(12) << Emp[i].pay * Emp[i].hours << "\n"; } _getch(); return 0; }
小优化说明
I also tweaked the output formatting with left and adjusted setw values to make the employee table look neater—feel free to adjust those numbers if you want different spacing!
内容的提问来源于stack exchange,提问作者MattHogen

