无法使用数组程序调用bool函数 我是C++的新手(一般是编码),我正在学习数组。分配的目的是让用户输入方形参数和数字来填充每一行。我的程序的工作是获取用户输入的数字,并根据行、列和对角线验证和
嗯,最初我写程序的时候主要是这样的,它起作用了。但是,我必须让程序在bool函数中运行,如果失败,则返回false。就我的一生而言,我想不出一个办法让它发挥作用。对于1个未解决的外部问题,我不断收到致命错误。有人能帮我吗?我已经用尽了所有的资源 多谢各位无法使用数组程序调用bool函数 我是C++的新手(一般是编码),我正在学习数组。分配的目的是让用户输入方形参数和数字来填充每一行。我的程序的工作是获取用户输入的数字,并根据行、列和对角线验证和,c++,arrays,boolean,C++,Arrays,Boolean,嗯,最初我写程序的时候主要是这样的,它起作用了。但是,我必须让程序在bool函数中运行,如果失败,则返回false。就我的一生而言,我想不出一个办法让它发挥作用。对于1个未解决的外部问题,我不断收到致命错误。有人能帮我吗?我已经用尽了所有的资源 多谢各位 #include <iostream> #include <iomanip> #include <fstream> using namespace std; bool va
#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;
}
}
你说
bool validateSums(int, int);
然后
这些不一样。编译器和链接器正在尝试查找接受2个整数的validateSums函数。您没有提供一个,因此出现了错误消息
由于不想阅读所有代码,我不知道您实际需要哪一个:int、int或int、[[10] 您的问题似乎是顶部的函数
validateSums
的声明与您调用它的方式和实际定义不一致
在顶部比较您的声明:
bool validateSums(int, int);
以及main后面的定义:
bool validateSums(int row, int square[][10])
{
// ...
}
请发布错误消息。在
if(compareTotal!=total){…}
中的return
之前还有一个break
,在validateSums
中有if(compareTotal!=total){…}
,因此返回将不会到达。1>----构建已启动:项目:控制台应用程序18,配置:调试Win32----1>MSVCRTD.lib(crtexe.obj):错误LNK2019:未解析的外部符号\u main在函数\uuuuu tmainCRTStartup 1>C:\Users\Sasha\Documents\CS 161\Assignment 4\ConsoleApplication18\Debug\ConsoleApplication18.exe中引用:致命错误LNK1120:1未解析的外部===生成:0成功,1失败,0最新,0跳过===下次,请编辑您的问题并将错误消息粘贴到您的问题中。是!成功了!谢谢!:)我在原型中使用了[][10]。我真不敢相信就这么多-_-
bool validateSums(int, int);
bool validateSums(int row, int square[][10])
{
// ...
}