-3

I need to create functions and then use them and call them in main. The function is not being called, so what is my mistake?

When the code was in main, it worked perfectly and without a hitch, but when I took it out of main and put it in a function, it no longer worked and I think the function is not being called correctly.

#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;
}

What I want to happen is that after I enter the company name and its no. of employees, I want the program to ask me to enter the employees' names and salaries. So I could really use some guidance and if you could please tell me where did I fall short.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
J.Z
  • 33
  • 1
  • 6
  • 3
    _`void fillEmployees(string names[50], int salaries[50][4], int N);`_ That's not how you call a function in c++. You probably want to read a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – πάντα ῥεῖ May 08 '19 at 12:57
  • The line `void fillEmployees(string names[50], int salaries[50][4], int N);` is a function declaration, not a call of the function. To fix, you will need to create arrays/variables to pass, then call the function using CORRECT SYNTAX. (Voting to close question, as this is essentially a glorified typo). – Peter May 08 '19 at 13:01
  • @J.Z What are the magic numbers 50 and 4? Indices in C++ start from 0. You do not call the function. The header is redundant. – Vlad from Moscow May 08 '19 at 13:03
  • It's probably just as well that the code doesn't call the function, as the function has an "off by one" error when reading to the asrrays, and will exhibit undefined behaviour if given a value of `N` that is `50` or more. – Peter May 08 '19 at 13:04
  • The 50 is supposed to represent the max number of employees and the 4 is supposed to represent the 4 quarters which the salaries of the employees will be under – J.Z May 08 '19 at 13:05

1 Answers1

0

We, beginners, should help each other.:)

There are many errors in your program.

For starters the header <array> is redundant in your program because neither declaration from the header is used.

You should not use magic numbers (50 and 4) as in the function declaration

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

Indices of arrays (and standard containers) start from 0.

The function fillEmployees is not called in the program.

This is a function declaration in main not its call

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

You have to declare a container where you will store information about employees.

I could suggest the following solution.

#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 );
}

The program output might look the following way

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 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335