0

what in the world is wrong with my code:

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


int main(void)

{
int n;
    do
    {
    // prompt user for a number 0 to 23


    n = get_int();
    printf ("Please type a number from 0-23 ,%n\n", n);

    }

    while(n<0 || n>23);
    for(int x=0; x<n; x++)

    {
    //print spaces
    int i=0;
    while (i < n-x-1)
    {
    printf(" ");
    i++;
    }
    // print hashes
    int j=0;
    while (j< x+1)
    {
    printf("#");
    j++;
    }
    // print new line
    printf("\n");
    }


    }
1
  • What seems to be the problem? Please describe the issue that you are having and we can focus on it. Does it compile? Does it run? What is wrong with the output? Please edit the question and add detail and examples. One problem, one question. – Cliff B Mar 23 '18 at 1:53
0

Before the answer, a few housekeeping things:

  1. Don't post huge chucks of your solution to any pset on the internet (a few lines may be ok - just kind in mind reasonable vs. unreasonable behaviour). Although it's true you are showing people so they may help you, remember the people who come to find answers.
  2. Follow the guides on how to ask a question. It makes it much easier for people to read parts of the question (e.g. your code), and what your exact issue/problem/question is (e.g. compiling issue, logical error etc).

Your first issue is style and readability. I've re-written you're code so that it's easiesr for me to read, and as an example of the styling cs50 prefers:

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


int main(void)
{
    // Extra comment here
    int n;
    do
    {
        // Prompt user for a number 0 to 23
        n = get_int();
        printf ("Please type a number from 0-23 ,%n\n", n);
    }
    while(n<0 || n>23);

    // What's happening here?
    for(int x=0; x<n; x++)
    {
        //print spaces
        int i=0;
        while (i < n-x-1)
        {
            printf(" ");
            i++;
        }
        // print hashes
        int j=0;
        while (j< x+1)
        {
            printf("#");
            j++;
        }

        // print new line
        printf("\n");
    }
}

Style is important if people are going to read you're code (including yourself - nothing worse than returning to a project a week later and not remembering what the heck some code does). The ide50 command style50 can help with that.

I assume the issue you're talking about is an issue at compile because I compiled you're code verbatim and got this message:

a.c:15:53: error: format specifies type 'int *' but the argument has type 'int' [-Werror,-Wformat]
    printf ("Please type a number from 0-23 ,%n\n", n);
                                             ~~     ^

This is one issue. You've mixed up a couple ideas here. When substituting variable in printf, within the string you put a placeholder (formally called a specifier) that describes a little bit about the variable, then you provide the variables in the order you want to substitute them. What you have here is the placeholder as the name of the variable, which the compiler doesn't understand. Coincidentally, %n tells printf you want to plug in an address in memory, which n is not. The specifier (placeholder) you're looking for is %i for an integer. See if you can see the difference below:

int age = 64;

printf("I am %age years old", age); // Won't compile, wrong, broken, null, don't do this

printf("I am %i years old", age); // This is how it should be

Do look at get_int()s usage, because logically speaking, the lines inside the do-while loop are incorrect (Hint: printf isn't needed).

There is an error in your output, but I think you can figure it out. Hint:

   ##
  ###
 ####

Finally, there is an optimisation with two of your loops. Just remember that there is a loop that initialises a variable (like i or j); checks a condition (like i < n or j < x); and then runs code that changes the counter variable (i++, or j++, just as some random, non-specific examples wink, wink) instead of taking three different lines of code.

All that said, enjoy the challenge! Play around, tinkering with the code till it works. And don't be afraid to keep asking questions here! Just remember the housekeeping stuff at the start.

You must log in to answer this question.

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