简体   繁体   English

我怎样才能使程序请求用户输入以关闭它或从头开始启动它

[英]how can i make the program request user input to either close it or start it from the beginning

i have constructed a program that makes basic calculations of a number the user enters before closing.我构建了一个程序,可以对用户在关闭前输入的数字进行基本计算。 homewever, i would like to make the program ask the user if he wants to input another number and closing if he says no. homewever,我想让程序询问用户是否要输入另一个数字,如果他说不,则关闭。 here is the program for reference.这是供参考的程序。 it asks for four numbers, then sums then:它要求输入四个数字,然后求和:

#include<stdlib.h>
int main(void)
{
  
  float n1, n2, n3,n4, resultado;
  
  
  printf("insira o primeiro numero: ");
  scanf("%f",&n1);
  
  printf("insira o segundo numero: ");
  scanf("%f",&n2);
  
  printf("insira o terceiro numero: ");
  scanf("%f",&n3);
  
  printf("insira o quarto numero: ");
  scanf("%f",&n4);
  
  resultado = (n1 + n2 + n3 + n4) ;
  
  
  printf(" A total soma dos numeros eh = %.1f\n",resultado);
  
  
  return 0;
}```

You can simply surround your code with do while loop, try something like this:您可以简单地用do while循环围绕您的代码,尝试这样的事情:

int main() {
    int user_response;
    do {
        // rest of your code here

        printf("Would you like to do another calculation? (1 - yes, 0 - no) ");
        scanf("%d", &user_response);
    } while (user_response == 1);
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何制作扫描用户输入(文本)并将其保存在动态字符串上的C程序 - How can i make C program that scans user's input(text) and save it on a dynamic string 如何使C程序停止任何用户输入的循环? - How to make a C program stop a loop from any user input? 在C中,我编写了一个使用system()调用exe的程序。 exe要求用户输入。 如何让我的程序提供输入? - In C, I wrote a program to call an exe using system(). The exe asks for user input. How can I make my program provide the input? C套接字程序-更新标准输出的读数时如何接受用户输入? - C Socket program - How can I accept user input while updating reading from stdout? 如何立即关闭 C 中的程序? - How can I immediately close a program in C? 如何修改 c 程序,以便在用户输入非浮点输入时,向用户显示“无效输入”? - How can I modify the c program such that if the user enters a non-float input,“invalid input” is shown to the user? C在后台从其他程序启动程序,然后从该程序关闭 - C Start Program from other Program in Background and close it from the Program 如何在 C 程序中从终端读取输入 - how can I read input from terminal in a C program 如何检查CLI程序是否在等待stdin的输入? - How can I check if a CLI program is waiting for input from stdin? 如何通过输入避免负数,或者如果用户输入负数,程序会给出错误结果? - How can i avoid negative number through input or if user input negative number the program gives bug result?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM