What is wrong with this code

I get 2 error messages:
Line 13: "a function-definition is not allowed here before '{' token"
Line 37: "expected '}' at end of input"

I don't understand either error. Can someone explain what I am doing wrong. Thank you.

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

using namespace std;

const string prompt = "Enter Integer: ";

// integer enter and check
void getInt(const string & prompt, int & value)
{  //Line 13
    cout << prompt;
    bool isInteger(string stringToCheck)
    {
        bool integer = false;

        if(stringToCheck.find_first_not_of("0123456789") == string::npos)
            {
                integer = true;
                return integer;
            }
        else
            isInteger(stringToCheck);
    }

    cout << value;
    cin.ignore(1000, '\n');

}


int main()
{
   getInt(const string & prompt, int & value);

    return 0;
}  // Line 37 
As the error message states: You can't define a function inside another function. Try this:
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
  #include <iostream>
#include <string>

using namespace std;

const string prompt = "Enter Integer: ";

 bool isInteger(string stringToCheck)
    {
        bool integer = false;

        if(stringToCheck.find_first_not_of("0123456789") == string::npos)
            {
                integer = true;
                return integer;
            }
        else
            isInteger(stringToCheck); //This won't work, try return integer
    }

// integer enter and check
void getInt(const string & prompt, int & value)
{
    cout << prompt;

    cin >> value;
    if (!isInteger())
       //Do stuff

}


int main()
{
   getInt(const string & prompt, int & value);

    return 0;
}  // Line 37  


Also, try not to view error messages as "My code doesn't work", but as an indication to what is wrong.
@goldenchicken
Thank you for your response.
I understand what you are saying about not being able to define a function within another function.
I note that at line 18 you have noted that what I have tried to do will not work. Perhaps if I explain what I am trying to do, you may be able to help.

The idea is to enter an number at the prompt and check if it is an integer ( I will also want to do something similar with floating point numbers). If it is the program moves on. If it isn't an integer (say the user has entered a letter in error) I want it to simply go back to the prompt so that the user can re-enter the number. It is easy in BASIC but I am finding it not so easy in C++.
With my "This won't work" I meant: What happened in that case was that, when the user entered something that is not an integer, the isInterger function will be called an infinite amount of times, as your code says:
1
2
3
4
5
6
7
bool isInteger(string s)
{
    if(/*Check if integer*/)
        return true;
    else
        isInteger(s) /*This will check again, but as nothing has changed, this will continue to be executed*/
}

Instead you want to do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool isInteger(string s)
{
    if (/*Check integer*/)
        return true;
    else return false;
}

void getInt()
{
    string s = "abc";
    while(!isInteger(s))
    {
        std::cin >> s;
    }
}
Last edited on
Topic archived. No new replies allowed.