gpt4 book ai didi

c - 在我输入 scanf 接受的输入后程序立即退出

转载 作者:太空宇宙 更新时间:2023-11-04 04:09:52 25 4
gpt4 key购买 nike

我正在尝试制作菱形。我向用户询问一个数字,然后我制作一个 2n+1 x 2n+1 的形状。我要求写一封信而不是星星,而是打印出来。另外,我打印了句号而不是空格。我的程序已停止运行。它要求我输入,然后窗口就消失了,程序就死了。我真的可以使用提示。是的,这是一道作业题。

#include <stdio.h>

void main(){
int number_user_Input;
int temp;
char letter;
int shape_dimension;

printf("Enter a number please: ");
while((temp=scanf("%d", &number_user_Input))==0)
{
scanf("%*[^\n]");
printf("Please enter an actual number:\n");
}
if(temp==EOF)
{
printf("Nothing to read, number was not found.\n");
}
else
{
printf("You typed: %d\n", number_user_Input);
}
printf("Please type only one letter: ");
scanf(" %c", &letter);
shape_dimension = 2*number_user_Input+1;
printf("Shape dimensions are: %d \n", shape_dimension);
for(int i=0; i<shape_dimension; i++)
{
for(int j=1; j <shape_dimension-i; j++)
{
printf(".");
}
for(int j=1; j<2*i-1; j++){
printf("%c", letter);
}
printf("\n");

}

for(int i=shape_dimension-1; i>=1; i--)
{
for(int j=1;j<=shape_dimension-i;j++){
printf(".");
}
for(int j=1;j<=2*i-1;j++){
printf("%c", letter);

}
printf("\n");
}
}

最佳答案

我认为更改此部分将解决您的问题。

for(int i=0; i<shape_dimension; i++){
for(int j=0; j <shape_dimension-i; j++){
printf(".");
}

for(int j=0; j<2*i-1; j++){
printf("%c", letter);
}
printf("\n");

}

//~ printf("\n");

for(int i=shape_dimension; i>=1; i--){

for(int j=0;j<shape_dimension-i;j++){
printf(".");
}

for(int j=0;j<2*i-1;j++){
printf("%c", letter);
}
printf("\n");
}

运行代码会产生这个输出

./code <<< "1 a"
Enter a number please: You typed: 1
Please type only one letter: Shape dimensions are: 3
...
..a
.aaa
aaaaa
.aaa
..a

其他注意事项

  • void main() -> int main(void)return 0;main()

关于c - 在我输入 scanf 接受的输入后程序立即退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58544306/

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