Thread: Problems with Mario Pyramide

  1. #1
    Registered User
    Join Date
    Jun 2016
    Location
    Germany
    Posts
    4

    Question Problems with Mario Pyramide

    Hey guys,

    I recently started the cs50x course online and try to learn c because I haven't got any programming courses in school atm.

    I don't want anyone here to solve the courses homework for me but I'm stuck with an error I don't really get...

    The task was to get an integer from 1-23 from the user and then to build a pyramide like this:


    height: 8
    ##
    ###
    ####
    #####
    ######
    #######
    ########
    #########


    So I wrote this code:
    Code:
    #include <stdio.h>
    #include <cs50.h>
    
    int main()
    {
        printf("Please enter the pyramid's height:\n");
        int height = GetInt();
        
        while (height < 1 || height > 23)
        {
            printf("Please enter a positive Integer not bigger than 23:\n");
            height = GetInt();
        }
        
        for(int height2 = height , int b = 2 , int c = 6 ; height2 > 0 ; height2-- , b++ , c--)
        {
            
            for(int leerzeichen = (height (+1 -b));leerzeichen > 0;leerzeichen--)
            {
                printf(" ");
            }
            
            for(int rauten = (height (+1 - c)); rauten > 0; rauten--)
            {
                printf("#");
            }
            printf("\n");
           
        }
        
    
        
          
    }
    But I get this goddamn error:

    ~/workspace/pset1 $ make mario
    clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wshadow mario.c -lcs50 -lm -o mario
    mario.c:15:32: error: expected identifier or '('
    for(int height2 = height , int b = 2 , int c = 6 ; height2 > 0 ; height2-- , b++ , c--)
    ^
    mario.c:15:32: error: expected ';' in 'for' statement specifier
    mario.c:15:32: error: expected expression
    mario.c:15:68: error: expected ')'
    for(int height2 = height , int b = 2 , int c = 6 ; height2 > 0 ; height2-- , b++ , c--)
    ^
    ~/workspace $ make mario
    make: *** No rule to make target `mario'. Stop.
    ~/workspace $ cd pset1
    ~/workspace/pset1 $ make mario
    clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wshadow mario.c -lcs50 -lm -o mario
    mario.c:15:32: error: expected identifier or '('
    for(int height2 = height , int b = 2 , int c = 6 ; height2 > 0 ; height2-- , b++ , c--)
    ^
    mario.c:15:32: error: expected ';' in 'for' statement specifier
    mario.c:15:32: error: expected expression
    mario.c:15:68: error: expected ')'
    for(int height2 = height , int b = 2 , int c = 6 ; height2 > 0 ; height2-- , b++ , c--)
    ^
    mario.c:15:8: note: to match this '('
    for(int height2 = height , int b = 2 , int c = 6 ; height2 > 0 ; height2-- , b++ , c--)
    ^
    mario.c:15:64: error: relational comparison result unused [-Werror,-Wunused-comparison]
    for(int height2 = height , int b = 2 , int c = 6 ; height2 > 0 ; height2-- , b++ , c--)
    ~~~~~~~~^~~
    mario.c:15:70: error: use of undeclared identifier 'height2'; did you mean 'height'?
    for(int height2 = height , int b = 2 , int c = 6 ; height2 > 0 ; height2-- , b++ , c--)
    ^~~~~~~
    height
    mario.c:7:9: note: 'height' declared here
    int height = GetInt();
    ^
    mario.c:15:82: error: use of undeclared identifier 'b'
    for(int height2 = height , int b = 2 , int c = 6 ; height2 > 0 ; height2-- , b++ , c--)
    ^
    mario.c:15:88: error: use of undeclared identifier 'c'
    for(int height2 = height , int b = 2 , int c = 6 ; height2 > 0 ; height2-- , b++ , c--)
    ^
    8 errors generated.
    make: *** [mario] Error 1


    It would be great if you could help me, because I really don't know
    what the hell I did wrong

    Lovely greetings from Germany,
    Polarsturm (Johannes)

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You have too many "int"s in your for loop. Once you say int, that means all the new variables in your list are int:
    Code:
    for (int height2 = height, b = 2, c = 6; height2 > 0; height2--, b++, c--)
    You are also limited to one type of variable in the for loop header. It doesn't matter if it's int, pointer, or something else, but you can only use one type.

    Actually, pointers can get complicated, so here's an example:
    Code:
    #include <stdio.h>
     
    int main(void)
    {
        char text[] = "Example.";
        for (char *ptext = text, *end = &text[8]; ptext < end; ptext++)
            printf("%2x ", *ptext);
    }
    Last edited by whiteflags; 06-28-2016 at 09:59 AM.

  3. #3
    Banned
    Join Date
    Oct 2014
    Location
    Home
    Posts
    135
    Chua Me O! It's a lot trickier than you tink. The order is extremely important depending on the compiler you use. So for instance:

    Code:
    char value;
    value = 'A'
    if NOT the same like
    Code:
    value = 'A'
    char value

  4. #4
    Registered User
    Join Date
    Jun 2016
    Location
    Germany
    Posts
    4
    Thank you both very very much!
    I'm all new with programming and didn't know this

    I had to do a few more changes but now I almost get it running how it should ^^

  5. #5
    Registered User
    Join Date
    Jun 2016
    Location
    Germany
    Posts
    4
    I don't know if anybody cares, but I'm a little bit proud I got it working, so here's the correct code ^^
    I think that I'll can learn a lot from this forum

    Code:
    #include <stdio.h>
    #include <cs50.h>
    
    int main()
    {
        printf("Please enter the pyramid's height:\n");
        int height = GetInt();
        
        while (height < 1 || height > 23)
        {
            printf("Please enter a positive Integer not bigger than 23:\n");
            height = GetInt();
        }
        
        for(int height2 = height , b = 2 , c = height +1 -2 ; height2 > 0 ; height2-- , b++ , c--)
        {
           
            for(int space = height +1 -b; space > 0; space--)
            {
                printf(" ");
            }
            
            for(int rauten = height +1 - c; rauten > 0; rauten--)
            {
                printf("#");
            }
            printf("\n");
           
        }
        
    
        
          
    }

  6. #6
    Banned
    Join Date
    Oct 2014
    Location
    Home
    Posts
    135
    Jesus! Chua Me O! That's great. I don't understand it because my compiler spits out errors because I don't have the cs50 header. Also my compiler expects you to define everything upfront instead of as you go. But congratulations. I think people learn best when they are not pressured. You need room to experiment on your own time. Then again, others need structure and a routine or else they fall through the cracks............

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    A bit over engineered for my taste.
    Code:
    void mario (int height)
    {
        int length = 2;
        while (height-- > 0) {
            int c;
            for (c = 0; c < length; ++c) {
                putchar('#');
            }
            putchar('\n');
            ++length;
        }
    }

  8. #8
    Registered User
    Join Date
    Jun 2016
    Location
    Germany
    Posts
    4
    Thank you Tien Yes I'll definitely need some time to learn programming ^^

    And whiteflags my version is indeed a bit over engineered
    Why make it complicate if you can keep it short and simple.

  9. #9
    Banned
    Join Date
    Oct 2014
    Location
    Home
    Posts
    135
    Quote Originally Posted by Polarsturm View Post
    Thank you Tien Yes I'll definitely need some time to learn programming ^^

    And whiteflags my version is indeed a bit over engineered
    Why make it complicate if you can keep it short and simple.
    @Polarsturm: I'm no hotshot. Far from it. What I've learned is nothing compared to all the knowledges out there. I plan on keeping learning programming while I'm alive. Although my age has made me slow in learning these days I do actually read 1 page a day. I once heard old people tell me it is harder for them to study than when young. Now I see it is true. Back in the old days when I was just a kid I could study 5 textbooks (30 pages each) per book a day. Now I'm down to 1 on average per day. Serious. At least there is a theory that you can get it done by the time you pass if you keep at it. My real dad told me to be consistent and my real mom told me to keep working. So thank you to my real mom Huong Thi Thuyen Vu and to my real dad Nguyen Binh Thuy. My two sisters followed their advices and succeeded while I didn't and I'm way behind with outdated technology. Thank you Jesus Christ.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Happy Birthday Mario....
    By CommonTater in forum General Discussions
    Replies: 7
    Last Post: 08-24-2011, 09:55 AM
  2. Have you played Mario 2?
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 07-23-2006, 09:33 AM
  3. Pointers and gaming - MARIO
    By swgh in forum Game Programming
    Replies: 9
    Last Post: 02-24-2006, 04:32 PM
  4. Programming Mario
    By Coder87C in forum Game Programming
    Replies: 3
    Last Post: 12-20-2004, 08:15 AM

Tags for this Thread