【问题标题】:Ask user to repeat the program or exit in C要求用户重复程序或退出 C
【发布时间】:2016-01-22 07:57:11
【问题描述】:

在这个非常基本的程序中,它要求用户输入两个数字,然后程序会将这些数字相加。最后我想问用户他/她是想再次重复程序还是退出程序!例如,如果他/她按 y,程序将重新要求用户输入两个数字,否则程序将关闭。怎么办?

main(){
float x,y,sum;
printf ("Enter the first number:");
scanf ("%f",&x);
printf ("Enter the second number:");
scanf ("%f",&y);
sum=x+y;
printf ("The total number is:%f",sum);
}

【问题讨论】:

  • 一个while 循环,同时读取一个char
  • 您的代码嵌套在 do while 循环中,这就是答案
  • 你真的应该花一些时间阅读一些关于 C 编程的书,阅读标准函数的文档,如 scanfprintf。您的 main 声明错误。您应该在编译时启用所有警告和调试信息 (gcc -Wall -Wextra -g)。您应该使用调试器。投票结束您的问题作为修复我的代码请求。
  • @yaya,检查我的答案,它有一个 do-while 循环的教程。这可能会对您有所帮助。

标签: c printf


【解决方案1】:
main(){
    float x,y,sum;
    char ch;

    do{
    printf ("Enter the first number:");
    scanf ("%f",&x);
    printf ("Enter the second number:");
    scanf ("%f",&y);
    sum=x+y;
    printf ("The total number is:%f",sum);
    printf ("Do you want to continue: y/n");
    scanf (" %c", &ch);
    } while(ch == 'y');
    }

或者你也可以试试这个:

main(){
    float x,y,sum;
    char ch;

    do{
    printf ("Enter the first number:");
    scanf ("%f",&x);
    printf ("Enter the second number:");
    scanf ("%f",&y);
    sum=x+y;
    printf ("The total number is:%f",sum);
    printf ("Do you want to continue: y/n");
    ch = getchar();
    getchar();
    } while(ch == 'y');
    }

【讨论】:

  • 这不是main 的正确声明。它必须是 int main ()int main (void)int main (int argc, char **argv)
  • @AshishAhuja 这是旧 C90 标准中允许的形式。考虑到编译器记录了 main() 的形式,它也可以在任何 C 编译器上被允许。
  • @Lundin,gcc 记录了吗?
  • @AshishAhuja 在 C90 中当没有使用显式类型的函数时,隐式使用 int
  • @AshishAhuja -ffreestanding 已正确记录并允许任何形式。当为托管(默认/-fhosted)编译时,gcc 不允许main(),除非您告诉它针对旧的 C90(-std=c90)标准进行编译。然后它只会发出警告。
【解决方案2】:
int main(void) {
float x,y,sum;
char ch;
do {
printf ("Enter the first number:");
scanf ("%f",&x);
printf ("Enter the second number:");
scanf ("%f",&y);
sum=x+y;
printf ("The total number is:%f\n",sum);
printf ("Do you want to repeat the operation Y/N: ");
scanf (" %c", &ch);
}
while (ch == 'y' || ch == 'Y');
}

这使用do-while 循环。它将一直持续到do-whilewhile 中的条件返回false。

简单来说,只要用户输入yYwhile 循环就会返回 true。因此,它将继续。

查看this 示例和do-while 循环教程。

【讨论】:

    【解决方案3】:

    [在这个程序中我使用'goto'语句,因为如果我使用do while循环,那么如果我输入任何没有“Y或y”的东西,那么程序将关闭。为了避免这个问题,我使用'goto'语句。@ 987654321@

    #include<stdio.h>
    int main(){
    float x, y, sum;
    char ch;
    print:
        printf ("Enter the first number:");
        scanf ("%f",&x);
        printf ("Enter the second number:");
        scanf ("%f",&y);
        sum=x+y;
        printf ("\nThe total number is:%.2f\n",sum);
    again:
        printf ("\n\t\t\t\t\tDo you want to repeat the operation(Y/N): ");
        scanf (" %c", &ch);
    
        if(ch == 'y' || ch == 'Y'){
            goto print;
        }
        else if(ch == 'n' || ch == 'N'){
            return 0;
        }
        else{
            printf("\n\t\t\t\t\tPlease enter Yes or NO.\n");
            goto again;
        }
       return 0;}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-21
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-29
      相关资源
      最近更新 更多