How do I if the user type a number other than 1 and 2 he asks you to terminate the program? n[1] for yes and [2] for no?

Asked

Viewed 54 times

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

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

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

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 the getch(); it would be necessary to include the library conium. h

-3

//Use a fução 
main()
{
     do
     {
          _________
          _________
          _________
          _________
     }while(decisao==não)
}

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.