gpt4 book ai didi

c pset 的编译问题

转载 作者:行者123 更新时间:2023-11-30 19:42:32 28 4
gpt4 key购买 nike

我不知道为什么这不能编译,对我来说看起来不错。想要制作一个程序来构建像 super 马里奥关卡末尾那样的金字塔。会询问用户 获取一个数字,然后将金字塔 build 到该高度。

#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 来结束循环。

如果您查看代码,您会发现在 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;
}
}

关于c pset 的编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31763064/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com