我尝试输入值时程序崩溃

时间:2016-11-28 20:29:59

标签: c++ debugging

所以我在我的程序中得到了整数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;
}

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

(cin >> m), n;表示:cin, n;要更正它:

cin >> m >> n;

if(a > 0 || b > 0) break;是多余的,因为你检查这个条件两次:一旦进入while条件第二个内部while循环因此检查相同条件是多余的,因为如果条件成功则自动中断(a或b等于或小于比0)。

你传递的数组没有传递它的大小,你很幸运,如果你设置第一个元素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;
}