Advertisement
Guest User

Untitled

a guest
Dec 16th, 2010
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. /*********************************************************************
  2. Switch Statement example code by BitterCake
  3. The program:
  4. Just a basic calculator codded by using the Switch Statement
  5. **********************************************************************/
  6.  
  7. #include <iostream> // To use the cin and cout
  8. using namespace std; // So there is no need to type "std::cout" instead you just type "cout"
  9.  
  10. int main()
  11. {
  12. // Declaring the varieables
  13. int x;
  14. int y;
  15. char ch;
  16.  
  17. //Asking the user to enter the first and second number along with the operation he wants to perfume
  18. cout <<"Please enter the first number: " << endl;
  19. cin >> x;
  20. cout <<"And the second number: " << endl;
  21. cin >> y;
  22. cout <<"Enter \" + or - or * or / \" " << endl;
  23. cin >> ch;
  24.  
  25. /*
  26. Now I am going to use the Switch Statement.. some notes that will help you get the idea:
  27. Switch (ch)
  28. case '+'
  29. {
  30. dothis
  31. }
  32. are the same as if (ch == '+') (dothis)
  33. break :: is the same as "else" (meaning we use it so we won't have to check other cases)
  34. default :: means if nothing is true, do whatever comes next
  35. */
  36. switch (ch)
  37. {
  38. case '+':
  39. cout <<"The result is: " << x+y << endl;
  40. break;
  41. case '-':
  42. cout <<"The result is: " << x-y << endl;
  43. break;
  44. case '*':
  45. cout <<"The result is: " << x*y << endl;
  46. case '/':
  47. cout <<"The result is: " << x/y << endl;
  48. default:
  49. cout <<"Invalid code, terminating the program!" << endl;
  50. }
  51.  
  52. //Ending the program with cin.get() which means close the program when the user enters any key on the
  53. cin.get();
  54. cin.get();
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement