-4

I'm having trouble passing off the 2D array called "sales" to a function. Any idea what I'm doing wrong? I have C++ as an online class and my teacher is no help :( The error message I'm getting is:

no instance of overloaded function "getTotal" matches the argument list as well as "COLS": undeclared identifier

getTotal function does not take 2 arguments"

// Week 7 Assignment 2
#include <iostream>
#include <iomanip>
using namespace std;

// Function Prototypes
int getTotal(int[][COLS]);


// Global Variables
const int ROWS = 4;
const int COLS = 4;
const int NUM_DIVS = 5;
const int NUM_QTRS = 5;
int sales[ROWS][COLS];
int totalSales;
string division[NUM_DIVS] = { "North", "South", "East", "West", "Quarter Total" };
string quarters[NUM_QTRS] = { "Quarter 1", "Quarter 2", "Quarter 3", "Quarter 4" };
int total;

int main()
{
    // Variables

    cout << "This program will calculate information about sales during a year." << endl;

    // Loops to fill the array
    for (int count = 0; count < COLS; count++)
    {
        cout << "Please enter the sales for the North during Quarter " << (count + 1) << ": $";
        cin >> sales[0][count];
    }
    for (int count = 0; count < COLS; count++)
    {
        cout << "Please enter the sales for the South during Quarter " << (count + 1) << ": $";
        cin >> sales[1][count];
    }
    for (int count = 0; count < COLS; count++)
    {
        cout << "Please enter the sales for the East during Quarter " << (count + 1) << ": $";
        cin >> sales[2][count];
    }
    for (int count = 0; count < COLS; count++)
    {
        cout << "Please enter the sales for the West during Quarter " << (count + 1) << ": $";
        cin >> sales[3][count];
    }
    total = getTotal(sales, 4);

    return 0;

}

// Function to get the total of everything in the array
int getTotal(int sales[][COLS])
{
    int totAl = 0;
    for (int count = 0; count < ROWS; count++)
    {
        for (int count = 0; count < COLS; count++)
            totAl += sales[count][count];

        return totAl;
    }
}
winhowes
  • 6,741
  • 5
  • 27
  • 39

1 Answers1

0

First you are using COLS before it is defined and you can't do that, so you fix that by defining it first before attempting to use it:

// Global Variables
const int ROWS = 4;
const int COLS = 4;//this is defined here

int getTotal(int[][COLS]);//now the compiler knows what COLS is

Second your getTotal function, as your compiler rightly pointed out, only accepts one argument but you are passing two in total = getTotal(sales, 4);. I believe you are trying to pass the row size as the second argument but there is no need to do this in your case since all your functions can see the ROW definition being a global variable(in fact you are using ROWS inside getTotal function).

total = getTotal(sales); //this is enough to call the function

You should also note that there is no need to pass any parameter to your getTotal function since the array is declared global,though i would not advise you to use global variables this extensively in your code.

 int getTotal();//function prototype without parameters since sales array is global variable
 total = getTotal(); //call the function

You might also need to revisit the logic of you code inside getTotal since the current implementation, like defining the same variable count as both of your loops counter, will lead to unexpected result.

Biruk Abebe
  • 2,174
  • 1
  • 10
  • 23
  • Thank you so much bkVnet....I have the function up and running now thanks to you. This assignment requires 5 more functions though so I will hopefully apply the same technique to get those to work. I truly appreciate it. – Jambvant Ramoutar May 07 '16 at 18:13