Learning by doing: Reading books and trying to understand the (code) examples
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
868 B

  1. #include <stdio.h>
  2. #include <limits.h>
  3. #include <stdbool.h>
  4. int getNumber(char * s, bool *error);
  5. int main(void) {
  6. int first_operand;
  7. int second_operand;
  8. bool first_error;
  9. bool second_error;
  10. printf("This program computes moduli.\n");
  11. second_operand = getNumber("Enter an integer to serve as the second operand: ", &second_error);
  12. first_operand = getNumber("Now enter the first operand: ", &first_error);
  13. do {
  14. printf("%d %% %d is %d\n", first_operand, second_operand, first_operand % second_operand);
  15. first_operand = getNumber("Now enter the first operand (<= 0 to quit): ", &first_error);
  16. } while (!first_error && !second_error);
  17. return 0;
  18. }
  19. int getNumber(char * s, bool *error) {
  20. int number;
  21. *error = false;
  22. printf("%s", s);
  23. if (!scanf("%d", &number) || number < 0) {
  24. *error = true;
  25. }
  26. return number;
  27. }