error: a function-definition is not allowed here before '{' token

Nov 22, 2016 at 7:33pm
Write your question here.

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
#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    cout << "Test with variants.\nChoose a,b,c,or d as the answer by typing the coresponding letter.\n";
    Sleep(3000);
    cout <<"Get ready for the first question.You have 5 seconds to answer\n";
    Sleep(5000);
    cout<<"Question one:\n What's 48+52?\n";
        int timer()
        {
            
        
            int a=5;
                while(a>=0)
                    cout<<a<<","<<--a;
        }
    cout<<"a=96\n";
    cout<<"b=100\n";
    cout<<"c=104\n";
    cout<<"d=98\n";
    int ans1,a,b,c,d;
    cin>>ans1;
    Sleep(1000);
        if(ans1=a)
        {
            cout<<"Correct,the answer is 96.\n";
        }
        else
        {
            cout<<"Wrong.The right answer was 96\n";
        }



I would like to make a test where you insert one of the given variants,in 5 seconds.I fail when I try to add the timer.The error is <<a function-definition is not allowed here before '{' token>>

EDIT:Forgot to say that if the time runs out ,the message <<Question skipped>> to appear and to automatically skip to second question.
Last edited on Nov 22, 2016 at 7:38pm
Nov 22, 2016 at 7:54pm
Line 12: What is this line? Is it an int declaration? A function call? Or a function prototype?
The compiler thinks you're trying to define a nested function, which is not allowed.
Nov 22, 2016 at 7:57pm
Lines 12-19 seem to contain a definition of a function, which takes no arguments, has name "timer" and returns an int value.

The function definition is inside the body of an another function. That is not legal. (C++11 did add an exception though: lambda functions.)
Nov 22, 2016 at 8:04pm
I wrote that function outside of main function,how do I use it in main after line 11?
Last edited on Nov 22, 2016 at 8:04pm
Nov 22, 2016 at 8:17pm
Simply make a function call to it.
 
  timer();


BTW, your function appears to be an int function, but does not return anything.
Last edited on Nov 22, 2016 at 8:18pm
Nov 22, 2016 at 8:20pm
What exactly should it return?I'm a starter here,it's my second year of study in c++....
Nov 22, 2016 at 8:33pm
There doesn't appear to be any value that it is trying to return to the caller.
If that's the case, then make the type of the function void.
Topic archived. No new replies allowed.