1

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);
        }
    }
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
Muhsin
  • 27
  • 3
  • 2
    Something missing after `int main(void)` – ikegami Mar 03 '22 at 06:21
  • 2
    You can't declare Main without parentheses or ; you wrote int main(void) // no ; or {} afterwards – Omer Kawaz Mar 03 '22 at 06:22
  • 2
    The single line containing `int main(void)` should be moved down after the line closing the struct alias `phnbk;` and before the `{` that follows. That single change should allow this to compile, though whether it runs correctly or not is unrelated. – WhozCraig Mar 03 '22 at 06:46
  • This is one reason why it is actually required (!) to produce a [mcve]. At some point, I'm sure you would have found that typo yourself! – Ulrich Eckhardt Mar 03 '22 at 15:59

2 Answers2

3

the main function must be within {} Something like:

string username;
typedef struct
{
   string name;
   string number;
}
phnbk;
    
int main(void)
{
   phnbk conta      
   ....
Mike
  • 4,041
  • 6
  • 20
  • 37
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

}
tjcaul
  • 383
  • 1
  • 9
  • 2
    _"(The two syntaxes are functionally identical, but which one to use is controversial)":_ There are no two syntaxes here, just two different formatting styles. – Jabberwocky Mar 03 '22 at 08:21