# Thread: Compiling error... a function-definition is not allowed here before â{â token

1. ## Compiling error... a function-definition is not allowed here before â{â token

I keep getting this compiling error

a function-definition is not allowed here before â{â token

Code:
```#include <iostream>
#include <cstdlib>

using namespace std;

double areaOfCircle (int r);
double lbsToKgs (int lbs);
double mphToFps (int mph);
int coinFlip (void);
int rollDi (void);

int main(void)
{
int done = 0;
char choice;
while ( !done )
{
" Area of Circle"
" Convert lbs to kgs"
" Convert mph to fps"
" Coin flip"
" Roll dice"
"Enter choice (q to quit)? ";
cin >> choice;
switch(choice)
{
case '1':
{
int r;
cout << "Enter positive int radius? ";
cin >> r;
double areaOfCircle (int r)
{
double pi = 3.14159;
double a;
a = pi * r * r;
return a;
}
cout << areaOfCircle(r) << "units squared" << endl;
break;
}
case '2':
{
int lbs;
cout<<"Enter positive int lbs? ";
cin>>lbs;
double lbsToKgs (int lbs)
{
double b;
b = lbs * 0 .45359237;
return b;
}
cout << lbsToKgs(lbs) << "kgs" << endl;

break;
}
case '3':
{
int mph;
cout << "Enter pos int mph? ";
cin >> mph;
double mphToFps (int mph)
{
double c;
c = mph * 1.46666667;
return c;
}
cout << mphToFps (mph) << "fps" << endl;
break;
}
case '4':
{
int coinFlip (void)
{
int f;
f = lrand48() % 2;
if (f ==1)
{
}
else
{
cout << "Tails" <<endl;
}
return f;
}
break;
}
case '5':
{
int rollDi (void)
{
int s, t;
s = (lrand48() % 6) + 1;
t = (lrand48() % 6) + 1;
if(s==1 && t==1)
{
cout << "Snake eyes!" << endl;
}
else
{
cout << s << " " << t << endl;
}
return s,t;
}
break;
}
case 'q':
{
done = 1;
break;
}
default:
cout<< "Invalid Choice try again";
}
}
return 0;
}```

This is the output:
p7.cc: In function âint main()â:
p7.cc:39: error: a function-definition is not allowed here before â{â token
p7.cc:54: error: a function-definition is not allowed here before â{â token
p7.cc:69: error: a function-definition is not allowed here before â{â token
p7.cc:80: error: a function-definition is not allowed here before â{â token
p7.cc:124: error: expected `}' at end of input
p7.cc:124: error: expected `}' at end of input
p7.cc:124: error: expected `}' at end of input
p7.cc:124: error: expected `}' at end of input

I am really confused why these errors are there. Any help would be great. 2. The compiler is being as explicit as possible: you're defining a function where it is not allowed to define a function! Here is a snippet
Code:
```int main(void)
{
int done = 0;
char choice;
while ( !done )
{
" Area of Circle"
" Convert lbs to kgs"
" Convert mph to fps"
" Coin flip"
" Roll dice"
"Enter choice (q to quit)? ";
cin >> choice;
switch(choice)
{
case '1':
{
int r;
cout << "Enter positive int radius? ";
cin >> r;
double areaOfCircle (int r)
{
double pi = 3.14159;
double a;
a = pi * r * r;
return a;
}
cout << areaOfCircle(r) << "units squared" << endl;
break;
}
//.. rest of 'main' here
}```
You're defining this function inside of the function "main". Move the definition outside of this function, and simply call the function from "main". So it may look like:
Code:
```             double areaOfCircle (int r)
{
double pi = 3.14159;
double a;
a = pi * r * r;
return a;
}

int main(void)
{
int done = 0;
char choice;
while ( !done )
{
" Area of Circle"
" Convert lbs to kgs"
" Convert mph to fps"
" Coin flip"
" Roll dice"
"Enter choice (q to quit)? ";
cin >> choice;
switch(choice)
{
case '1':
{
int r;
cout << "Enter positive int radius? ";
cin >> r;
cout << areaOfCircle(r) << "units squared" << endl;
break;
}
//.. rest of 'main' here
}``` 3. thanks! got it Popular pages Recent additions 