我的 C 程序运行良好,但最后当我尝试创建一个循环时,如果用户需要它会不断重复,它似乎不起作用
My program in C works all fine, but at the very end when I try to create a loop so it keeps repeating if the user wants, It doesnt seem to work
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define pi 3.14159265
int main()
{
int n;
int fact=1;
int i;
char repeat;
do{
//part a
printf("Please input a positive whole number value to be stored as 'n': ");
scanf("%d",&n);
while(n<0){
printf("Improper value of n. Please input a positive whole number value to be stored as 'n': ");
scanf("%d",&n);
}
//part b
if (n>0){
printf("n is now %d\n",n);
for(i=1;i<=n;i++){
fact=fact*i;
}
//printf("The factorial of %d is %d.\n",n,fact);
}
else{
printf("The factorial of 0 is 1.\n");
}
//part c
double approx = (pow((double)n,n)*exp(-n))*sqrt(((2*n)+(1/3))*pi);
//printf("Approx is %lf",approx);
//part d
printf("%d! equals approximately %lf.\n",n,approx);
printf("%d! is %d accurately.\n",n,fact);
//part e
double perror=((fact-approx)/(fact))*100;
printf("The approximate value is off by %lf%%\n",perror);
//part f
printf("Would you like to restart with another value? Respond y or n: ");
scanf("%c",&repeat);
} while(repeat=='y');
/*I was going to have the program restart if the user input "y" at the end of the program but I can't
figure out why it isn't working.*/
return 0;
}
我对 C 语言编程还是很陌生,并且仍在学习基础知识,因此不胜感激。
我唯一没有弄清楚的部分是为什么 do...while 循环在代码的开头和结尾不起作用。
scanf("%c",&repeat);
您需要在 %c
之前添加一个 space,以便使用之前调用 scanf
:
留下的换行符
scanf(" %c",&repeat);
来自 https://linux.die.net/man/3/scanf:
Conversions
c
Matches a sequence of characters whose length is specified by the
maximum field width (default 1); the next pointer must be a pointer to
char, and there must be enough room for all the characters (no
terminating null byte is added). The usual skip of leading white space
is suppressed. To skip white space first, use an explicit space in the
format.
此外,永远不要相信用户并始终在使用 scanf
之前初始化变量,否则如果失败,您最终可能会读取未初始化的值(垃圾):
char repeat;
应该是
char repeat = 'n';
或至少检查 scanf
的结果并用适当的默认值填充变量:
char repeat;
...
if (scanf(" %c",&repeat) != 1)
{
repeat = 'n';
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define pi 3.14159265
int main()
{
int n;
int fact=1;
int i;
char repeat;
do{
//part a
printf("Please input a positive whole number value to be stored as 'n': ");
scanf("%d",&n);
while(n<0){
printf("Improper value of n. Please input a positive whole number value to be stored as 'n': ");
scanf("%d",&n);
}
//part b
if (n>0){
printf("n is now %d\n",n);
for(i=1;i<=n;i++){
fact=fact*i;
}
//printf("The factorial of %d is %d.\n",n,fact);
}
else{
printf("The factorial of 0 is 1.\n");
}
//part c
double approx = (pow((double)n,n)*exp(-n))*sqrt(((2*n)+(1/3))*pi);
//printf("Approx is %lf",approx);
//part d
printf("%d! equals approximately %lf.\n",n,approx);
printf("%d! is %d accurately.\n",n,fact);
//part e
double perror=((fact-approx)/(fact))*100;
printf("The approximate value is off by %lf%%\n",perror);
//part f
printf("Would you like to restart with another value? Respond y or n: ");
scanf("%c",&repeat);
} while(repeat=='y');
/*I was going to have the program restart if the user input "y" at the end of the program but I can't
figure out why it isn't working.*/
return 0;
}
我对 C 语言编程还是很陌生,并且仍在学习基础知识,因此不胜感激。 我唯一没有弄清楚的部分是为什么 do...while 循环在代码的开头和结尾不起作用。
scanf("%c",&repeat);
您需要在 %c
之前添加一个 space,以便使用之前调用 scanf
:
scanf(" %c",&repeat);
来自 https://linux.die.net/man/3/scanf:
Conversions
c
Matches a sequence of characters whose length is specified by the maximum field width (default 1); the next pointer must be a pointer to char, and there must be enough room for all the characters (no terminating null byte is added). The usual skip of leading white space is suppressed. To skip white space first, use an explicit space in the format.
此外,永远不要相信用户并始终在使用 scanf
之前初始化变量,否则如果失败,您最终可能会读取未初始化的值(垃圾):
char repeat;
应该是
char repeat = 'n';
或至少检查 scanf
的结果并用适当的默认值填充变量:
char repeat;
...
if (scanf(" %c",&repeat) != 1)
{
repeat = 'n';
}