Ok, so apparently there are no compilation errors. But what exactly is happening here. Like, why am I getting this error? enter image description here

P.S-Even help50 doesn't seem to be of much help here... The actual code is as follows:

#include <cs50.h>
#include <stdio.h>

int main(void)
 int i,n,j;

  printf("Please enter a number between 1 and 8 to see your block from mario: \n");
  scanf("%n", &n);
 while(n>=1 && n<=8);

 for(i=1; i<=n;i++)


1 Answer 1


There are a couple issues here. First, the loop test condition is incorrect.

      while(n>=1 && n<=8);

This says that if the number is between 1 and 8 inclusive, loop again. You want exactly the opposite of that.

The second problem is the scanf. The formatting string should be %i (integer type), not %n. %n is a special format specifier that says "read the number of characters specified by the contents of the variable. This means that it reads 0 characters, since the variable n contains "0". Later, n still has 0 in it so the code runs to completion without printing anything.

However, if you fix the while loop condition, the combination of these two turn the code into an infinite loop instead of a one-pass.

I suggest you use the get input functions provided by CS50 for these first few psets. You'll be taught how to get input properly later. Or, study the docs for scanf, printf and their formatting string rules more carefully. (you'll learn more doing that. ;-) )

Just to be clear, the correct way to write a format string is to insert var placeholders (they start with %, specify a variable type, and can also include additional formatting to describe how the value from the var should be read or displayed) into the string and then place the var names in order, following the string and separated by commas. One does not insert the var name itself in the string. For example:

 string name = "fred";

    printf("My name is %name ");

    printf("My name is %s ",name);

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

  • Ah. ok Got it thanks!
    – Soham Roy
    Commented Jun 17, 2023 at 20:52

You must log in to answer this question.

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