Expected primary expression before int error

Hello all, my program is just about done, but i'm receiving an error trying to call my binary search in main? - line 71

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>


using namespace std;

// function prototypes
int binarySearch(int array[], int size, int value);
void selectionSort(int array[], int size);
void swap(int &, int&);

const int SIZE = 200;

int main()
{
    // prompt user to enter file name. also display if it exists or not
    ifstream inputFile; // gives access to file
    string name, filename;
    int number;
    const int SIZE = 200;
    int values[SIZE];
    int results;

    // Get file name from the user
    cout << "Enter the file name: ";
    cin >> filename;

    // Open the file
    inputFile.open(filename.c_str());

    // If the file opened successfully, process it
    if (inputFile)
    {
        // Read the numbers from the file and display them.
        while (inputFile >> number)
        {
            cout << number << endl;
        }

        // Close the file
        inputFile.close();
    }
    else
    {
        // display an error message if the file was not found
        cout << "Error opening the file.\n";

    }

       // display the unsorted array
       cout << "The unsorted values:\n";
       for (auto element : values)
        cout << element << " ";
       cout << endl;

       // sort the array
       selectionSort(values, SIZE);

       cout << "The sorted values are\n";
       for (auto element : values)
          cout << element << " ";
       cout << endl;

      // prompt user to search for the number
      cout << "Enter the number you're looking for or 0 to quit: ";
      cin >> number;

      // search for the number
      results = binarySearch(int size, int value);

      //Searches for # in the array
      if(results == -1)
        cout << "That number does not exist in the array.\n";
      else{
        cout << "That number was found at element " << results;
        cout << " in the array.\n;"
      }

      else if (results == 0){
        exit(EXIT_FAILURE);
      }

   return 0;
}


    // prompt the user to enter a # to search the array or Q to quit


    // swap function
    void swap(int &a, int &b)
    {
        int temp = a;
        a = b;
        b = temp;
    }

    // sort the array using selection sort
    void selectionSort(int array[], int size) // function used outside of main
    {

        int minIndex, minValue; // declaration

        for(int start = 0; start < (size - 1); start++) // search begins from one element below the actual starting point
         {
            minIndex = start;
            minValue = array[start];
            for(int index = start + 1; index < size; index++) // incremented by one value at a time
            {
                if(array[index] < minValue)
                {
                    minValue = array[index]; // locates element in position 0
                    minIndex = index;
                }
            }
            swap(array[minIndex], array[start]);

         }

        }




    // perform a binary search to locate the number, If found display
    int binarySearch(const int array[], int size, int value) //
    {

        int first = 0, // First array element
        last = size - 1, // Last array element
        middle, // Mid point of search
        position = -1; // Position of search value
        bool found = false; // Flag

        while(!found && first <= last)
        {
            middle = (first + last) / 2; // Calculate mid point
            if(array[middle] == value)  // If value is found at mid
            {
                found = true;
                position = middle;
            }
            else if(array[middle] > value)  // If value is in lower half
                last = middle - 1;
            else
                first = middle + 1; // If value is in upper half
        }
        return position;
    }

  
Last edited on
That's not how you call a function.
Have a look at: http://www.cplusplus.com/doc/tutorial/functions/

binarySearch(const int array[], int size, int value) expects three parameters (array of ints, int, int).

But you're passing in two, plus you shouldn't put the type of the variable in when calling:
binarySearch(int size, int value);

Instead, do: results = binarySearch(values, SIZE, number);
Instead of your own swap(), you could use the C++ std::swap() http://www.cplusplus.com/reference/utility/swap/

results = binarySearch(values, SIZE, number);

I wrote that and it gives me an error saying: undefined referance to binarySearch(int*, int, int)
int binarySearch(int array[], int size, int value);

int binarySearch(const int array[], int size, int value)
Last edited on
Thank you! The bottom one worked
Topic archived. No new replies allowed.