Advertisement
whitesurge

C Program (Premature scanf)(Line 54 & 55 for explain)

Sep 5th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5.  
  6. /******************************************************************************
  7.  
  8. C program
  9.  
  10. *******************************************************************************/
  11.  
  12.  
  13.  
  14.  
  15. int main()
  16. {
  17.     setvbuf(stdout, 0, _IONBF, 0);
  18.     setvbuf(stdin,  0, _IONBF, 0);
  19.  
  20.     int x = 0;
  21.     int y = 0;
  22.     int z = 0;
  23.     int cont = 1;
  24.     int option = 0;
  25.  
  26.     printf("Please enter a number: ");
  27.     scanf("%d", &x);
  28.  
  29.     printf("Please enter a number: ");
  30.     scanf("%d", &y);
  31.    
  32.  
  33.  
  34.     while (1){
  35.  
  36.         printf("Would you like to \n");
  37.         printf("1) Take the sum? \n");
  38.         printf("2) Take the product? \n");
  39.         printf("3) Take the difference? \n");
  40.         printf("4) Take the quotient? \n");
  41.         printf("5) Get the remainder? \n");
  42.         printf("6) Compare both integers? \n");
  43.         printf("7) Compare Three integers? \n");
  44.         printf("8) Even or odd? \n");
  45.         printf("9) Is the first the multiple of the second? \n");
  46.         printf("0) Exit \n");
  47.  
  48.         printf("Please enter an option: ");
  49.         scanf("%d \n", &option);
  50.  
  51.         //The is a bug on the terminal after you enter the the scanf for option variable. The terminal asks for input, that then
  52.         //enters the next input into the cont variable. Which it should not be calling and effects the code later
  53.         //The scanf also requests later when called apon by the scanf for cont
  54.  
  55.  
  56.         //Sum
  57.         if(option == 1){
  58.             int full = x + y;
  59.             printf("The sum of %d and %d is %d \n", x,y,full);
  60.  
  61.             printf("Press enter 1 to continue or enter 0 to exit: ");
  62.             scanf("%d \n", &cont);
  63.             if (cont == 1){
  64.  
  65.             }
  66.             else if (cont == 0){
  67.                 break;
  68.             }
  69.         }
  70.  
  71.         //Product
  72.         if(option == 2){
  73.             int full = x * y;
  74.             printf("The product of %d and %d is %d \n", x,y,full);
  75.  
  76.             printf("Press enter 1 to continue or enter 0 to exit: ");
  77.             scanf("%d \n", &cont);
  78.             if (cont == 1){
  79.  
  80.             }
  81.             else if (cont == 0){
  82.                 break;
  83.             }
  84.         }
  85.  
  86.         //Difference
  87.         if(option == 3){
  88.             int full = x - y;
  89.             printf("The sum of %d and %d is %d \n", x,y,full);
  90.  
  91.             printf("Press enter 1 to continue or enter 0 to exit: ");
  92.             scanf("%d \n", &cont);
  93.             if (cont == 1){
  94.  
  95.             }
  96.             else if (cont == 0){
  97.                 break;
  98.             }
  99.         }
  100.  
  101.         //Quotient
  102.         if(option == 4){
  103.             int full = x/y;
  104.             printf("The quotient of %d/%d is %d \n", x,y,full);
  105.  
  106.             printf("Press enter 1 to continue or enter 0 to exit: ");
  107.             scanf("%d \n", &cont);
  108.             if (cont == 1){
  109.  
  110.             }
  111.             else if (cont == 0){
  112.                 break;
  113.             }
  114.         }
  115.  
  116.         //Remainder
  117.         if(option == 5){
  118.             float full = x % y;
  119.             printf("The remainder of %d and %d is %f \n", x,y,full);
  120.  
  121.             printf("Press enter 1 to continue or enter 0 to exit: ");
  122.             scanf("%d \n", &cont);
  123.             if (cont == 1){
  124.  
  125.             }
  126.             else if (cont == 0){
  127.                 break;
  128.             }
  129.         }
  130.  
  131.         //Compare two integers
  132.         if(option == 6){
  133.             if (x > y)
  134.                 printf("First number is %d which is greater than the second number which is %d \n",x,y);
  135.             else if (x < y)
  136.                 printf("Second number is %d which is greater than the first number which is %d \n",y,x);
  137.             else
  138.                 printf("Both numbers are equal to %d \n", x);
  139.  
  140.             printf("Press enter 1 to continue or enter 0 to exit: ");
  141.             scanf("%d \n", &cont);
  142.             if (cont == 1){
  143.  
  144.             }
  145.             else if (cont == 0){
  146.                 break;
  147.             }
  148.         }
  149.  
  150.         //Compare three integers
  151.         if(option == 7){
  152.             printf("Enter a third number to compare with %d and %d :",x,y);
  153.             scanf("%d \n", &z);
  154.             int max = x;
  155.             int min = x;
  156.  
  157.             //Max
  158.             if (x > max){
  159.                 max = x;
  160.             }
  161.  
  162.             if (y > max){
  163.                 max = y;
  164.             }
  165.  
  166.             if (z > max){
  167.                 max = z;
  168.             }
  169.  
  170.             //Min
  171.  
  172.             if (x < min){
  173.                 min = x;
  174.             }
  175.  
  176.             if (y < min){
  177.                 min = y;
  178.             }
  179.  
  180.             if(z < min){
  181.                 min = z;
  182.             }
  183.  
  184.             printf("The max is %d \n",max);
  185.             printf("The min is %d \n",min);
  186.  
  187.             printf("Press enter 1 to continue or enter 0 to exit: ");
  188.             scanf("%d \n", &cont);
  189.             if (cont == 1){
  190.  
  191.             }
  192.             else if (cont == 0){
  193.                 break;
  194.             }
  195.         }
  196.  
  197.         //Even or Odd
  198.         if(option == 8){
  199.             if (x % 2 == 0){
  200.                 printf("%d is Even\n", x);
  201.             }
  202.             else{
  203.                 printf("%d is Odd\n", x);
  204.             }
  205.             if (y % 2 == 0){
  206.                 printf("%d is Even\n", y);
  207.             }
  208.             else{
  209.                 printf("%d is Odd\n", y);
  210.             }
  211.  
  212.             printf("Press enter 1 to continue or enter 0 to exit: ");
  213.             scanf("%d \n", &cont);
  214.             if (cont == 1){
  215.  
  216.             }
  217.             else if (cont == 0){
  218.                 break;
  219.             }
  220.         }
  221.  
  222.         //First a multiple of the second
  223.         if(option == 9){
  224.             if (x % y == 0){
  225.                 printf("%d is a multiple of %d\n", y, x);
  226.             }
  227.             else{
  228.                 printf("%d is not a multiple of %d\n", y, x);
  229.             }
  230.  
  231.             printf("Press enter 1 to continue or enter 0 to exit: ");
  232.             scanf("%d \n", &cont);
  233.             if (cont == 0){
  234.                 return 0;
  235.             }
  236.         }
  237.  
  238.         //Exit
  239.         if(option == 0 || cont == 0){
  240.             break;
  241.         }
  242.  
  243.  
  244.     }
  245.  
  246.     return 0;
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement