Code using C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Saba
    New Member
    • Oct 2006
    • 16

    Code using C++

    Create the equivalents of a four-function calculator. The program should request the user to enter a number, an operator, and another number. (Use floating point). It should then carry out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers. Use switch case statement to select the operation. Finally display the result.
    When it finishes the calculation, the program should ask if the user wants to do another calculation. The response can be ā€˜y’ or ā€˜n’.

    Please help me in writing the code.
  • tyreld
    New Member
    • Sep 2006
    • 144

    #2
    Generally, nobody is going to just write code for something that looks like homework or a learning exercise. Try it yourself. This is the best way to learn. When you get stuck post your code and ask specific questions about what is posing a problem. Once you do this people will be more likely to give you help and feedback. This applies to your other 2 posts.

    Comment

    • Saba
      New Member
      • Oct 2006
      • 16

      #3
      i have tried but i got struck at one place.....plz help me if u can....
      i am writting the code.....when i compile.....it does not switch again when i enter "y".....plz help me as soon as possible.
      #include <iostream.h>

      main()
      {
      int choice;
      float firstNum, secondNum;
      char oper, y, Y, n, N;

      do
      {
      cout<< "Please enter the first number: " << endl;
      cin >> firstNum;

      cout << "Please enter the second number: " << endl;
      cin >> secondNum;

      cout << "Please enter the operator: "<<endl;
      cin>> oper;

      switch(oper)
      {
      case '+':
      cout<< "Sum = " << firstNum + secondNum << endl;
      break;

      case '-':
      cout<< "Difference = " << firstNum - secondNum << endl;
      break;

      case '*':
      cout<< "Product = " << firstNum * secondNum << endl;
      break;

      case '/':
      cout<< "Divsion = " << firstNum / secondNum << endl;
      break;

      }

      cout<< "Do you want to do more calculations(y/n)?";
      cin>> choice;
      }
      while ( (choice==y) && ( choice==Y) );

      }

      Comment

      • tyreld
        New Member
        • Sep 2006
        • 144

        #4
        There are 3 things wrong.

        First, the variable "choice" should be of type "char".

        Code:
        char choice;
        Second, the comparison should be against char literals not undefined variables as you have it now.

        Code:
        (choice == 'y')
        Finally, the while condition should be OR'ed not AND'ed. You want to check that the input to choice was either 'y' ***OR*** 'Y'. The AND operation would imply that choice have both 'y' and 'Y' as a value. Which isn't possible.

        Code:
        while ((choice == 'y') || (choice == 'Y'));

        Comment

        • tyreld
          New Member
          • Sep 2006
          • 144

          #5
          On a side note. Your main method should have one of the following forms:
          int main(void) { ... }

          or
          int main(int argc, char **argv) { ... }

          Your main shold always return a value. You either need to return 0 for success or one of the macros defined in <stdlib.h> or <cstdlib>. Namely "EXIT_SUCCE SS" and "EXIT_FAILU RE."

          Code:
          #include <stdlib.h>
          
          int main(void)
          {
             // do a bunch of stuff
          
             return EXIT_SUCCESS;
          }

          Comment

          • AR JAlbani
            New Member
            • Sep 2016
            • 2

            #6
            four-function calculator in c++

            #include <iostream>
            using namespace std;
            int main()
            {
            char op; // ** must be outside loop
            do // ** start loop
            {
            int number1, number2;
            cout << " Enter a number: ";
            cin >> number1;
            cout << "Enter another number: ";
            cin >> number2;
            cout << "Enter a valid operator: ";

            cin >> op;
            switch (op)
            {
            case '+':
            cout << "The result is: " << number1 + number2 << endl;
            break;
            case '-':
            cout << "The result is: " << number1 - number2 << endl;
            break;
            case '*':
            cout << "The result is: " << number1 * number2 << endl;
            break;
            case '/':
            cout << "The result is: " << number1 / number2 << endl;
            break;
            default: cout << "You Have entered an invalid operator." << endl;
            }
            cout<< "do you want to another calculation? (y or n)";
            cin >> op;
            }
            while (op == 'y'); // ** loop while y is entered
            return 0;
            }

            Comment

            • AR JAlbani
              New Member
              • Sep 2016
              • 2

              #7
              /* Source code to create a simple calculator for addition, subtraction, multiplication and division using switch...case statement in C++ programming. */

              # include <iostream>
              using namespace std;
              int main()
              {
              char o;
              float num1,num2;
              cout << "Enter operator either + or - or * or /: ";
              cin >> o;
              cout << "Enter two operands: ";
              cin >> num1 >> num2;
              switch(o) {
              case '+':
              cout << num1+num2;
              break;
              case '-':
              cout << num1-num2;
              break;
              case '*':
              cout << num1*num2;
              break;
              case '/':
              cout << num1/num2;
              break;
              default:
              /* If operator is other than +, -, * or /, error message is shown */
              cout << "Error! operator is not correct";
              break;
              }
              return 0;
              }

              Comment

              Working...