当我尝试输入值时程序崩溃
Program crashes when I try to input values
所以我的程序里有整数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;
}
相关文章:
- 程序崩溃并显示"std::out_of_range"错误
- 试图创建流或fopen时程序崩溃
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 如何找出应用程序崩溃的原因 - Win 10 LTSB
- 操纵安卓相机的深度图导致应用程序崩溃
- 为什么当我尝试搜索双链表中第一个数据条目之外的数据时,程序崩溃了?
- DLL Made with CMake 使程序崩溃
- 程序崩溃使用boost::asio
- 调用 free() 有时会导致程序崩溃
- 邮件加密程序崩溃
- 调用 java 的回调() 时应用程序崩溃.由于 detatchThread 而获得运行时错误
- 比较迭代器会使程序崩溃,而不会在自定义气泡排序实现中出现错误
- For 循环在尝试读取数组 c++ 时程序崩溃
- 即使有 0 个错误,Getter 似乎也会使程序崩溃
- Windows桌面程序保存您的计算机会话 - 基于程序崩溃时的恢复会话
- 无法访问的代码如何导致我的程序崩溃?
- 矢量迭代器在尝试调用函数时使我的程序崩溃
- QCompleter set模型使应用程序崩溃
- 使用唯一指针调用函数会使我的程序崩溃
- 删除SDL_PollEvent时程序崩溃