Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int math(int a, int b, char selection) {
  4.  
  5. int result;
  6.  
  7. switch (selection) {
  8. case 'a':
  9. result = a + b;
  10. break;
  11. case 's':
  12. result = a - b;
  13. break;
  14. case 'm':
  15. result = a * b;
  16. break;
  17. case 'd':
  18. result = a / b;
  19. break;
  20. }
  21.  
  22. return result;
  23. }
  24.  
  25. int main() {
  26. int num1 = 0;
  27. int num2 = 0;
  28. int result = 0;
  29. char selection;
  30.  
  31. printf("Enter first number: ");
  32. scanf("%i", &num1);
  33.  
  34. printf("Enter second number: ");
  35. scanf("%i", &num2);
  36.  
  37. printf("n[a] Addn[s] Subtractn[m] Multiplyn[d] DividennWhich one: ");
  38. scanf("%c", &selection);
  39.  
  40. result = math(num1, num2, selection);
  41. printf("%i", result);
  42.  
  43. return 0;
  44. }
  45.  
  46. printf("Enter second number: ");
  47. scanf("%i", &num2);
  48.  
  49. you can't see that, but it's there
  50. V
  51. > Enter second number: 3n
  52.  
  53. scanf("%c", &selection);
  54.  
  55. scanf(" %c", &selection);
  56.  
  57. printf("n[a] Addn[s] Subtractn[m] Multiplyn[d] DividennWhich one: ");
  58. int ret = scanf("%c", &selection);
  59.  
  60. printf("Scanf I think failed. Return value is: %d. Selection is: %c (%d)n",
  61. ret, selection, selection);
  62.  
  63. Scanf I think failed. Return value is: 1. Selection is:
  64. (10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement