调用一个函数来填充我的主数组无法正常工作

时间:2019-05-08 12:54:50

标签: c++ arrays string function multidimensional-array

我需要创建函数,然后使用它们并在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;
}

我想发生的是在输入公司名称及其编号之后。对员工,我希望程序要求我输入员工的姓名和工资。 所以我真的可以使用一些指导,如果可以的话,请告诉我我在哪里失败。

1 个答案:

答案 0 :(得分:0)

我们,初学者,应该互相帮助。:)

您的程序中有很多错误。

对于初学者来说,头文件<array>在程序中是多余的,因为没有使用头文件中的声明。

您不应在函数声明中使用幻数(50和4)

void fillEmployees(string names[50], int salaries[50][4], int N) {

数组(和标准容器)的索引从0开始。

程序中未调用函数fillEmployees

这是main中的函数声明,而不是其调用

void fillEmployees(string names[50], int salaries[50][4], int N);

您必须声明一个容器,用于存储有关员工的信息。

我可以提出以下解决方案。

#include <iostream>
#include <string>
#include <utility>
#include <array>
#include <vector>

const size_t MAX_NUMBER_OF_EMPLOYEES = 50;
const size_t NUMBER_OF_QUATERS = 4;

using CompanyStuff = std::vector<std::pair<std::string, std::array<int, NUMBER_OF_QUATERS>>>;

void fillEmployees( CompanyStuff &employees )
{
    for ( CompanyStuff::size_type i = 0; i < employees.size(); i++ )
    {
        std::cout << "Please enter the name of the employee #" << i + 1 << ": ";
        std::getline( std::cin, employees[i].first );

        std::cout << "and his salaries throughout the " << NUMBER_OF_QUATERS << " quarters: ";
        for ( int &salary : employees[i].second ) std::cin >> salary;

        std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    }
}

std::ostream & displayCompanyStuff( const std::string &nameOfCompany, const CompanyStuff &employees, std::ostream &os = std::cout )
{
    os << "\nThe company name: " << nameOfCompany << '\n';
    os << "Employees:\n";

    for ( const auto &employee : employees )
    {
        os << '\t' << employee.first << ": ";
        for ( const auto &salary : employee.second ) os << salary << ' ';
        os << '\n';
    }

    return os;
}

int main()
{
    std::string nameOfCompany; 
    size_t numberOfEmployees;

    std::cout << "Enter the name of the company and its number of employees: ";

    if ( not std::getline( std::cin, nameOfCompany ) ) 
    {
        nameOfCompany = "Unknown";
    }

    if ( not ( std::cin >> numberOfEmployees ) or ( MAX_NUMBER_OF_EMPLOYEES < numberOfEmployees ) ) 
    {
        numberOfEmployees = MAX_NUMBER_OF_EMPLOYEES;
    }        

    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );


    CompanyStuff employees( numberOfEmployees );

    fillEmployees( employees );

    displayCompanyStuff( nameOfCompany, employees );
}

程序输出可能如下所示

Enter the name of the company and its number of employees: The bset company
2
Please enter the name of the employee #1: Peter
and his salaries throughout the 4 quarters: 3500 3500 3500 3500
Please enter the name of the employee #2: Bob
and his salaries throughout the 4 quarters: 4000 4000 4000 4000

The company name: The best company
Employees:
    Peter: 3500 3500 3500 3500 
    Bob: 4000 4000 4000 4000