Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********************************************************************
- Switch Statement example code by BitterCake
- The program:
- Just a basic calculator codded by using the Switch Statement
- **********************************************************************/
- #include <iostream> // To use the cin and cout
- using namespace std; // So there is no need to type "std::cout" instead you just type "cout"
- int main()
- {
- // Declaring the varieables
- int x;
- int y;
- char ch;
- //Asking the user to enter the first and second number along with the operation he wants to perfume
- cout <<"Please enter the first number: " << endl;
- cin >> x;
- cout <<"And the second number: " << endl;
- cin >> y;
- cout <<"Enter \" + or - or * or / \" " << endl;
- cin >> ch;
- /*
- Now I am going to use the Switch Statement.. some notes that will help you get the idea:
- Switch (ch)
- case '+'
- {
- dothis
- }
- are the same as if (ch == '+') (dothis)
- break :: is the same as "else" (meaning we use it so we won't have to check other cases)
- default :: means if nothing is true, do whatever comes next
- */
- switch (ch)
- {
- case '+':
- cout <<"The result is: " << x+y << endl;
- break;
- case '-':
- cout <<"The result is: " << x-y << endl;
- break;
- case '*':
- cout <<"The result is: " << x*y << endl;
- case '/':
- cout <<"The result is: " << x/y << endl;
- default:
- cout <<"Invalid code, terminating the program!" << endl;
- }
- //Ending the program with cin.get() which means close the program when the user enters any key on the
- cin.get();
- cin.get();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement