Sasha Miko
Sasha Miko

Reputation: 25

Cannot call bool function with array program

I am new to C++ (and coding in general) and am learning about arrays. The purpose of the assignment was to have a user input square parameters and numbers to fill each row. My program's job was to take user input numbers and validate sums against rows, columns, and diagonals.

Well, originally when I wrote the program in main, it worked. However, I have to get the program to run in a bool function and return a false if it fails. For the life of me I cannot figure out a way to make it work. I keep getting fatal errors for 1 unresolved externals. Can someone please help me? I've exhausted all resources.

Thank you.

    #include <iostream>
   #include <iomanip>
   #include <fstream>

   using namespace std;

   bool validateSums(int, int);

   int main()
   {
    int row,
        square[10][10];
    bool match;

    cout << "Enter the size of your square by the # of rows (e.g. 3 would yield a 3x3 square).\n"
        << "Please keep the # to 10 or below." << endl;
    cin >> row;

    if (row >= 1 && row <= 10)
    {
        cout << "Your square will be " << row << "  x " << row << " big." << endl;

        for (int index = 0; index < row; index++)
        {
            cout << "List " << row << " numbers for row " << (index + 1)
                << " separated by a space." << endl;
            for (int colindex = 0; colindex < row; colindex++)
            {
                cin >> square[index][colindex];
            }
        }
        cout << "You listed \n";
        for (int index = 0; index < row; index++)
        {
            for (int colindex = 0; colindex < row; colindex++)
            {
                cout << setw(2) << square[index][colindex] << " ";
            }
            cout << endl;
        }

        match = validateSums(row, square);
        cout << match << endl;
    }
    else
    {
        cout << "You must enter a # between 1-10." << endl;
    }

    return 0;
   }

   bool validateSums(int row, int square[][10])
   {
    //summation of 1st row
    int total = 0,
        compareTotal = 0;

    for (int colindex = 0; colindex < row; colindex++)
    {
        total += square[0][colindex];
    }

    //calculation of sum for rest of rows while comparing to total
    for (int index = 1; index < row; index++)
    {
        for (int colindex = 0; colindex < row; colindex++)
        {
            compareTotal += square[index][colindex];
        }

        if (compareTotal != total)
        {
            cout << "The rows entered do not match." << endl;
            break;
            return false;
        }
        else
        {
            compareTotal = 0;
        }
    }

    //summation of 1st column
    total = 0;
    for (int index = 0; index < row; index++)
    {
        total += square[index][0];
    }
    cout << "Sum of column 1 is " << total << endl;

    //calculation of sum for rest of columns while comparing to total
    compareTotal = 0;
    for (int colindex = 1; colindex < row; colindex++)
    {
        for (int index = 0; index < row; index++)
        {
            compareTotal += square[index][colindex];
        }

        if (compareTotal != total)
        {
            cout << "The columns entered do not match." << endl;
            break;
            return false;
        }
        else
        {
            compareTotal = 0;
        }
    }

    //summation of 1st diagonal
    total = 0;
    for (int index = 0; index < row; index++)
    {
        total += square[index][index];
    }
    cout << "Sum of diagonal 1 is " << total << endl;

    //calculation of sum of the other diagonal
    compareTotal = 0;
    for (int index = 0; index < row; index++)
    {
        compareTotal += square[index][row - 1 - index];
    }
    if (compareTotal != total)
    {
        cout << "The diagonals entered do not match." << endl;
        return false;
    }
}

Error message (pasted from comment):

1>------ Build started: Project: ConsoleApplication18, Configuration: Debug Win32 ------
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\Users\Sasha\Documents\CS 161\Assignment 4\ConsoleApplication18\Debug\ConsoleApplication18.exe : fatal error LNK1120: 1 unresolved externals
==========  Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Upvotes: 1

Views: 122

Answers (2)

AliciaBytes
AliciaBytes

Reputation: 7429

Your problem seems to be that the declaration of the function validateSums on the top doesn't correspond with the way you call it and the actual definition.

Compare your declaration at the top:

bool validateSums(int, int);

and the definition after main:

bool validateSums(int row, int square[][10])
{
    // ...
}

Upvotes: 1

pm100
pm100

Reputation: 50110

you say

 bool validateSums(int, int);

then

bool validateSums(int row, int square[][10])

These are not the same. The compiler and linker is trying to find the validateSums function that takes 2 ints. You didn't supply one - hence the error message

Not wanting to read all your code I don't know which one you actually need: int,int or int,[][10].

Upvotes: 2

Related Questions