Advertisement
piffy

Wrong Calculator

Feb 6th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.30 KB | None | 0 0
  1. /********************************************************
  2.  * Name: Calculator (Version 0 -- prototype)        *
  3.  *                          *
  4.  * Purpose:                     *
  5.  *  Act like a simple 4 function calculator.    *
  6.  *                          *
  7.  * Usage:                       *
  8.  *  Run the program.                *
  9.  *  Type in an operator (+ - * /) and a number. *
  10.  *  The operaton will be performed on the current   *
  11.  *  result and a new result displayed.      *
  12.  *                          *
  13.  * Note:                        *
  14.  *  This is the first attempt at doing this program.*
  15.  *  It only works for + and because of a bug, even  *
  16.  *  that fails.                 *
  17.  ********************************************************/
  18. /*+*/
  19. #include <stdio.h>
  20. char  line[100];/* line of data from the input */
  21. int   result;   /* the result of the calculations */
  22. char  operator; /* operator the user specified */
  23. int   value;    /* value specified after the operator */
  24.  
  25. int main()
  26. {
  27.     result = 0; /* initialize the result */
  28.  
  29.     /* Loop forever (or till we hit the break statement) */
  30.     while (1) {
  31.         printf("Result: %d\n", result);
  32.  
  33.         printf("Enter operator and number: ");
  34.         fgets(line, sizeof(line), stdin);
  35.         sscanf(line, "%c %d", &operator, &value);
  36.  
  37.         if (operator = '+') {
  38.             result += value;
  39.         } else {
  40.             printf("Unknown operator %c\n", operator);
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement