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.
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 likeget_int()
so I usedscanf()
; 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!