How do I do if the user enters a number other than 1 and 2 he asks do you want to end the program? \ n [1] for yes and [2] for no? [duplicate]

-5
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() 
{
int var1, var2, Q, R, decisao;
    printf("Dividindo dois numeros\n\n");
    printf("Digite o dividendo: ");
    scanf("%d", &var1);
    printf("Digite o divisor: ");
    scanf("%d", &var2);
    Q = var1 / var2;
    R = var1 % var2;
    printf("Resultado: %d\n", Q);
    printf("Resto: %d\n", R);


    printf("\nDeseja encerrar o programa?[1] para sim e [2] para nao..\nOpcao: ");
    scanf("%d", &decisao);

    if(decisao == 2)
        return main(); 

        if(decisao ==1);
        exit (0);


        system("PAUSE>>NULL");
        return 0;
}
    
asked by anonymous 14.06.2018 / 15:38

2 answers

-2
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() 
{
int var1, var2, Q, R, decisao = 2;
while(decisao == 2){
    printf("Dividindo dois numeros\n\n");
    printf("Digite o dividendo: ");
    scanf("%d", &var1);
    printf("Digite o divisor: ");
    scanf("%d", &var2);
    Q = var1 / var2;
    R = var1 % var2;
    printf("Resultado: %d\n", Q);
    printf("Resto: %d\n", R);


    printf("\nDeseja encerrar o programa?[1] para sim e [2] para nao..\nOpcao: ");
    scanf("%d", &decisao);
}
        printf("\nPrograma finalizado");
        system("PAUSE>>NULL");
        return 0;
}

Notice that I added a while, that is, as long as the number the user types is equal to 2, the replay will continue. Also when declaring the variable "continue" was assigned the value 2

It would be better to do the repetition this way:

char ch = 'S';

while(ch == 'S' || ch == 's'){
**FAÇA**

printf("Para continuar pressione "S" para sair digite qualquer outra tecla");
ch = getch();
}

To use getch (); you would need to include the conio.h     

15.06.2018 / 05:29
-3
//Use a fução 
main()
{
     do
     {
          _________
          _________
          _________
          _________
     }while(decisao==não)
}

// Using this, what's inside will only happen as long as it does not, but that's just the basics. You have to know strings to do what I'm saying.

    
15.06.2018 / 02:39