1
#include <stdio.h>

int main() {
  int input;
  do {
    printf("Welcome to the Arithmetic Quiz Program!\n\n");
    printf("Please choose one of the following by entering the correspoding "
           "number:\n\n");
    printf("1. Give me an addition problem.\n");
    printf("2. Give me a subtraction problem.\n");
    printf("3. Give me a multiplication problem.\n");
    printf("4. Quit.\n\n");

    scanf(" %i", &input);

  } while (input != 1 && input != 2 && input != 3 && input != 4);
  return 0;
}

When I enter something that's not integer (ex: 'a','b',..), it turns out like this:

tadman
  • 208,517
  • 23
  • 234
  • 262
Ducc
  • 31
  • 1
  • 1
    Does this answer your question? [scanf() leaves the new line char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer) – raina77ow Apr 16 '21 at 07:10
  • [This answer](https://stackoverflow.com/a/66264109/1229023) is particularly interesting. – raina77ow Apr 16 '21 at 07:11
  • `scanf` also returns the number of fields it read correctly, you should not ignore it. – rustyx Apr 16 '21 at 07:12
  • 2
    A) C/C++ is not a language. You can only compile and run it as either C *or* C++. B) Please do not post screenshots of plain-text data. Copy-paste that into your question instead. It helps considerably when adapting your question into an answer. – tadman Apr 16 '21 at 07:15
  • What this really needs is `if (input == 4) break`. – tadman Apr 16 '21 at 08:10
  • I'm sorry for not making it clear but the loop went on forever and I couldn't copy it. What I entered is a input that's not integer (ex: char). – Ducc Apr 17 '21 at 07:55

0 Answers0