0

Here's one of my attempts at creating the mario.c problem set...it won't compile. Can anyone give me a hint as to where I'm going wrong? (prob more than one error)

I wrote another one that didn't have the separate "blocks(int n)" or "space(int n)" functions, and it would only print the pyramid backwards...

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

void say(string s, int n);
void blocks(int n);
void space(int n);
int main(void)
{
    printf("Mario, tell me how many levels you want your pyramid to be...0 to 23\n");
    int height = get_int();
    while (height < 0 || height < 24)
    {
        printf("no, you dingus, I said 'between 0 and 23\n");
        //is this necessary? -> int height = get_int();
    }
    for (int i = 0; i < height; i++)
    {
        for (int i = 1; i <= height; i++)
        {
            space(int n);
        }
        for (int i = 1; i <= height; i++)
        {
            blocks(int n);
        }
    }
}
void space(int n)
{
    say(" ", height - n);
}
void blocks(int n)
{
    say("#", n + 1);
}
void say(string s, int n)
{
    for (int i = 0; i < n; i++)
    {
        printf("%s", n);
    }
}

1 Answer 1

3

There are a number of issues, some of which I'll point out. Others, I'll leave for you to sort through. Learning to debug is an important skill to learn and this will be a good exercise for you. ;-)

First, the code is using the same variable, i, to control the nested for loops. This generally won't work and is a bad idea. It generates shadow var warnings from the compiler, which are treated as errors in the IDE. You need to use different vars for nested for loops.

Next, when the code tries to call functions, it shouldn't use a type identifier as you would when writing the signature. For example, blocks(int n); is incorrect because of int, but blocks(n); would be correct.

Following this, these calls will fail because n doesn't exist in main. To call any of these functions, the value to be passed must exist somewhere.

This will get you going. I recommend that you pursue these fixes and then try to work through whatever follows. You should focus on fixing the errors in the order given from the compiler. Often one bug can generate multiple errors. By fixing the first of the related errors, it can eliminate the later dependent errors.

Finally, there's a logic error lurking. I'll leave it to you to find it. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thank you Cliff B!
    – Macas
    Jul 18, 2017 at 19:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .