0

This program is meant to call to read_in, sum_row, and print. However, I am getting error messages saying that these functions are not declared in this scope. How can I fix this? I'm sure its somewhat of an easy fix but I am just starting to learn function calls and I can't for the life of me figure this out. Any help is very much appreciated, thank you!

#include <iostream>
#include <iomanip>

using namespace std;

int sum = 0;
int row = 0;
int col = 0;

const int stud_num_rows = 28;
const int stud_num_cols = 10;

const int fac_num_rows = 15;
const int fac_num_cols = 10;

const int staff_num_rows = 100;
const int staff_num_cols = 10;

const int adm_num_rows = 30;
const int adm_num_cols = 10;

int main()
{
    
    int students[28][10];
    int faculty[15][10];
    int staff[100][10];
    int admin[30][10];
    
    read_in(students, faculty, staff, admin);
    
    sum_row(students, faculty, staff, admin);
    
    print(students, faculty, staff, admin);
    
    return 0;
}

void read_in(int students[][stud_num_cols],
             int faculty[][fac_num_cols],
             int staff[][staff_num_cols],
             int admin[][adm_num_cols])
{
    for (row = 0; row < stud_num_rows; row++)
        for (col = 0; col < stud_num_cols; col++)
            cin >> students[row][col];
    
    for (row = 0; row < fac_num_rows; row++)
        for (col = 0; col < fac_num_cols; col++)
            cin >> faculty[row][col];
    
    for (row = 0; row < staff_num_rows; row++)
        for (col = 0; col < staff_num_cols; col++)
            cin >> staff[row][col];
    
    for (row = 0; row < adm_num_rows; row++)
        for (col = 0; col < adm_num_cols; col++)
            cin >> admin[row][col];
}

void sum_row(int students[][stud_num_cols],
             int faculty[][fac_num_cols],
             int staff[][staff_num_cols],
             int admin[][adm_num_cols])
{
    row = 1;
    
    cout << "The sum of the first row of each array is:" << endl;
    
    for (col = 0; col < stud_num_cols; col++)
    {
        sum = sum + students[row][col];
    }
    cout << left << setw(5) << "Students:" << sum << endl;
    
    for (col = 0; col < fac_num_cols; col++)
    {
        sum = sum + faculty[row][col];
    }
    cout << left << setw(5) << "Students:" << sum << endl;
    
    for (col = 0; col < staff_num_cols; col++)
    {
        sum = sum + staff[row][col];
    }
    cout << left << setw(5) << "Students:" << sum << endl;
    
    for (col = 0; col < adm_num_cols; col++)
    {
        sum = sum + admin[row][col];
    }
    cout << left << setw(5) << "Admin:" << sum << endl;
}

void print(int students[][stud_num_cols],
           int faculty[][fac_num_cols],
           int staff[][staff_num_cols],
           int admin[][adm_num_cols])
{
    for (row = 0; row < stud_num_rows; row++)
        for (col = 0; col < stud_num_cols; col++)
            cout << setw(5) << students[row][col] << " ";
    cout << endl;
    
    for (row = 0; row < fac_num_rows; row++)
        for (col = 0; col < fac_num_cols; col++)
            cout << setw(5) << faculty[row][col] << " ";
    cout << endl;
    
    for (row = 0; row < staff_num_rows; row++)
        for (col = 0; col < staff_num_cols; col++)
            cout << setw(5) << staff[row][col] << " ";
    cout << endl;
    
    for (row = 0; row < adm_num_rows; row++)
        for (col = 0; col < adm_num_cols; col++)
            cout << setw(5) << admin[row][col] << " ";
    cout << endl;
}
user4581301
  • 29,019
  • 5
  • 26
  • 45
  • Thou shalt declare before first use. – user4581301 Nov 19 '20 at 02:03
  • Think like a compiler. The first time you see `read_in` is in `main` where you are supposed to call it - but you've never seen it before so you don't know what it is! You need to put a declaration of `read_in` before it is used (or just move `main` to the end so the actual definition is before `main` This might help too https://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration – John3136 Nov 19 '20 at 02:03
  • I'm facepalming now... thanks for the help, guys. Won't make that mistake again. – FlashpointPrdx Nov 19 '20 at 02:12
  • I appreciate the use of named constants instead of magic numbers, but I would suggest actually using them. I know `int students[stud_num_rows][stud_num_cols];` is longer than `int students[28][10];` but I think it is worth it. – Nathan Pierson Nov 19 '20 at 02:22
  • yeah, I used `int students[28][10]` because my teacher said to "use the following declarations". I see what you mean, though. – FlashpointPrdx Nov 19 '20 at 02:36

1 Answers1

1

You have to either move other functions above main(), or put function prototypes above main().

Eugene
  • 4,579
  • 1
  • 16
  • 29