cin >> m, n;
是不正确,它输入只有这可以解释为m:
(cin >> m), n;
这意味着:cin, n;
纠正它:因为你检查此条件两次
cin >> m >> n;
if(a > 0 || b > 0) break;
是多余的:一旦在条件成立的时候(a或b等于或小于0),while while条件second while while检查相同的条件是多余的,因为它会自动中断。
你通过而不通过其大小的阵列,你是幸运的,如果设置了第一元件1中的任何第二值等于阵列例如大小:
m = 1; n = 10; then the size is ten which is correct.
什么约:
m = 7; n = 10; // now is size 10? it's only 3
纠正它传递的大小如:
m = 4; n = 8;
int size = 8 - 4;
cout << Squared(m, n, M, size);
也:
for (int p = 0; p < b; p++)
{
for (int i = a ; i < b; i++)
{
q[p] = a;
}
}
你正在分配相同的值a
到数组的所有元素和迭代在嵌套循环中做同样的事情!它很可能写入:
int x = 0; x = 0;
因此,平方内结果的条件永远不会成功,因为相同的值永远不会等于它的平方。 4 = 4 * 4
从未达到
这里是你的搜索内容:
#include <iostream>
using namespace std;
// I only make squared search for the result not inputing m and n lik e in yours
int squared (int m, int n, int* M)
{
int result;
for(int i(0); i < n; i++)
for(int j(0); j < n; j++)
for(int k(0); k < n; k++)
if((M[i] == ((M[j] * M[j]) + (M[k] * M[k]))) && j != k) // here to avoid comparing with the same index
{
cout << M[i] << " = (" << M[j] << "*" << M[j] << ") + (" << M[k] << "*" << M[k] << ")" << endl;
result = ((M[j] * M[j]) + (M[k] * M[k]));
cout << result << endl;
return result; // if success we return result
}
return -1; // otherwise we return -1 as a sign of error (no square yields in negative value)
}
int main()
{
int n, m, size;
do
{
cout <<"m: ";
cin >> m;
cout << "n: ";
cin >> n;
if(n <= 0 || m <= 0)
cout <<"You can't input an integer that is 0 or below: ";
// also it's very important to check whether n is greater than m or not because if m == n or m > n then size will be negative and as you know no array has a negative nor 0 size
if(m >= n)
cout << "n must be greater than m!" << endl;
}while (m <= 0 || n <= 0 || m >= n);
size = n - m; // getting size of array assuming m can be any value
int* M = new int[n - m]; // allocating dynamic array
// inputting array as you asked
for(int i(0), j = m; i < size; i++, j++)
M[i] = j;
// checking the values of array elements
for(int i = 0; i < size; i++)
cout << M[i] << ", " ;
cout << endl;
// getting the result
cout << squared(m, n, M) << endl;
// cleaning
delete[] M;
return 0;
}
'CIN >> M,N;'没有做什么,你认为它。 – NathanOliver
您是否尝试过在调试器中查找错误发生的位置?另外,你能定义“崩溃”吗?究竟出了什么问题?有关调试小程序,请参阅[本文](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。 – EJoshuaS
'cin >> m >> n;' – Raindrop7