gpt4 book ai didi

compiler-errors - C++从 'int'到 'int*'的无效转换

转载 作者:行者123 更新时间:2023-12-02 10:57:35 26 4
gpt4 key购买 nike

我知道我的问题出在第25行和第30行之内,我似乎无法解决该问题。

我尝试在LinearInfo功能内部声明“搜索”,但没有成功。我尝试在声明中添加“const”,并将第30行的“[AMOUNT]”位置保留为空白。每次更改一件事,我都会抛出另一错误代码。如果有人能在正确的方向上为我指明正确的方向,我将不胜感激!旁注:我知道我的代码可能看起来有些困惑,但这是我在书中遵循的类要求,并编写类的代码,而上次我跳过时对它进行了惩罚,因此它必须像在大多数情况下。再次再次感谢您提供的任何建议。

#include <iostream>
#include <vector>
#include <string>

using namespace std;

//defining my variables
const int AMOUNT = 10;
int luckyNumbers[AMOUNT] = {13579, 26791, 26792, 33445, 55555, 62483, 77777, 79422, 85647, 93121};
int weeklyNumber;

//function that asks the user to enter the winning lottery number
int main()
{
cout << "Please enter this weeks winning lottery number." << endl;
cin >> (weeklyNumber);

cout << endl << endl << "The number you entered was: " << endl;
cout << weeklyNumber;

return 0;
}

//defining my prototype
int Search(int[ ], int);

//beginning of function to assign the information for my linear search
int linearInfo()
{
int winningNumber[] = {13579, 26791, 26792, 33445, 55555, 62483, 77777, 79422, 85647, 93121};

//search the array for the users input
winningNumber = Search(luckyNumbers[AMOUNT], weeklyNumber);

//setting the result to false if the users input was not a lucky number
if (winningNumber == -1)
cout << "You did not win the lottery." << endl;

//setting the result to true if the users input was one of the lucky numbers
else
{
cout << "YOU WON THE LOTTERY!!" << endl <<endl;
}
}

//beginning of fuction for the linear search
int winningNumber(const int arr[], int luckyNumbers, int value)
{
int index = 0;
int position = -1;
bool found = false;

while (index < luckyNumbers && !found)
{
if (arr[index] == value)
{
found = true;
position = index;
}

index++;
}

return position;
}

我期望该代码允许我使用线性搜索来找出用户每周中奖彩票号码的输入是否在luckyNumbers列表内。

我收到错误代码“从'int'到'int *'的无效转换”和“初始化'int Search(int *,int)'的参数1”。

最佳答案

您将Search的第一个参数声明为int *int Search(int [], int);,然后尝试使用intwinningNumber[AMOUNT]调用它,这也是一个越界数组索引(AMOUNT为10,数组中只有10个元素带有索引0..9)。

关于compiler-errors - C++从 'int'到 'int*'的无效转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57881789/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com