我知道我的问题出在第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”。
答案 0 :(得分:0)
您使用Search
将int *
的第一个参数声明为int Search(int [], int);
,然后尝试使用int
值(winningNumber[AMOUNT]
)来调用它也是一个越界数组索引(AMOUNT
是10,该数组只有10个元素的索引为0..9)。