0

I'm trying to retrieve an input from the user and then apply it to the portion of mario.c that makes prints the pyramid. After several hours of trial and error, here is what I have been able to come up with so far:

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

int main(void);

int (n);
int n = GetInt();

do
{
    n = GetInt();
}
while (n !> 0 && n !< 23)


for (int i = 0; i < n; i++)
{
    printf ("##");
}

When I attempt to compile this program, I get the following errors:

mario.c:7:9: error: initializer element is not a compile-time constant
int n = GetInt();
    ^~~~~~~~
mario.c:9:1: error: expected identifier or '('
do
^
mario.c:13:1: error: expected identifier or '('
while (n !> 0 && n !< 23)
^
3 errors generated.

No matter what I do to my code, I always get these three errors, at the very least.

  • What is a 'compile-time constant'?
  • To what are the 'expected identifier' errors referring?
  • What am I doing wrong and what can I do to start debugging?

2 Answers 2

5

You have the right idea behind how the code works, it's just your English to C needs some practice (understandably).

int main(void);

This is, as its name suggests, the "main" method. Essentially, when you say to run the program, it looks for main(void) and goes from there. Therefore, all your other code should be inside this function, Which will take care of error number one.

int main(void)
{
    // code goes here
}

Next up: The do-while. First, after the closing ) in the while there should be a ;. Next, !> and !< aren't recognized by C. Your options are ==, !=, > and <. So instead of saying "while n isn't greater than 0 and isn't less than 23", instead try "while n is outside the boundries (of 0 and 23)".

Let me know if you could use more guidance, particularly on the second part. You're doing well so far!

6
  • Thanks so much for the help. I've made some modifications based on your suggestions: #include <stdio.h> #include <cs50.h> int main(void); { int (n); int n = GetInt(); do { n = GetInt(); } while (n < 0 && n > 23); for (int i = 0; i < n; i++) { printf("##") } } I'm now left with just one error: mario.c:6:1: error: expected identifier or '(' { ^ I'm still not really sure what this error is, or how to fix it. Sep 24, 2014 at 17:20
  • Remove the ; after int main(void) and it should run. However, you're going to run into a bit of an issue in the while. Right now it says "while N is negative AND more than 23". Obviously one number can't be both. I would use OR (||) there instead.
    – Melde
    Sep 24, 2014 at 18:09
  • Awesome. My program now compiles and runs well, so far as it goes. I'm now having some trouble making the actual pyramid. I know I'm going to need a loop which changes the number of characters each time, but I can't quite work out how to do it. Sep 24, 2014 at 19:27
  • When I run into that problem, I think through how I would do it by hand. Since it's simpler, let's try a left-aligned triangle first. Keep in mind that you're printing from top to bottom. So let's say n = 5. How many #s need to be printed in the top row? How about in the next row under that? And under that? See if you can find a relationship between what row it's at (where i = something in the for loop) and how many #s to print.
    – Melde
    Sep 24, 2014 at 19:55
  • I think I understand the relationship. The main thing I don't understand is how I should indicate this relationship in my code. Sep 25, 2014 at 0:27
1

Your problem now is that you have written ; after int main (void). Delete ; and everything should be right. Your main function should be:

int main(void)
{ 
  int (n); 
  int n = GetInt(); 
  do { 
       n = GetInt(); 
  } while (n < 0 && n > 23); 
  for (int i = 0; i < n; i++) { 
    printf("##") 
  }
}

You must log in to answer this question.

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