【问题标题】:How to terminate a program using while loop [duplicate]如何使用while循环终止程序[重复]
【发布时间】:2018-05-02 16:10:48
【问题描述】:

我做了这个计算器。

一切正常。

但是,我想使用如下的while循环:

char cont = 'y'
while (cont == 'y') {
    /code/
}
printf("Do you want to continue (y/n)")
scanf("%c", & cont)

打印出来:

Do you want to continue (y/n)

但是当我输入一些东西时,程序会意外结束。

完整代码:

#include < stdio.h > 
#include < conio.h >

void main() {
    float x, y, result;
    int select;
    char cont = 'y';
    clrscr();
    while (cont == 'y') {
        clrscr();
        printf(
            "Please Enter The Respective Number For Following Operation\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n"
        );
        scanf("%d", & select);
        clrscr();
        switch (select) {
        case 1:
            {
                printf("\nEnter The First Number To Add\n");
                scanf("%f", & x);
                printf("\nEnter The Second Number To Add\n");
                scanf("%f", & y);
                clrscr();
                result = x + y;
                printf("Addition of two numbers %f and %f is %f", x, y,
                    result);
                break;
            }
        case 2:
            {
                printf("\nEnter The First Number To Subtract\n");
                scanf("%f", & x);
                printf("\nEnter The Second Number To Subtract\n");
                scanf("%f", & y);
                clrscr();
                result = x - y;
                printf("Subtraction of two numbers %f and %f is %f", x, y,
                    result);
                break;
            }
        case 3:
            {
                printf("\nEnter The First Number To Multiply\n");
                scanf("%f", & x);
                printf("\nEnter The Second Number To Multiply\n");
                scanf("%f", & y);
                clrscr();
                result = x * y;
                printf("Multiplication of two numbers %f and %f is %f", x,
                    y, result);
                break;
            }
        case 4:
            {
                printf("\nEnter The Numerator\n");
                scanf("%f", & x);
                printf("\nEnter The Denominator\n");
                scanf("%f", & y);
                clrscr();
                result = x / y;
                printf("\nDivision of two numbers %f and %f is %f", x, y,
                    result);
                break;
            }
        default:
            {
                printf("Invalid Choice");
                break;
            }
        }
        printf("\n\nCalculator By XXX\n\nDo you want to Continue (y/n)\n");
        scanf("%c", & cont);
    }
    getch();
}

【问题讨论】:

  • 您是否尝试过调试代码以了解实际发生的情况?
  • 此代码不可读。尝试在发布之前对其进行合理的格式化。
  • 缩进/格式化:(

标签: c while-loop terminate


【解决方案1】:

您的 while 循环终止,因为 scanf("%c",&amp;cont); 从缓冲区中读取剩余的 \n,这使您的 while 语句失败。 您应该将您的 scanf 修改为 scanf(" %c",&amp;cont);

【讨论】:

    【解决方案2】:

    scanf 语句放在while 循环中,或者您可以使用do..while 循环代替while

    do { 
       // code
       printf("Do you want to continue (y/n)"); scanf("%c",&cont);
    
    } while(cout=="y");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-25
      相关资源
      最近更新 更多