【问题标题】:Calling a function that fills in an array in my main is not working在我的 main 中调用填充数组的函数不起作用
【发布时间】: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”错误,并且如果给定值将表现出未定义的行为N50 或更多。
  • 50代表最大员工人数,4代表员工工资低于的4个季度

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


【解决方案1】:

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

你的程序有很多错误。

对于初学者来说,标题 &lt;array&gt; 在您的程序中是多余的,因为没有使用标题中的声明。

您不应在函数声明中使用幻数(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 

【讨论】:

  • 不确定你算不算初学者,弗拉德!
  • @LightnessRacesinOrbit 我从未做过 C++ 程序员。
  • 这里很多都没有,但你有一个c++ 金徽章 - 足够接近。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-13
  • 2018-12-29
  • 1970-01-01
  • 2021-08-11
  • 1970-01-01
  • 2018-07-01
相关资源
最近更新 更多