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 62 times

-2

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

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

1 answer

1

Your code is almost correct, just need some changes, the logic and this while it is different from 2 it will repeat, also would do with infinite looping but is 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;
}

Browser other questions tagged

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