[英]Nested If statement Ignoring scanf function
我已经编写了一个程序,用户在其中输入ax坐标和y坐标。如果用户输入了字母,我希望重复输入直到输入了有效的输入。 尝试3次后,程序将结束。 但是,每次我运行程序并输入测试字符时,程序都会跳过scanf函数,并且不测试嵌套的if循环,我尝试在scanf函数中添加一个空格,但这似乎根本不起作用。 希望能有所帮助,谢谢:)
码:
printf("Insert first vector:\n");
vector1 = scanf(" %f %f",&x1,&y1);
printf("x1 %f y1 %f \n",x1,y1);
if(vector1 == 0)
{
printf("Invalid input\n");
printf("Insert first vector; \n");
vector2 = scanf(" %f %f",&x1,&y1);
if(vector2 == 0)
{
printf("Invalid input\n");
printf("Insert first vector; \n");
vector3 = scanf(" %f %f",&x1,&y1);
if(vector3 == 0)
{
printf("Invalid input \n");
x1=WRONG;
我假设您正在使用C语言。
int count = 1;
do {
printf("Insert First Vector: ");
fflush(stdin);
vector1 = scanf("%f %f", &x1, &y1);
count++;
} while (vector == 0 && count <= 3);
尝试使用上面的代码。
为了简单起见,我使用do while
循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.