c pset的编译问题

时间:2015-08-01 14:43:36

标签: c cs50

我不知道为什么这不会编译,对我来说看起来很好。想要制作一个像超级马里奥级别结束时构建金字塔的程序。会问用户     为一个数字,然后建立金字塔到那个高度。

#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;
    }
}

编译错误:

$ 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.
$ 

1 个答案:

答案 0 :(得分:0)

编译器告诉您错误地构造了do / while循环。以下是do / while循环的工作原理:

do
{
   // Your statements here

} while( condition );

请注意,while结束于括号之后。 &#34;条件&#34;应该是true来继续循环或false来结束它。

如果你查看你的代码,你会看到在关闭之后,你有一个for循环。编译器期待一段时间。但是,你把时间放在循环中。

以下是您的代码,其中包含一些注释:

 #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;
    }
}
相关问题