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.
#include <iostream>
#include <fstream>
usingnamespace 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;
}
elseif (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;
}
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.
#include <iostream>
#include <fstream>
usingnamespace 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;
}
elseif (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;
}
}
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. :)