c pset 的编译问题
Compilation issue with c pset
我不知道为什么这不会编译,me.Want 看起来不错,可以制作一个像超级马里奥关卡末尾那样构建金字塔的程序。会询问用户
一个数字,然后将金字塔建到那个高度。
#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.
$
编译器告诉您,您构建的 do / while 循环不正确。以下是 do / while 循环的工作原理:
do
{
// Your statements here
} while( condition );
请注意 while 出现在右 } 括号之后。 "condition" 应为 true 以继续循环或为 false 以结束循环。
如果您查看您的代码,您会发现在关闭 } for the do 之后,您有一个 for 循环。编译器在那里等待一段时间。但是,您将 while 放在循环中。
这是你的代码,带有一些注释:
#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;
}
}
我不知道为什么这不会编译,me.Want 看起来不错,可以制作一个像超级马里奥关卡末尾那样构建金字塔的程序。会询问用户 一个数字,然后将金字塔建到那个高度。
#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.
$
编译器告诉您,您构建的 do / while 循环不正确。以下是 do / while 循环的工作原理:
do
{
// Your statements here
} while( condition );
请注意 while 出现在右 } 括号之后。 "condition" 应为 true 以继续循环或为 false 以结束循环。
如果您查看您的代码,您会发现在关闭 } for the do 之后,您有一个 for 循环。编译器在那里等待一段时间。但是,您将 while 放在循环中。
这是你的代码,带有一些注释:
#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;
}
}