Search code examples
cprogram-entry-pointdo-whilecs50function-definition

expected identifier (in C)


I am currently doing CS50 introduction to computer science: I started this code (written in C) where I have to code a pyramid based on what the user writes.

Here is my code :

#include <stdio.h>

#include <cs50.h>

int main(void);

int n;

do

{

    n = get_int("Positive Number: \n");

}

while (n > 0 || n < 9);

Here is the error displayed by my terminal:

mario.c:6:1: error: expected identifier or '('
do
^
mario.c:10:1: error: expected identifier or '('
while (n > 0 || n < 9);
^
2 errors generated.
<builtin>: recipe for target 'mario' failed
make: *** [mario] Error 1

Can someone please help? William


Solution

  • You placed a semicolon after the declaration of the function main

    int main(void);
                ^^^^
    

    Remove it and enclose the body of main in braces

    int main(void)
    {
        //...
    }
    

    Also it seems the condition of the do-while statement

    do
    
    {
    
        n = get_int("Positive Number: \n");
    
    }
    
    while (n > 0 || n < 9);
    

    is incorrect. I suspect that you want to repeat the loop if the entered value of n is not positive or is greater than or equal to 9. In this case the condition should look like

    do
    {
        n = get_int("Positive Number: \n");
    } while ( !( n > 0 && n < 9 ) );