#include <iostream>
using namespace std;
//Constants
const int EMPLOYEE_NUM = 4, MONTH_NUM = 5;
const double MONTH_QUOTA[MONTH_NUM] = {1, 1.05, 1.1, 1.15, 1.2};
int main ()
{
//Define Arrays
string names[EMPLOYEE_NUM][2], count[EMPLOYEE_NUM] = {"first", "second", "third", "fourth"};
double salesGoal[EMPLOYEE_NUM][MONTH_NUM];
//Initialize Loop Variables
int employee, month;
//Get Names of Salesmen
for (int employee = 0; employee < EMPLOYEE_NUM; employee++)
{
cout << "Enter the first name of the " << count[employee] << " salesman: \t";
cin >> names[employee][0];
cout << "Enter the last name of the " << count[employee] << " salesman: \t";
cin >> names[employee][1];
}
//Get Initial Sales Goal For Each Salesman
for (int employee = 0; employee < EMPLOYEE_NUM; employee++)
{
cout << "Enter the initial sales goal for " << names[employee][0] << ": \t$";
cin >> salesGoal[employee][0];
while (salesGoal[employee][0] < 0)
{
cout<< "Number cannot be negative. Enter the initial sales goal for " << names[employee][0] << ": \t$";
cin >> salesGoal[employee][0];
}
}
//Make Sales Goal Array for All 5 Months
for (int employee = 0; employee < EMPLOYEE_NUM; employee++)
{
for (int month = 1; month < MONTH_NUM; month++)
{
salesGoal[employee][month] = salesGoal[employee][month-1]*MONTH_QUOTA[month];
}
}
cout << endl;
//Display Names And Projected Sales Table
cout << "Name\t\t\t"<< "Sales Goal" << endl;
cout << "*****************************************************" <<endl;
for (int employee = 0; employee < EMPLOYEE_NUM; employee++)
{
cout << names[employee][1] << ", " << names[employee][0] << endl;
for (int month = 0; month < MONTH_NUM; month++)
{
cout << "\t" << "Month " << month + 1 << ":\t$" << salesGoal[employee][month] <<endl;
}
cout << endl;
}
return 0;
}
I have made this for a class, however I want to make this part of the code into a function
for (int employee = 0; employee < EMPLOYEE_NUM; employee++)
{
for (int month = 1; month < MONTH_NUM; month++)
{
salesGoal[employee][month] = salesGoal[employee][month-1]*MONTH_QUOTA[month];
}
}
after looking through stacks i found an explanation along the lines of this
using namespace std;
//Contants
const int EMPLOYEE_NUM = 4, MONTH_NUM = 5;
const double MONTH_QUOTA[MONTH_NUM] = {1, 1.05, 1.1, 1.15, 1.2};
//Make Array with All 5 Months
void projectedSales(double (*x)[EMPLOYEE_NUM][MONTH_NUM], double MONTH_QUOTA[])
{
for (int employee = 0; employee < EMPLOYEE_NUM; employee++)
{
for (int month = 1; month < MONTH_NUM; month++)
{
x[employee][month] = x[employee][month-1]*MONTH_QUOTA[month];
}
}
}
int main ()
{
//Define Arrays
string names[EMPLOYEE_NUM][2], count[EMPLOYEE_NUM] = {"first", "second", "third", "fourth"};
double salesGoal[EMPLOYEE_NUM][MONTH_NUM];
double (*p_salesGoal)[MONTH_NUM];
p_salesGoal = salesGoal
//Initialize Loop Variables
int employee, month;
//Get Names of Salesmen
for (int employee = 0; employee < EMPLOYEE_NUM; employee++)
{
cout << "Enter the first name of the " << count[employee] << " salesman: \t";
cin >> names[employee][0];
cout << "Enter the last name of the " << count[employee] << " salesman: \t";
cin >> names[employee][1];
}
//Get Initial Sales Goal For Each Salesman
for (int employee = 0; employee < EMPLOYEE_NUM; employee++)
{
cout << "Enter the initial sales goal for " << names[employee][0] << ": \t$";
cin >> salesGoal[employee][0];
while (salesGoal[employee][0] < 0)
{
cout<< "Number cannot be negative. Enter the initial sales goal for " << names[employee][0] << ": \t$";
cin >> salesGoal[employee][0];
}
}
cout << endl;
projectedSales(p_salesGoal, MONTH_QUOTA);
//Display Names And Projected Sales Table
cout << "Name\t\t\t"<< "Sales Goal" << endl;
cout << "*****************************************************" <<endl;
for (int employee = 0; employee < EMPLOYEE_NUM; employee++)
{
cout << names[employee][1] << ", " << names[employee][0] << endl;
for (int month = 0; month < MONTH_NUM; month++)
{
cout << "\t" << "Month " << month + 1 << ":\t$" << salesGoal[employee][month] <<endl;
}
cout << endl;
}
return 0;
}
but receive the errors
main.cpp:20:58: error: invalid operands of types ‘double [5]’ and ‘double’ to binary ‘operator*’
x[employee][month] = x[employee][month-1]*MONTH_QUOTA[month];
~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
main.cpp: In function ‘int main()’:
main.cpp:36:5: error: expected ‘;’ before ‘int’
int employee, month;
^~~
main.cpp:57:44: error: cannot convert ‘double (*)[5]’ to ‘double (*)[4][5]’ for argument ‘1’ to ‘void projectedSales(double (*)[4][5], double*)’
projectedSales(p_salesGoal, MONTH_QUOTA);
what am i doing wrong? how do i pass the array i've made known as salesGoal to my function so it can be computed and modified with my constant global array MONTH_QUOTA?