简体   繁体   中英

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. 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:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM