【发布时间】:2019-09-26 05:18:29
【问题描述】:
我需要创建函数,然后使用它们并在 main 中调用它们。该函数没有被调用,那么我的错误是什么?
当代码在 main 中时,它可以完美地运行,没有任何问题,但是当我将它从 main 中取出并放入一个函数时,它不再工作,我认为该函数没有被正确调用。
#include <iostream>
#include <string>
#include <array>
using namespace std;
void fillEmployees(string names[50], int salaries[50][4], int N) {
for (int i = 1; i <= N; i++) {
cout << "Please enter the name and salaries of employee " << i << " throughout the four quarters: ";
cin >> names[i];
for (int quarters = 0; quarters < 4; quarters++)
cin >> salaries[N][quarters];
}
}
int main() {
string nameOfCompany;
int N;
cout << "Enter the name of the company and its number of employees: ";
cin >> nameOfCompany >> N;
void fillEmployees(string names[50], int salaries[50][4], int N);
system("pause");
return 0;
}
我想要发生的是,在我输入公司名称和编号后。的员工,我希望程序要求我输入员工的姓名和薪水。 所以我真的可以使用一些指导,如果你能告诉我我的不足之处。
【问题讨论】:
-
void fillEmployees(string names[50], int salaries[50][4], int N);这不是你在 C++ 中调用函数的方式。您可能想阅读good book。 -
void fillEmployees(string names[50], int salaries[50][4], int N);行是函数声明,而不是函数调用。要修复,您需要创建要传递的数组/变量,然后使用正确的语法调用该函数。 (投票结束问题,因为这本质上是一个美化的错字)。 -
@J.Z 神奇的数字 50 和 4 是什么? C++ 中的索引从 0 开始。您不调用该函数。标头
是多余的。 -
代码不调用该函数可能也不错,因为该函数在读取数组时会出现“off by one”错误,并且如果给定值将表现出未定义的行为
N即50或更多。 -
50代表最大员工人数,4代表员工工资低于的4个季度
标签: c++ arrays string function multidimensional-array