Thread: Mario problem

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Mario problem


    Hi I'm trying to create a code that creates a half-pyramid such as the end of mario based upon a height entered by a user.

    I've started the basics, and I want to test it but I keep getting the problem
    "mario.c:7:1: error: expected identifier or '('
    {
    ^
    1 error generated." any help??


    Code would work like this:
    User inputs height 8, but it would be RIGHT ALIGNED:
    Code:
                  ##              
                  ##
                ###
              ####
            #####
          ######
        #######
      ########
    Here is the code, thanks for the help
    Code:
    #include <stdio.h>
    #include <cs50.h>
    
    int main(void);
    {
        printf("Please choose a positive integer for the height of Mario's pyramid\n");
        /*int h is the height of the half-pyramid*/
        int h = GetInt();
        while (h=<0|| h >= 24);
    
        {
        
            for(int space = 9 - h; space < h; space++)
            {
            printf(" ");
            }
        
            for(int hash = h + 1; hash < h; hash++)
            {
            printf("#");
            }    
            
        printf("\n")
        }
        
        else
        {
        printf("Please choose a number between 1 and 23\n");
        int h = GetInt();
        }
       
    }
    The GetInt is a function to get an integer and works with <cs50.h>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    There is no operator =< in C. You probably want <=.

    You have an extra semicolon after the while condition, effectively turning the loop into an infinite loop (and creating a free brace delimited code block).

    You have a dangling else in your code.

    [Edit]Using code tags helps us examine your code and better help you.[/Edit]
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by bdb
    There is no operator =< in C. You probably want <=.

    You have an extra semicolon after the while condition, effectively turning the loop into an infinite loop (and creating a free brace delimited code block).

    You have a dangling else in your code.

    [Edit]Using code tags helps us examine your code and better help you.[/Edit]
    Thank you, what do you mean by a dangling else?

    I still have the error error: expected identifier or '('
    {
    ^
    though
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,696
    Rep Power
    480
    Search your program for "if"
    Search your program for "else"
    (there is an else, there are no ifs.)
    if precedes else otherwise syntax error.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Search your program for "if"
    Search your program for "else"
    (there is an else, there are no ifs.)
    if precedes else otherwise syntax error.
    Thank you, I was under the impression there was a while-else statement.

    Still getting the error though :(

    error: expected identifier or '('
    {
    ^
    1 error generated.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    Originally Posted by Mukilab
    I still have the error error: expected identifier or '('
    {
    ^
    though
    It would help a bit if you identify the line in question (I noticed a message saying line 7: "int h = GetInt();").

    Are you using a C89 compiler (notably Microsoft's Visual Studio)?
    In C89 you cannot have declarations and code intermixed.

    Line 6 is code; line 7 is a declaration (definition with initialization).
    Swap lines 6 and 7.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by bdb
    It would help a bit if you identify the line in question (I noticed a message saying line 7: "int h = GetInt();").

    Are you using a C89 compiler (notably Microsoft's Visual Studio)?
    In C89 you cannot have declarations and code intermixed.

    Line 6 is code; line 7 is a declaration (definition with initialization).
    Swap lines 6 and 7.
    Hi, I am using a virtual engine which is running linux fedora. Not exactly sure what compiler I'm using unfortunately, it was more of a package thing, I didn't have to do too much work myself installing it.

    By line do you mean mario.c:7:1: error:??? However this refers to line 9 in the code I posted since my original code has two lines extra on the top.

    Specifically it refers to { after int main(void) although what could possibly be wrong with that?

    Specifically as in assuming that mario.c:7:1: refers to line 7
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    You have an extra semicolon

    int main(void); <=== THIS SEMICOLON IS WRONG
    {
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by bdb
    You have an extra semicolon

    int main(void); <=== THIS SEMICOLON IS WRONG
    {

    fantastic! thanks
  18. #10
  19. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,696
    Rep Power
    480
    Where a compiler indicates trouble is often caused by a mistake appearing earlier in the program.


    The compiler reads more code than you might think it ought to because it tries hard to follow all the possible parse rules.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo