0

Ok so this is my code I have right now:

#include <cs50.h>
#include <stdio.h>
int get_positive_int(string prompt);
int main(void)
{
int i = get_positive_int("Height: ");
for (i = 0; i < 8; i++)
{
   printf("#");
   printf("\n");
}
}
// Ask for integer between 1 and 8
int get_positive_int(string prompt)
{
int n;
do
{
    n = get_int("%s", prompt);
}
while (n > 8 || n <= 0);
return n;    
}

It prints out this: enter image description here

Can someone please help! Thanks!

1 Answer 1

1

You assign the height to variable i, and on the next line you overwrite it with 0, throwing away the user input. Make the row counter a different variable, and name your variables according to their meaning, especially if they are used at any point outside of the loop head, and both height and likely also the row counter (or whatever you want to call it) are.

For multiple hashes in a row, you would place another loop inside your existing for loop, repeating as often as you need to print a hash in the current row (same for the leading spaces).

3
  • Could you show me an example of a for loop inside of another one? And what do you mean with naming your variables? I don’t really get it. Thanks. Commented Apr 21, 2019 at 20:02
  • Please, can someone help me on this issue? Commented Apr 21, 2019 at 22:53
  • A nested for loop could for example be for (int i = 1; i <= 10; i++) { for (int j = 1; j <= i; j++) { printf("%i * %i = %i\n", i, j, i * j); } printf("\n"); } (obviously, that one is not made for mario, but in some properties similar)
    – Blauelf
    Commented Apr 22, 2019 at 13:32

You must log in to answer this question.

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