Home > Mobile >  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."

Time:03-03

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);
        }
    }
}

CodePudding user response:

the main function must be within {} Something like:

string username;
typedef struct
{
   string name;
   string number;
}
phnbk;
    
int main(void)
{
   phnbk conta      
   ....

CodePudding user response:

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;
}

or this

int main(void)
{
    string username;
}

(The two syntaxes are functionally identical, but which one to use is controversial).

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

}
  • Related