Johanna
Johanna

Reputation: 89

How to solve error: expected identifier or '('

I've got a problem with something I'm programming. I get this error over and over;

jharvard@appliance (~/Dropbox/pset1): make mario
clang -ggdb3 -O0 -std=c99 -Wall -Werror    mario.c  -lcs50 -lm -o mario
mario.c:23:5: error: expected identifier or '('
    do
    ^
mario.c:32:1: error: expected identifier or '('
do
^
2 errors generated.

I searched all over the internet but couldn't find the problem.. removing the ; after int main(void) didn't help

This is my code:

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

int main(void);

    //Ask User for Height, and check

    int a, b, rows, height;
    int a = 0;
    int b = 0;
    int rows = 1;   

    do
    { 
        printf ("Height: ");
        height = GetInt();
    }
    while (height <=0 || height > 23);   

    //build half pyramid

    do
    {
        do
        {
            printf("r");
            a++;
        }
        while (a < height - rows);

        do
        {
            printf("#");
            b++;
        }
        while (b < rows + 1);


        printf("\n");
        rows++;

        while (rows <= height);
    }

I've been trying to solve this problem for a few days, but i just can't figure it out!

Thank you so much in advance!

Upvotes: 5

Views: 171188

Answers (7)

Rahul Shakya
Rahul Shakya

Reputation: 1425

In case of react native project .... its a simple issue.

You can go to you project target in ios and check Build, version and build identifier... they might have extra space on the end which should be removed

Hope it helps. Worked for me

Upvotes: -1

Voicu
Voicu

Reputation: 17860

If you don't indent your code, which you (by all means) should do, at least write the starting and the ending curly brackets at once when you write the loop statement, before putting any code into that loop's body (which goes between the curly brackets). It will save you from troubles like these in the future.

Upvotes: 1

dreadlock
dreadlock

Reputation: 106

You got nested loop with do/while. Make sure that each start with do end with while.

Look like at the end of file, the "while" is not correct.

printf("\n");
rows++;

while (rows <= height);
}

That could be you missing the close '}' before 'while (rows <= height);'

Correct code could be:

int main(void)
{

    //Ask User for Height, and check

    int a, b, rows, height;
    a = 0;                    // <- removed int
    b = 0;                    // <- removed int
    rows = 1;                 // <- removed int

    do
    { 
        printf ("Height: ");
        height = GetInt();
    }
    while (height <=0 || height > 23);   

    //build half pyramid

    do
    {
        do
        {
            printf("r");
            a++;
        }
        while (a < height - rows);

        do
        {
            printf("#");
            b++;
        }
        while (b < rows + 1);


        printf("\n");
        rows++;
    }                             // <- add }
    while (rows <= height);
}

Upvotes: 6

Sunil Bojanapally
Sunil Bojanapally

Reputation: 12658

int a, b, rows, height;
int a = 0;
int b = 0;

Here in your above statements a and b are re-declared more than once causing compilation error.

Upvotes: 2

Konstantin Dinev
Konstantin Dinev

Reputation: 34905

Take the last while outside of the do scope:

while (rows <= height);

Upvotes: 1

Blastcore
Blastcore

Reputation: 370

Main post is edited, so clear answer.

All your code is outside of a function because you're doing int main(); you're declaring a function. Use {} brackets instead.

int main() {
    //Code here.
}

Upvotes: 2

Alok Save
Alok Save

Reputation: 206586

int main(void);

You have just declared the main. You need to define it and add the code inside that definition.

int main()
{
   .....
}

Upvotes: 12

Related Questions