I get an error at line 23: A function-definition is not allowed before "{" token, and I don't understand why this is illegal. The other error I get is on line 36: expected '}' at the end of input and as far as I can tell all my parentheses seem to be closed. I would appreciate the help thanks.
#include <iostream>
#include <vector>
#include <fstream>
#include<string>
usingnamespace std;
int main()
{
int input;
vector<int> gameBoard;
cout << "Please enter the numbers of a game board on one line, separating numbers by spaces,then press the Enter key." << endl ;
while (cin >> input){
gameBoard.push_back(input);
}
int cost (int index)
{
if (index == gameBoard.back())
return gameBoard.back();
}
else
{
int costOne = cost (index + 1);
int costTwo = cost (index + 2);
return min(one,two) + gameBoard[index];
}
}
First, you have mispaired curly braces - Your curly brace on line 26 matches up with the one on 23, so you have an else statement with no if before it.
You need to define your int cost(int index) function outside of main().
1 2 3 4 5 6 7 8 9 10 11 12
int cost(int index)
{
// ...
return some_number_based_on_index;
}
int main()
{
// ...
int my_cost = cost(some_index);
// ...
}
You are missing clams and you cannot as ^says, define a function within a function. I recommend you to use visual studio since it reminds you of both theese errors by itself.
Oh wow I don't know why I didn't catch that :P. Thanks guys. Since I moved my function I will need the declaration in the main and to have it use the gameBoard parameter I would just have to call it in the main function right?