无法使用数组程序调用bool函数

时间:2014-11-18 00:20:14

标签: c++ arrays boolean

我是C ++(一般编码)的新手,我正在学习数组。赋值的目的是让用户输入平方参数和数字来填充每一行。我的程序的工作是获取用户输入数字并根据行,列和对角线验证总和。

好吧,最初当我在main编写程序时,它起作用了。但是,我必须让程序在bool函数中运行,如果失败则返回false。对于我的生活,我无法想办法让它发挥作用。我一直在解决1个未解决的外部因素的致命错误。有人可以帮帮我吗?我已经耗尽了所有资源。

谢谢。

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

错误消息(粘贴自评论):

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 ==========

2 个答案:

答案 0 :(得分:2)

你说

 bool validateSums(int, int);

然后

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

这些不一样。编译器和链接器正在尝试查找需要2个整数的validateSums函数。你没有提供一个 - 因此错误信息

不想阅读所有代码我不知道你真正需要的代码:int,int或int,[] [10]。

答案 1 :(得分:1)

您的问题似乎是顶部函数validateSums的声明与您调用它的方式和实际定义不符。

比较您在顶部的声明:

bool validateSums(int, int);

和main之后的定义:

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