function-definition is not allowed here before '{'.

Line 28 & 58 are telling me a function-definition is not allowed here before '{'.
New to programming, i've been researching for a solution and it said i need to declare these variables earlier or something but i tried putting them before main & received various errors so i'm not really sure what else to try. Any & all help is appreciated, an explanation would be great too.

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
  #include <iostream>
#include <fstream>

using namespace std;



int main()
{

    // prompt user to enter file name. also display if it exists or not
    ifstream inputFile; // gives access to file
    string name, filename;

    cout << "Enter the file name: ";
    cin >> filename;

    if (!inputFile) // if it's not the input file - error message
   {
       cout << "Cannot open file.\n"; // error message
       return 1;
   }


    // read contents into integer array

int binarySearch(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;
}






    // sort the array using selection sort
 void selectionSort(int array[], int size){

 int startScan, minIndex, minValue;

 for (startScan = 0; startScan < (size - 1); startScan++)
 {
 minIndex = startScan;
 minValue = array[startScan];
 for(int index = startScan + 1; index < size; index++)
 {
 if (array[index] < minValue)
   {
minValue = array[index];
 minIndex = index;
   }
 }
 array[minIndex] = array[startScan];
 array[startScan] = minValue;
 }
}

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

    // perform a binary search to locate the number, If found display
    // "Number %d Found at Position %d". Where the
    //first %d is the number the user is searching for
    // and the second %d is the location in the SORTED ARRAY.
    //Else display "Number %d Not Found", where you will put the number the user is searching for in place of %d. You can use either printf() or cout.


    // search continues until they hit Q since it's a loop.
    return 0;
}
Last edited on
The overall structure of your program should be something like this, keeping in mind that so far you haven't called the functions inside main which should be your next step as and when you need to use them.

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
#include <iostream>
#include <fstream>

using namespace std;

int binarySearch(int array[], int size, int value); // <--
void selectionSort(int array[], int size); // <--

int main()
{
    
    // prompt user to enter file name. also display if it exists or not
    ifstream inputFile; // gives access to file
    string name, filename;
    
    cout << "Enter the file name: ";
    cin >> filename;
    
    if (!inputFile) // if it's not the input file - error message
    {
        cout << "Cannot open file.\n"; // error message
        return 1;
    }
    
    return 0;
}





int binarySearch(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;
}




void selectionSort(int array[], int size){
    
    int startScan, minIndex, minValue;
    
    for (startScan = 0; startScan < (size - 1); startScan++)
    {
        minIndex = startScan;
        minValue = array[startScan];
        for(int index = startScan + 1; index < size; index++)
        {
            if (array[index] < minValue)
            {
                minValue = array[index];
                minIndex = index;
            }
        }
        array[minIndex] = array[startScan];
        array[startScan] = minValue;
    }
}


Thank you!!, i was trying something very similar and i think i was just missing the semi-colon when declaring binary search & selection sort
Functions sit outside main in your setup otherwise they can only be used once - and albeit not in the usual form because they have a return value (or void) which would make sense. Functions can call functions but that's getting even further off the current track. :)
Topic archived. No new replies allowed.