类型为“ int”的参数与类型为“ int *”的参数不兼容

时间:2019-03-29 22:24:39

标签: c++

目标是搜索数组并在其中找到一个特定的数字并返回位置。我有一个方法来解决这个问题,但是试图让它在main方法中运行会给我标题中的错误。我该怎么办?

  mean_mpg
1     20.1

期望它应该没有错误地运行并在数组中找到数字的位置,但是我只是遇到错误而无法运行它。

2 个答案:

答案 0 :(得分:-1)

在main()中的for()循环之后,您需要以下内容:

cout << "Enter the value to search for: ";
cin >> x; 

wait = searchArray(a, x);

cout << x;
cout << " is at position: ";
cout << wait;

答案 1 :(得分:-2)

int searchArray(int a[], int x) 
{
    for (int i = 0; i < 100; i++) //change a[100] to 100 because it only needs the size not the array itself
    {
        if (x == a[i])
            return i + 1;
        break;
    }
    return -1;
}

int main()
{
    int wait, x, y, a[100];

    cout << "Enter the size of the array(1-100): ";
    cin >> y;

    for (int i = 0; i < y; i++) {
        cout << "Enter an array of numbers:";
        cin >> a[i];
    }

    cout<<"Number to look for: "; //sets a value for x
    cin>>x;

    cout<<"Index: "<<searchArray(a, x)<<endl; //function returns an int therefore a cout is needed

    cin >> wait;

    return 0;
}