[英]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.