0

I'm working on the 'More Comfortable' version of the Mario Pyramid exercise from Problem Set 1. It isn't too difficult and I have it up and working. It successfully rejects incorrect input and produces fairly predictable output, as follows:

Enter a number between 1 and 8 to print a Mario 1-1 style block pyramid!
Enter height: -4
Enter height: 210
Enter height: a
Enter height: a string
Enter height: 8

       #  #
      ##  ##
     ###  ###
    ####  ####
   #####  #####
  ######  ######
 #######  #######
########  ########

As you can see, however, there is a newline inserted between the 'Enter Height' prompt, and the top layer of the pyramid. When I try to submit this through GitHub and the code is tested by their system, some of the lines fail (the ones with the frowny faces), producing these messages:

:) mario.c exists
:) mario.c compiles
:) rejects a height of -1
:) rejects a height of 0
:( handles a height of 1 correctly
    expected ""#  #"", not ""   "\n"#  #""
:( handles a height of 2 correctly
    expected "" #  #"\n"##  ...", not ""    "\n" #  #..."
:( handles a height of 8 correctly
    expected ""       #  #"\...", not ""          "\n..."
:( rejects a height of 9, and then accepts a height of 2
    expected "" #  #"\n"##  ...", not ""    "\n" #  #..."
:) rejects a non-numeric height of "foo" 
:) rejects a non-numeric height of "" 

You can see that the code 'correctly rejects' a height out of range, and 'then accepts a height' in range, but the problem appears to be that the test is expecting the top of the pyramid to begin immediately after the height prompt. I am not putting a newline in between the prompt and the pyramid. Since I'm using cs50.h's get_int() instead of scanf() I'm not sure if their function is inserting a newline somewhere.

The output is completely to spec apart from this newline. Without giving away my solution too much (well, in the interest of being informative I've given the game away completely!), the code for the prompt and beginning of the pyramid looks like this:

int main(void)
{
    printf("Enter a number between 1 and 8 to print a Mario 1-1 style block pyramid!\n");

    int pyramidSize;

    do
    {
        pyramidSize = get_int("Enter height: ");
    }
    while (pyramidSize < 1 || pyramidSize > 8);  // Ensure input is in the correct range, otherwise prompt again

    // Build the pyramid!
    string brick = "#";
    int padding = pyramidSize;

    for (int i = 0; i <= pyramidSize; i++) 
    {                                      
        // BUILD THE LEFT SIDE OF THE PLUMBER'S PYRAMID
        for (int j = 0; j < padding; j++)
        {
            printf(" ");
        }
        for (int k = 0; k < i; k++)
        {
            printf("%s", brick); 
        }
        padding = padding - 1; 
        printf("  ");          // Middle gap of 2 brick spaces.

        // BUILD RIGHT SIDE OF THE PLUMBER'S PYRAMID
        for (int x = 0; x < i; x++)
        {
            printf("%s", brick);
        }
        printf("\n"); 
    }   
}

Previously, I also had separate functions to get the input for the pyramid height and print the pyramid, but moved both of these into main because I thought this was causing a problem, but this hasn't helped.

I understand that with the amount of students signing up they need automated tools to vet whether a program is correct, but its a bit silly that a rogue newline can completely prevent my program from submitting. Has anyone run into this before and knows what I can do?

Follow-up: I moved the variable declarations for brick and padding up above the input do-while loop and above the loop, respectively. There is now nothing between the pyramid height input and the for loop that begins constructing the pyramid. I am stumped.

Follow-up 2: When I submit the input by pressing Enter, am I implicitly inserting a newline? What on earth am I supposed to do about that?

Thanks in advance.

7
  • There has to be an extra line feed somewhere. It's not in the code posted, so I'm guessing that it's in the code that wasn't included. Can you edit the code and add what's missing?
    – Cliff B
    Jan 15 '20 at 22:11
  • Hi Cliff, thanks for responding! I updated the code to include everything - from the start of main right through the pyramid-construction. This is running in a Linux cloud virtual machine with the Clang compiler as part of the course, but I also first wrote it on my own machine using the GCC compiler and the result is the same. On my own machine, I didn't have access to the <cs50.h> facilities like get_int() so I used scanf(); the result is exactly the same. Thanks again, let me know what you can spot! Oh, also, have a look at my 'Follow-up 2' at the bottom!
    – Rowan
    Jan 15 '20 at 22:21
  • Try removing the "instruction" line. Program should not produce output other than what is specified. Also, I believe the prompt should be "Height: " and not "Enter Height: ". check50, especially in the early psets, is very precise viz. the spec. Jan 15 '20 at 22:57
  • @DinoCoderSaurus Thanks for the advice! For some reason I can't get back into the CS50 sandbox at the moment (I simply get a white screen saying "CS50 Lab: You are logged in as me. Coming soon!"). However, I've tried your fixes on my own GCC platform and the newline is still there. I really don't want to look at the staff solution for this, but I bet it's something obvious I've done wrong. Checking the solution just feels like giving up.
    – Rowan
    Jan 15 '20 at 23:26
  • I'm quite concerned now. I can get into a sandbox for the second part of Problem Set 1, 'Credit', with no issue. Is there a limit to the amount of times you can attempt to submit code for a problem?
    – Rowan
    Jan 15 '20 at 23:31
1

Very interesting problem. I'll spell out the symptoms, since they're not clear unless you step through the code, and let you resolve the problem! ;-)

First of all, it's not a problem with get_int.

If you step through the program, using debug50, here's what happens. On the first pass through the while loop, it prints some spaces, but it doesn't print any hashes or bricks!

You could also try printing an X instead of a space to see what's going on! It would make the problem obvious.

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

1
  • I'm fairly sure I've just spotted the problem you described without having to run anything; would it be on the line for (int k = 0; k < i; k++) by any chance? I'm going to fix it now (if this is the case), but I appreciate you figuring this out!
    – Rowan
    Jan 16 '20 at 1:03
1

The extra line feed comes when i is 0. How many times will the k loop run? Then it prints the \n.

Have another look at this, too for (int i = 0; i <= pyramidSize; i++). Count if you have to. If pyramidSize is 1, how many times will the i loop run?

Starting counting from 0 is hard, until it becomes "the new normal"

1
  • It's always the silly, invisible mistakes that catch you out, eh? I've fixed the k loop now and the program seems to work perfectly to spec. Thanks to you and @Cliff B for having a better head on your shoulders than me! Edit; the i <= pyramidSize statement was actually a band-aid solution to this problem initially that didn't fix the underlying cause, rather than an issue with my counting from zero ;). Thanks for pointing that out though!
    – Rowan
    Jan 16 '20 at 1:08

You must log in to answer this question.

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