I have done my mario.c all correct but the cs50 check says: ':( rejects a height of 9, and then accepts a height of 2 expected exit code 0, not 2' Why I have this problem? Idon't understend. When I testing, everithing work perfectly. Please, help me!

  • Can you publish your code, or put a link to it? – MARS Jul 4 '19 at 17:44
  • Yes, of course. Here it is: #include <cs50.h> #include <stdio.h> int main(void) { int h; do { h = get_int("Height (from 1 to 8): "); } while (h < 1 || h > 9); for (int i = h; i >= 1; i--) { for (int sp = 1; sp < i; sp++) printf(" "); for (int hash = h; hash >= i; hash--) printf("#"); printf("\n"); } return h; } – Stricha Jul 4 '19 at 19:11
  • Sorry for my formating((( – Stricha Jul 4 '19 at 19:17

I've seen this several times and I wonder why. Well the return statement, inside the main function, causes the immediate termination of the program, with an integer that returns main, remember the declaration of main:

int main(void)

main should return an integer, it could be any, but by convention, main will return zero at the end, when the program ends and we know that it has been executed correctly. Therefore we must return zero if we reach the end:

return 0;

and do not return h as you do in your program. The zero, as a standard value, is used by check 50 to check the correct end of the program. The convention says that if we return a non-zero value in main, it's that something went wrong and we're not going to get to the end of the program for any reason. One of these reasons may be, for example, that the user does not collaborate and gives us incorrect values for the pyramid. We can choose to ask again or just leave with a code of 1 or 2 or 3 ... as a sign that something did not go well.

  • Thank you so much for your answer! I tried to change my code with your recommendation. But now I have another problem. Here it is: :) mario.c exists. :) mario.c compiles. :) rejects a height of -1 :) rejects a height of 0 :) handles a height of 1 correctly :) handles a height of 2 correctly :) handles a height of 8 correctly :( rejects a height of 9, and then accepts a height of 2 expected " #\n##\n", not "" :) rejects a non-numeric height of "foo" :) rejects a non-numeric height of "" – Stricha Jul 5 '19 at 14:26
  • :( rejects a height of 9, and then accepts a height of 2 expected " #\n##\n", not "" – Stricha Jul 5 '19 at 14:29
  • the condition h < 1 || h > 9 implies that the nine is an accepted height (think carefully), contrary to what the specifications require – MARS Jul 5 '19 at 15:08

You must log in to answer this question.

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