-1
jharvard@appliance (~/Dropbox/pset1): make mario
clang -ggdb3 -O0 -std=c99 -Wall -Werror    mario.c  -lcs50 -lm -o mario
jharvard@appliance (~/Dropbox/pset1): check50 2014/x/pset1/mario mario.c
:) mario.c exists
:) mario.c compiles
:) rejects a height of -1
:( handles a height of 0 correctly
   \ expected an exit code of 0, not output of "##"
:( handles a height of 1 correctly
   \ expected output, but not "##\n       ###\n      ####\n"
:( handles a height of 2 correctly
   \ expected output, but not "##\n       ###\n      ####\n"
:( handles a height of 23 correctly
   \ expected output, but not "##\n       ###\n      ####\n"
:( rejects a height of 24
   \ expected prompt for input, not exit code of 0
:) rejects a non-numeric height of "foo"
:) rejects a non-numeric height of ""
https://sandbox.cs50.net/checks/d7edbb66e9b94346accb91ee8b13e9e4
jharvard@appliance (~/Dropbox/pset1): 

Part of my mario.c listing

for (i = 0; i < 8; i++)
    printf(" ");

for (i = 0; i < 2; i++)
    printf("#");

for (m = 0; m < 1; m++)
    printf("\n");

for (i = 0; i < 7; i++)   
    printf(" ");

for (i = 0; i < 3; i++)
    printf("#");

    printf("\n");

for (i = 0; i < 6; i++)
    printf(" ");

for (i = 0; i < 4; i++)   
    printf("#");

    printf("\n");      

When I tried use printf("\n") to move to the next line , I have \n printed in my output instead of moving to the next line. I tried using a single line for loop to move to the next line. Still it prints a \n instead of new line. Obviously I am doing something wrong.

I looked at the earlier questions on this topic listed below. I am going round in circles with this and some other input logical error. Can somebody help me?

For Honor code reasons, I listed only part of my code. If somebody requires my complete listing to help me, if they have a reddit account or private Email, I can send my complete listing of the code to them.

Earlier questions on the similar topic listed below:

1
  • 3
    Please learn more about post formatting here to format your posts properly before asking/answering! Thank you!
    – kzidane
    Commented Jul 30, 2014 at 20:57

2 Answers 2

3

Running the program: check50 should only be used to check your program before submitting it. To test your program while you're working on it, first run make to compile the program, then run mario to run it:

    jharvard@appliance (~/Dropbox/pset1): make mario
    # lots of messages appear here
    jharvard@appliance (~/Dropbox/pset1): ./mario

This will let you see the actual output which is easier to debug, instead of just having to rely on error messages from check50.

The literal \n displaying on the screen is just the output from check50 telling you what your program is outputting. If you run ./mario from the command line instead, you will see the correct behaviour.

I have added comments your existing code below to clarify what it is actually doing currently:

// display first line

// print 8 spaces -> "        "
for (i = 0; i < 8; i++)
    printf(" ");

// print 2 pound / hash characters -> "##"
for (i = 0; i < 2; i++)
    printf("#");

// print 1 newline -> "\n"
// the loop construct is unnecessary here
for (m = 0; m < 1; m++)
    printf("\n");

// display second line

// print 7 spaces -> "       "
for (i = 0; i < 7; i++)   
    printf(" ");

// print 3 pound / hash characters -> "###"
for (i = 0; i < 3; i++)
    printf("#");

// print 1 newline -> "\n"
// this is the correct way to print a single newline char
printf("\n");

// display third line

// print 6 spaces -> "      "
for (i = 0; i < 6; i++)
    printf(" ");

// print four pound / hashes -> "####"
for (i = 0; i < 4; i++)   
    printf("#");

// print newline -> "\n"
printf("\n");

So the output from the program will look like the following:

        ##
       ###
      ####

While this looks like it might be the correct initially, one problem with this is that the program does not respond to the user input. The code currently caters for a fixed number of rows, with a fixed number of spaces and hashes. It needs to be able to print out a variable number of spaces and hashes.

In other words, instead of manually copying the code each time to create a each row, it needs to be able to repeat the necessary lines itself.

Here is the extract that draws a single row:

// print 8 spaces -> "        "
for (i = 0; i < 8; i++)
    printf(" ");

// print 2 pound / hash characters -> "##"
for (i = 0; i < 2; i++)
    printf("#");

// print 1 newline -> "\n"
printf("\n");  

This is almost correct for the goal of the pset. To complete this, the following needs to be done:

  1. The program needs to ask the user to input a number of rows to display, of not doing so already. This is critical to the rest of the program, so I would suggest getting this to work first.
  2. The block of code for drawing a row needs to be repeated. Not by manually copying because that won't be responsive to user input. It needs to be repeated the number of times entered by the user. A for loop or a while loop will work here. At this point you should see the same row repeating.
  3. The number of spaces and hashes needs to vary depending on the number of lines to be displayed. For example, if the user enters 5, the output should look like the following (without the comments):

        ## // line 0, 4 spaces, 2 hashes
       ### // line 1, 3 spaces, 3 hashes
      #### // line 2, 2 spaces, 4 hashes
     ##### // line 3, 1 space, 5 hashes
    ###### // line 4, 0 spaces, 6 hashes
    

    A direct correlation can be seen between the line index, and the number of spaces and hashes.

    Tip: Try with only one hash on the first line, two on the second, etc. It may be easier to reason about the logic. Once that is working, you can easily increase the number.

The other answers you referred to include some pseudo code, but I'll include some here as well:

get user input until valid value is entered

loop the number of times entered by the user
{
    loop number of spaces
        print " "

    loop number of hashes
        print "#"

    print "\n"
}    

The challenge is figuring out the correct number of spaces and hashes to repeat on each line.

2
  • Thanks a lot for very helpful answer. I tried using ./mario on command line.It asked me for a number greater than 23. Then I typed 23. Nothing happened. I typed some other numbers then a Retry came. Again I will check my code. There may be some other errors.uma1966
    – uma1966
    Commented Jul 30, 2014 at 1:51
  • @uma1966 It sounds like your input code is not working correctly. I suggest focusing on getting that working before tackling the code that draws the rows. You might first want to try just printing out the number entered by the user. Then modify it and add the code that repeats the question if the wrong number is entered. Other questions have some code you could refer to, or if none of those help, you could post another question dealing with that specifically. Commented Jul 30, 2014 at 12:04
0

Actually, you're doing it wrong... Did you see the walkthrough if Zamyla (http://youtu.be/z32BxNe2Sfc)? If not, I really recommend you to do so. Cheers, Matteo

You must log in to answer this question.

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