'I,m unable to figure out the error "expected ";" after top-level indicator."

I,m unable to figure out the error " expected ";" after top-level indicator". I cant understand the error.

ERROR test.c:5:15: error: expected ';' after top level declarator int main(void) ^ ; fatal error: too many errors emitted, stopping now [-ferror-limit=] 2 errors generated. make: *** [: test] Error 1

#include<stdio.h>
#include<cs50.h>
#include<string.h>

int main(void)
string username;


typedef struct
{
   string name;
   string number;
}
phnbk;

{
   phnbk contact[2];
   contact[0].name = "david";
   contact[0].number = "123456789";

   contact[1].name = "victor";
   contact[1].number = "9987654321";

   username = get_string("enter your name: ");

    for(int i = 0 ; i < 2; i++)
    {
        if(strcmp(contact[i].name,username) == 0)
        {
            printf("your number is %s" , contact[i].number);
        }
    }
}


Solution 1:[1]

Functions cannot be defined without braces ({}). main() is no exception, which is causing the error.

Therefore, you must define your main function like this:

int main(void)
{
    string username;
}

You have another block of code in {} later, outside of any function, which is not allowed (citation needed). You likely meant to include that code in main(), like this:

#include<stdio.h>
#include<cs50.h>
#include<string.h>

typedef struct
{
   string name;
   string number;
}
phnbk;

int main(void)
{
   string username;
   phnbk contact[2];
   contact[0].name = "david";
   contact[0].number = "123456789";

   //Other main() stuff

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1