如何从头开始重复 ac 程序并清理屏幕和第一个输入值?

Max*_*ark 3 c loops console-application

我是编程新手。我写了一个简单的程序。我想一次又一次地重复该程序,并且只有当用户想要退出时它才能退出。这是我的程序

#include<stdio.h>
#include<conio.h>
main()
{
    char ch;
    int num1, num2, a, m, s, choice;
    float d;
    printf("\nEnter The First Number: ");
    scanf("%d", &num1);
    printf("\nEnter The Second Number: ");
    scanf("%d", &num2);
    a=num1+num2;
    m=num1*num2;
    s=num1-num2;
    d=(float)(num1/num2);
    printf("\nEnter Your Choice \nFor Addition Type A \nFor Multipication Type M \nFor Division Type D \nFor Substraction Type S : ");
    scanf(" %c", &ch);
    switch(ch)
        {
            case 'A': printf("\nThe Addition Of The Number Is= %d", a);
                break;
            case 'M': printf("\nThe Multipication Of The Numbers Is= %d", m);
                break;
            case 'S': printf("\nThe Substraction Of THe Numbers Is= %d", s);
                break;
            case 'D': printf("\nThe Division Of The Two Numbers Is= %f", d);
                break;
            default : printf("\nInvalid Entry");
                break;
        }
    printf("\nPress Any Key To Exit");
    getch();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是输出

"输入第一个数字:10

输入第二个数字:10

输入您的选择

对于添加类型 A

对于乘法类型 M

对于 D 型部门

对于减法类型 S :A

数字相加 = 20

按任何一个键退出

我想要在该行之前添加一行按任意键退出

"如果您想再次计算请按 Y

或者

按任何一个键退出

当按Y时,程序应该从头开始。

我怎样才能做到这一点???

Uni*_*ash 5

将代码包装在do{} while()?中

char answer;
do{
printf("\nEnter The First Number: ");
scanf("%d", &num1);
printf("\nEnter The Second Number: ");
scanf("%d", &num2);
a=num1+num2;
m=num1*num2;
s=num1-num2;
d=(float)(num1/num2);
printf("\nEnter Your Choice \nFor Addition Type A \nFor Multipication Type M \nFor Division Type D \nFor Substraction Type S : ");
scanf(" %c", &ch);
switch(ch)
    {
        case 'A': printf("\nThe Addition Of The Number Is= %d", a);
            break;
        case 'M': printf("\nThe Multipication Of The Numbers Is= %d", m);
            break;
        case 'S': printf("\nThe Substraction Of THe Numbers Is= %d", s);
            break;
        case 'D': printf("\nThe Division Of The Two Numbers Is= %f", d);
            break;
        default : printf("\nInvalid Entry");
            break;
    }
printf("\nPress Y to continue. Press any Key To Exit");
scanf(" %c",&answer); // dont forget type &
}
while(answer == 'y' || answer == 'Y');
Run Code Online (Sandbox Code Playgroud)

声明一个变量,假设为答案,当您询问“按 Y 继续。按任意键退出”时,该变量将存储用户答案。检查该变量的值是什么。如果是“y”或“Y”,则循环将重复。如果用户按下其他键,则循环结束。