Éanan Halferty Éanan Halferty - 3 months ago 11
C Question

Compilation issue with c pset

I have no idea why this won't compile, looks fine to me.Want to make a program that builds a pyramid like the one at the end of Super Mario levels. Will ask user
for a number and then build pyramid to that height.

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

/*Want to make a program that builds a pyramid like the one at the end of Super Mario levels. Will ask user
for a number and then build pyramid to that height. for example user inputs 4 then pyramid would be
**
***
****
*****

*/
int main (void)
{
int input = 0;
int block = 2;

/* Asks user for a number between 0 and 24 and stores it as int input. If the number is not between
0 and 24 then it tells them to try again until it is between 0 and 24*/

do{
while(input == 0)

{
printf("\n\nEnter a number between but not including 0 and 24 and then press enter:\n\n");
scanf("%i", &input);
}

while(input <1 || input > 23) {
printf(" That number is not between 1-24! Try again idiot");
}
}

/* this is the algorithm that builds the pyramid*/

for(int x = 0; x < input; x++)
{

for(int x = 0; x < input -1; x++)
{
printf(" ");
}

for(int x = 0;x < block;x++)
{
printf("#");
}


printf("\n");
gap = gap -1;
block = block +1;
}
}


Compilation errors:

$ clang -ggdb3 -O0 -std=c99 -Wall -Werror mario.c -lcs50 -lm -o mario
mario.c:35:10: error: expected 'while' in do/while loop
for(int x = 0; x < input; x++)
^
mario.c:20:5: note: to match this 'do'
do{
^
1 error generated.
$

Answer

The compiler is telling you that you constructed your do / while loop incorrectly. Here is how a do / while loop works:

do
{
   // Your statements here

} while( condition );

Notice that the while comes after the closing } bracket. "condition" should be true to continue the loop or false to end it.

If you look at your code you'll see that after the closing } for the do, you have a for loop. The compiler was expecting a while there. But, you put the while inside the loop.

Here is your code with some comments:

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

/*Want to make a program that builds a pyramid like the one at the end of Super Mario levels. Will ask user
for a number and then build pyramid to that height. for example user inputs 4 then pyramid would be 
   **
  ***
 ****
*****

*/
int main (void)
{
    int input = 0;
    int block = 2;

    /* Asks user for a number between 0 and 24 and stores it as int input. If the number is not between
    0 and 24 then it tells them to try again until it is between 0 and 24*/



    /***  You start a do loop here ***/
    do{
        while(input == 0) /*** What does this do??? ***/

                                      {
      printf("\n\nEnter a number between but not including 0 and 24 and then press enter:\n\n");
      scanf("%i", &input);
                                      }

        while(input <1 || input > 23) {
        printf(" That number is not between 1-24! Try again idiot");
                                      }


      /*** You ended your do loop here and so the compiler is ***/
      /*** expecting to see a while. Look at my little sample ***/
      /*** do / while loop above ***/
      }
      /*** You need to add this... ***/
      while(some_condition);
      /*** The some_condition evaluates to true or false and ***/
      /*** controls whether or not the loop continues or ends ***/



   /* this is the algorithm that builds the pyramid*/



        /*** Because you have no while above, the thing the ***/
        /*** compiler sees right after the closing } of the ***/
        /*** do loop is this for, which is not valid ***/
         for(int x = 0; x < input; x++) 
    {

              for(int x = 0; x < input -1; x++) 
              { 
              printf(" ");
              }

              for(int x = 0;x < block;x++)
              {
              printf("#"); 
              }


        printf("\n");
        gap = gap -1;
        block = block +1;
    }
}