How do I 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

-2

If the user types a number other than 1 and 2, he asks to terminate the program? \ n [1] for yes and [2] for no

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

int main()
{
    int N1, N2, Q, R, OP;
    {
    do
    {
    printf("DIVIDINDO DOIS NUMEROS: \n\n");
    printf("Digite o dividendo: ");
    scanf("%d", &N1);
    printf("Digite o divisor:   ");
    scanf("%d", &N2);
    Q = N1 / N2;
    R = N1 % N2;
    printf("\nResultado:       %d\n", Q);
    printf("Resto:            %d\n", R);
    printf("\n\nDeseja encerrar o programa?\n[1] para sim e [2] para nao.\n");
    printf("\nOpcao: ");
    scanf("%d", &OP);
    printf("\n \n");
    }
    while (OP == 2);
    if (OP >= 1)
    {
        exit(0);
    }
    system("PAUSE>>NULL");
    return 0;
    }
}
    
asked by anonymous 12.06.2018 / 15:22

1 answer

1

Your code is almost correct, just needs some changes, logic and this as long as it is different from 2 it will repeat, would also give to do with infinite looping but not very cool to do

Your corrected code

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

int main()
{
  int N1, N2, Q, R, OP;
  do
  {
    printf("DIVIDINDO DOIS NUMEROS: \n\n");
    printf("Digite o dividendo: ");
    scanf("%d", &N1);
    printf("Digite o divisor:   ");
    scanf("%d", &N2);
    Q = N1 / N2;
    R = N1 % N2;
    printf("\nResultado:       %d\n", Q);
    printf("Resto:            %d\n", R);
    printf("\n\nDeseja encerrar o programa?\n[1] para sim e [2] para nao.\n");
    printf("\nOpcao: ");
    scanf("%d", &OP);
    printf("\n \n");
  }
   while (OP != 2);
   system("pause");
   return 0;
}
    
12.06.2018 / 16:17