Need a little help debugging

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.

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

using namespace 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];
        }
        
     
}
Last edited on
You can't define functions within functions.

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);
    // ...
}
Last edited on
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?
Either define the function before main or announce them in the top and declare them below main. And you call the function in main yes.
Last edited on
Topic archived. No new replies allowed.