[英]How to loop a whole codeblock in C?
因此,基本上在计算之后,程序会提示用户是否要退出程序,并且用户输入了字符(“ y”或“ n”),并且用户输入的数字或字母不是“ y”或“ n” ',程序将继续提示用户,直到他们输入字符之一。
我遇到的问题是,即使输入了“ y”或“ n”,该程序仍会不断循环并提示用户。 当我尝试fflush(stdin)仍然不起作用
我想知道如何在用户未输入选项之一的情况下再次循环该语句,以及当他们正确输入该选项时如何在“ do while”循环内重复执行该代码。 最好不必再次复制并粘贴整个块。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
int main()
{
float x, t, term = 1 , sum = 1;
int i;
char d;
printf("This program will compute the value of cos x, where x is user input\n\n");
do {
printf("Please input the value of x: ");
while (scanf("%f", &x) != 1)
{
fflush(stdin);
printf("Please input the value of x: ");
scanf("%f", &x);
}
fflush(stdin);
printf("\nPlease input the number of terms: ");
while (scanf("%f", &t) != 1)
{
fflush(stdin);
printf("\nPlease input the number of terms: ");
scanf("%f", &t);
}
fflush(stdin);
for (i=1; i<t+1; i++)
{
term = -term *((x*x)/((2*i)*(2*i-1)));
sum = sum+term;
}
printf("\nThe value of the series is %f", sum);
printf("\n****************************************");
printf("\nDo you wish to quit? (y/n): ");
scanf("%c", &d);
while (d != 'y' || d != 'n')
{
printf("\n****************************************");
printf("\nDo you wish to quit? (y/n): ");
scanf("%c", &d);
}
} while (d == 'n');
if (d == 'y')
{
printf("terminating program");
exit(0);
}
return (0);
}
除非将格式字符串设置为丢弃它,否则scanf()不会在输入缓冲区中丢弃换行符'\\ n'。 在代码中,输入浮点数的输入并按Enter后,换行符仍在缓冲区中。 因此对于提示Y \\ N的代码,请使用此格式字符串忽略换行符
scanf(" %c",&d);
如果这样做,则可以删除fflush()调用。 就您而言,循环条件似乎是错误的。
这条线
while (d != 'y' || d != 'n')
是错的。
这样想:
如果
d
不是'y'
或d
不是'n'
,则循环运行
现在假设您输入'y'
d
是'y'
。 如果d
不是'y'
或d
不是'n'
,则循环运行。d != 'y'
吗? 不,是d != 'n'
吗? 是。 因此,循环必须运行。
您需要使用&&
while (d != 'y' && d != 'n')
另外, scanf
不会丢弃换行符,因此请为所有scanf
添加一个空格。
scanf("%c", &d); //change to scanf(" %c", &d);
看这部分
while (d != 'y' || d != 'n')
{
printf("\n****************************************");
printf("\nDo you wish to quit? (y/n): ");
scanf("%c", &d);
}
} while (d == 'n');
您正在使用while
两次,我想您希望在这里有一个会一会儿的情况..如果您正在结束while
,那么请确保涉及到do
。
这是我认为正确的代码,因为您有很多问题,所以我做了很多更改:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
int main()
{
float x, t, term = 1 , sum = 1;
int i;
char d;
printf("This program will compute the value of cos x, where x is user input\n\n");
do {
printf("Please input the value of x: ");
while (scanf("%f", &x) != 1)
{
fflush(stdin);
printf("Please input the value of x: ");//delete the repeat scanf
}
fflush(stdin);
printf("\nPlease input the number of terms: ");
while (scanf("%f", &t) != 1)
{
fflush(stdin);
printf("\nPlease input the number of terms: ");
}
fflush(stdin);
sum=0;//no initalization
for (i=1; i<t+1; i++)
{
term = -term *((x*x)/((2*i)*(2*i-1)));
sum = sum+term;
}
printf("\nThe value of the series is %f", sum);
printf("\n****************************************");
printf("\nDo you wish to quit? (y/n): ");
scanf("%c", &d);
while ((d != 'y' )&& (d != 'n'))//this logical expression is the right way
{
scanf("%c", &d);
fflush(stdin);
printf("\n****************************************");//I change the pos of print to avoid double printing
printf("\nDo you wish to quit? (y/n): ");
}
} while (d == 'n');
if (d == 'y')
{
printf("terminating program");
exit(0);
}
return (0);
}
ps:对于您的cos计算部分,我不确定运行时的正确性:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.