Linker error, passing an array

Hi, I am receiving a linker error when trying to run the program. It has to do with the array I am trying to pass from main into my search function. I know it's probably something I have overlooked. Any assistance and outside views would be appreciated.

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  #include <iostream>
using namespace std;

int search();
   
int main()
{
   int winLose;
   int lottoNumber;
   
   int userNumber[10];
   userNumber[0] = 13579;
   userNumber[1] = 26791;
   userNumber[2] = 26792;
   userNumber[3] = 22445;
   userNumber[4] = 55555;
   userNumber[5] = 62483;
   userNumber[6] = 77777;
   userNumber[7] = 79422;
   userNumber[8] = 85647;
   userNumber[9] = 93121; 
   
   cout << "Enter the winning lottery Number: ";
   cin >> lottoNumber; 
   search ();
    
   if(winLose == 1){
              cout << "Congratulations! You have won!" << endl;
              }//end if
   else{
        cout << "I'm sorry, you have not won this time." << endl;
        }//end else
    system("pause");
    return 0;   
}//end main



//Linear Search
int search(int lottoNumber, int userNumber[])
{

    int winLose;
    
       for(int i=0; i<10; i++){
               
             if (lottoNumber == userNumber[i]){
                             winLose = 1;
                            
                             }//end if
             else{
                 winLose = 0;
                  }//end else
             
             }//end for
    return winLose;
}//end search
.
Hi MrBrewski99,

You have a linker error because the compiler cannot find a definition for the search function with no arguments. Your search function takes 2 ints as arguments.

As well as that, you did not assign the result to anything - it is supposed to return an int.

The variable winLose would be better as a bool type, use true or false rather than 1 or 0.

With your closing braces, put them directly under the statement that the compound statement belongs to:

27
28
29
30
31
32
   if(winLose == 1) {
              cout << "Congratulations! You have won!" << endl;
   }//end if
   else {
        cout << "I'm sorry, you have not won this time." << endl;
   }//end else 


This makes it much easier to read.

Hope all is well
Last edited on
Replace line 25:
search();
with
search(lottoNumber, userNumber);
winLose = search(lottoNumber, userNumber);

Also, I should have said be careful with variable names - the winLose in main is not the same as the winLose in the search function. Not a problem here, but could cause you grief if not careful.
Yeah, thanks guys...I was able to get it to work. It was just one of those things I couldn't see after sitting here and looking at it for awhile.
Topic archived. No new replies allowed.