【发布时间】:2014-10-24 23:25:19
【问题描述】:
嗨,这是我在这个网站上的第一篇文章,我刚刚开始为学校学习 C 编程。我试图寻找关于我的具体问题的其他帖子,但对其他答案的复杂性有点不知所措。 (所以我很抱歉,如果它是多余的)
我正在尝试编写一个代码,它接受 4 个输入数字,将它们向后打印给您,然后询问您是否要使用 y/n 选项再次执行此操作。我需要一些帮助来让计算机读取用户的 y/n 输入,然后基于此继续/中断循环。这是我到目前为止的内容,但我遇到了一些严重错误,谢谢。
#include <stdio.h>
int main()
{
int x[5];
char choice;
choice = 'y'; //Assigned choice to y//
while (choice == 'y')
{
printf("Please input up to four numbers seperated for example, 1 2 3 4 : ");
scanf_s("%d %d %d %d", &x[0], &x[1], &x[2], &x[3]);
printf("Your entries in reverse order are %d %d %d %d\n", x[3], x[2], x[1], x[0]); //This is working//
printf("Would you like to enter another set of numbers? <y/n>:");
scanf_s(" %c", choice); //Want this line to get an input y/n and if y, repeat the loop and if n, end the program//
}
printf("Goodbye\n");
system("Pause");
return 0 ;
}
【问题讨论】:
-
“错误” - 告诉我们它们是什么。运行时错误或编译器错误?编译器消息是诊断信息,因此当请某人诊断问题时,如果您提供该信息会很有帮助(甚至是礼貌)。
-
与问题无关,但您可以在声明时初始化
choice,而不是先声明然后赋值:char choice = 'y' ; -
我建议对
while循环使用do while循环。那么你就不必在开头设置choice的值了。 -
以后会贴出错误码,感谢帮我简化char变量
标签: c while-loop char scanf