Search code examples
ccs50

There is an issue that I cannot find as I have just started coding with the cs50 class and something isn't right here


Here is my code:

#include <cs50.h>
#include <stdio.h>
int main(void);
{
    string name = get_string("What's your name? \n")
}
{
    printf("hello, %s\n", name);
}

The compiler output these errors:

me/ $ make hello
hello.c:4:1: error: expected identifier or '('
{
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
2 errors generated.
make: *** [‹builtin›: hello Error 1
me/ $ make hello
hello.c:4:1: error: expected identifier or '('
{
^
1 error generated.
make: *** [‹builtin>: hello Error 1 
me/ $ ^C
me/ $ make hello
hello.c:4:1: error: expected identifier or '('
{
^
1 error generated.
make: *** [‹builtin>: hello Error 1 me/ $

I have tried changing my ; and {} and () but nothing has helped, I consulted the internet but couldn't find a fix anywhere, I would appreciate any troubleshooting or potential fixes, I really want to learn coding so I hope I am not just dumb and blind.

Edit: Thank you for the comments, I didn't realize I could have both in the same set of braces I appreciate the help.


Solution

  • There are multiple problems:

    • The definition of function main should not have a ; after the argument list but a function body as a single block of statements surrounded by { and }.

    • individual C statements and definitions end with a ;.

    Here is a modified version:

    #include <cs50.h>
    #include <stdio.h>
    
    int main(void)
    {
        string name = get_string("What's your name?\n");
    
        printf("hello, %s\n", name);
    }