Welcome fellow students, register yourself and dive into asking questions, giving answers and helping the community learn with you.

dont know what is wrong for the mario more coftareble

0 votes
asked Nov 10, 2017 in Pset1 by SharpieSharp (150 points)
#include <stdio.h>

int main()

int height;
int row = 1;
int filler = 1;
	printf("Height: ");
	scanf("%d", &height);

	while((height > -1) || (height < 24))
	for(int Liam = 0; Liam < height; Liam++ );
		for(row = filler, filler++;;);
		printf("%0*d", (height - row), ' ');
		printf("%#*d", row);
		printf("  ");
		printf("%0*d\n", (height - row), '#');

1 Answer

0 votes
answered Nov 11, 2017 by Faïza Harbi (11,960 points)
selected Nov 12, 2017 by SharpieSharp
Best answer


So, I am curious to know if you have actually watched the lectures and walkthroughs and shorts, because there is a lot of things that are wrong here, starting with the syntax

  • First curly brace after declaring filler: Why are you putting a curly brace? It's incapsulating nothing, no statement (if, else etc...) no loop (while, for, do/while): Wrong.
  • You have a while loop - you may want to check if using a logical OR aka || is actually what you want to do: If it can help, you can check that short video https://www.youtube.com/watch?v=TTQTe3d4mqY&index=3&list=PLngjhIZVO0EDehFv6oJcGOr62HqtgMZTk on the OR and AND logical operators.
  • There is a semi-colon at the end of your first for line: No, there shouldn't be a semi-colon. A semi-colon is used to tell the compiler "Here is the end of this action", the only thing that comes after a for is an opening curly brace.
  • It brings us to your second for: Not only do you end your for line with a semi-colon, but you also have 2 of them, not at the right place within the parentheses. So, a for basically works this way:
                      for( starting point; stopping point aka end of your for loop aka exit the for loop ; step aka what you modify to move on/back in the loop until you eventually reach the stopping point)
                      { whatever you have to do in your loop}
                      If it helps you, there is also this video on how to use loops, not in depth, but it should help you get a better grasp of how it works: https://www.youtube.com/watch?v=SJtxFHuYuvk&list=PLngjhIZVO0EDehFv6oJcGOr62HqtgMZTk&index=2
  • At the end of let's say basically any main function, you return something. The common norm is : return 0; where 0 here is the number for "ok, good to go". A main without a return at the very end, before the last closing curly brace is going to make the compiler "yell" at you.
  • It's not exactly syntax, it's a mix, but why are you using the %d here? We are displaying characters. There is a specific format specifier for characters in C (as well as in different other programming languages), it's somewhere in the videos from the course, and you can also google printf in C, you'll end up on format specifiers pretty quickly.
  • I think the course provides you the "cs50.h" library, which helps a lot, including with how to "not use" (not directly) scanf. I have a rule of thumb when it comes to scanf: I use scanf means I also use fflush (again, google). It may not affect your code here, because your scanf is not in a loop, but you really want to remember that because trust me, you could spend quite some time on a good code looking for something wrong in it while the issue is a scanf without an fflush from the beginning (been there!).
Your logic is flawed here, which is ok, we're here to learn.
I am not going to enter in the details of the issues within your algorithm, that is something you can check by clicking on Pset1 mario, anyway, here is the direct link to a pseudocode I wrote for other cs50xers on this website: http://www.cs50xhelpers.org/question2answer/ask?follow=350&cat=2
First things first: Familiarise yourself with the C syntax aka semi-colons, curly braces, parentheses etc.... Then, take a piece of paper and a pen, take a height of 3 for instance and write down for each "turn" in each loop what your code does and NOT what you think your code does. Pen and paper still are your best friends while writing code.
Keep on going!
Welcome to CS50xHelpers Q&A, where you can ask questions and receive answers from other members of the community.

284 questions

248 answers


6,600 users

2 Online
0 Member And 2 Guest