当我尝试输入值时程序崩溃

Program crashes when I try to input values

本文关键字:程序 崩溃 输入      更新时间:2023-10-16

所以我的程序里有整数m和n,一旦你输入了值,它应该创建一个值从m到n的数组(例如m = 1和n = 10,它创建数组q的值从1到10)。然后,它会在数组中查找是否有任何数字等于任何两个平方的数字总和(例如,在数组中,数字 5 等于 1 平方 + 2 平方)。问题是当我尝试输入第一个值时,它会崩溃,很确定问题出在函数中,但似乎无法弄清楚。谢谢

#include <iostream>
using namespace std;
int squared (int a, int b, int q[]){

while (a<=0 || b<=0){
cout <<"You can't input an integer that is 0 or below: ";
cin >>a;
cin >>b;
if (a>0 || b>0) break;
}

for (int p=0; p<b; p++){
for (int i=a ; i<b; i++){
q[p] = a;
}
}
for (int z=0; z<b; z++){
for (int x=0; x<b; x++){
for (int c=0; c<b; c++){
if (q[z] == (q[x] * q[x]) + (q[c] * q[c])){
int result= (q[x] * q[x]) + (q[c] * q[c]);
return result;
}
}
}
}

}
int main () {
int m,n;
int M[100];
cout <<"Input integers m un n: ";
cin >>m,n;
cout <<squared(m,n,M);
return 0;
}

它很可能因为以下原因而崩溃:cin >>m,n;,它应该是cin >> m >> n;的,否则你在下一行使用未初始化n,从而得到未定义的行为,例如崩溃。

您使用什么编译器以及使用什么标志,因为这会在正常编译时触发一些警告/错误。

cin >> m, n;不正确,它只输入m,可以解释为:

(cin >> m), n;这意味着:cin, n;更正它:

cin >> m >> n;

if(a > 0 || b > 0) break;是多余的,因为您检查此条件两次:一次在 while 条件第二个在 while 循环中,因此检查相同的条件是多余的,因为如果条件成功(a 或 b 等于或小于 0),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;
}