Compiling error... a function-definition is not allowed here before ‚{‚ token

This is a discussion on Compiling error... a function-definition is not allowed here before ‚{‚ token within the C++ Programming forums, part of the General Programming Boards category; I keep getting this compiling error a function-definition is not allowed here before ‚{‚ token Code: #include <iostream> #include <cstdlib> ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    3

    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 )
       {
          cout << "Menu"
                  "[1] Area of Circle"
                  "[2] Convert lbs to kgs"
                  "[3] Convert mph to fps"
                  "[4] Coin flip"
                  "[5] 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)
                   {
                      cout << "Heads" <<endl;
                   }
                   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. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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 )
       {
          cout << "Menu"
                  "[1] Area of Circle"
                  "[2] Convert lbs to kgs"
                  "[3] Convert mph to fps"
                  "[4] Coin flip"
                  "[5] 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 )
       {
          cout << "Menu"
                  "[1] Area of Circle"
                  "[2] Convert lbs to kgs"
                  "[3] Convert mph to fps"
                  "[4] Coin flip"
                  "[5] 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. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    3
    thanks! got it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 02:29 PM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 11:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 10:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21