user11560496
user11560496

Reputation:

Looping for calculators

The program will take in floating-point numbers and operators from the user and perform the required calculations, printing out the results. The result of each calculation will serve as an operand for the next calculation.

So, I am not sure if I am doing this correctly by using switch statements ? Is there a better way of doing it ? Maybe, by using a do - while loop ? I am really confused.

#include <iostream>
#include <iomanip>

using namespace std; 

int main()
{
float firstOperand;
cout << "Enter a number: ";
cin >> firstOperand;
cout << endl;

cout << "Choose an instruction code: " << endl << endl;
cout << "1) (+) for addition. " << endl;
cout << "2) (*) for multiplication. " << endl;
cout << "3) (p) for power. " << endl;
cout << "4) (c) to clear the current result. " << endl;
cout << "5) (-) for subtraction. " << endl;
cout << "6) (/) for divison. " << endl;
cout << "7) (s) for square root. " << endl;
cout << "8) (q) to quit the program. " << endl << endl;

int choice;
cin >> choice;
cout << endl;

float secondOperand;
cout << "Enter the second number: ";
cin >> secondOperand;
cout << endl;



switch (choice)
{
case 1:


    {
        float resultOne = firstOperand + secondOperand;
        cout << "The result of the calculation is " << resultOne << endl;

    }


case 2:
{

    float thirdOperand;
    cout << "Enter another number ";
    cin >> thirdOperand;
    cout << endl;

    cout << "Choose an instruction code: " << endl << endl;
    cout << "1) (+) for addition. " << endl;
    cout << "2) (*) for multiplication. " << endl;
    cout << "3) (p) for power. " << endl;
    cout << "4) (c) to clear the current result. " << endl;
    cout << "5) (-) for subtraction. " << endl;
    cout << "6) (/) for divison. " << endl;
    cout << "7) (s) for square root. " << endl;
    cout << "8) (q) to quit the program. " << endl << endl;

    float resultTwo = resultOne + thirdOperand;
    cout << "The result of the calculation is " << resultTwo << endl;
}



    break;

}


system("pause");
return 0;
}

Upvotes: 2

Views: 91

Answers (2)

Parth Sarthi Sharma
Parth Sarthi Sharma

Reputation: 445

Try this code:

#include <iostream>
#include <math.h>

using namespace std; 

int main(){
    float firstOperand, secondOperand, result;
    int choice, quit = 0;

    cout << "Enter a number: ";
    cin >> firstOperand;
    cout << endl;

    while (1){
        cout << "The first operand is " << firstOperand << endl;

        cout << "\nChoose an instruction code: " << endl;
        cout << "1) (+) for addition. " << endl;
        cout << "2) (*) for multiplication. " << endl;
        cout << "3) (p) for power. " << endl;
        cout << "4) (c) to clear the current result. " << endl;
        cout << "5) (-) for subtraction. " << endl;
        cout << "6) (/) for divison. " << endl;
        cout << "7) (s) for square root. " << endl;
        cout << "8) (q) to quit the program. " << endl << endl;

        cin >> choice;
        cout << endl;

        if (choice == 8){
            cout << "Quitting the program..." << endl;
            break;
        }

        cout << "Enter the second number: ";
        cin >> secondOperand;
        cout << endl;

        switch(choice){
            case 1: result = firstOperand + secondOperand;
                    cout << "The result of the calculation is " << result << endl;
                    firstOperand = result;
                    break;


            case 2: result = firstOperand * secondOperand;
                    cout << "The result of the calculation is " << result << endl;
                    firstOperand = result;
                    break;

            case 3: result = pow(firstOperand, secondOperand);
                    cout << "The result of the calculation is " << result << endl;
                    firstOperand = result;
                    break;

            case 4: result = 0;
                    cout << "The result has been cleared to " << result << endl;
                    cout << "Enter the first operand: ";
                    cin >> firstOperand;
                    cout << endl;
                    break;

            case 5: result = firstOperand - secondOperand;
                    cout << "The result of the calculation is " << result << endl;
                    firstOperand = result;
                    break;

            case 6: if(secondOperand){
                        result = firstOperand / secondOperand;
                        cout << "The result of the calculation is " << result << endl;
                        firstOperand = result;
                        break;
                    }
                    else{
                        cout << "Second operand is " << secondOperand << "Choose again!" << endl;
                    }
            case 7: result = sqrt(secondOperand);
                    cout << "The result of the calculation is " << result << endl;
                    firstOperand = result;
                    break;

            default:cout << "Invalid input. Enter again!" << endl;
                    break;
        }
    }
}

The main code is wrapped inside an infinite while loop with a quit condition. The break statement is used to exit the while loop when the user wishes to. Note: The input for the operator is the number and not the symbol. You will have to change choice variable to char in case you want to use the symbol as the input.

Update: The following code is for character inputs.

#include <iostream>
#include <math.h>

using namespace std; 

int main(){
    float firstOperand, secondOperand, result;
    int quit = 0;
    char choice;

    cout << "Enter a number: ";
    cin >> firstOperand;
    cout << endl;

    while (1){
        cout << "The first operand is " << firstOperand << endl;

        cout << "\nChoose an instruction code: " << endl;
        cout << "1) (+) for addition. " << endl;
        cout << "2) (*) for multiplication. " << endl;
        cout << "3) (p) for power. " << endl;
        cout << "4) (c) to clear the current result. " << endl;
        cout << "5) (-) for subtraction. " << endl;
        cout << "6) (/) for divison. " << endl;
        cout << "7) (s) for square root. " << endl;
        cout << "8) (q) to quit the program. " << endl << endl;

        cin >> choice;
        cout << endl;

        if (choice == 'q'){
            cout << "Quitting the program..." << endl;
            break;
        }

        cout << "Enter the second number: ";
        cin >> secondOperand;
        cout << endl;

        switch(choice){
            case '+': result = firstOperand + secondOperand;
                    cout << "The result of the calculation is " << result << endl;
                    firstOperand = result;
                    break;


            case '*': result = firstOperand * secondOperand;
                    cout << "The result of the calculation is " << result << endl;
                    firstOperand = result;
                    break;

            case 'p': result = pow(firstOperand, secondOperand);
                    cout << "The result of the calculation is " << result << endl;
                    firstOperand = result;
                    break;

            case 'c': result = 0;
                    cout << "The result has been cleared to " << result << endl;
                    cout << "Enter the first operand: ";
                    cin >> firstOperand;
                    cout << endl;
                    break;

            case '-': result = firstOperand - secondOperand;
                    cout << "The result of the calculation is " << result << endl;
                    firstOperand = result;
                    break;

            case '/': if(secondOperand){
                        result = firstOperand / secondOperand;
                        cout << "The result of the calculation is " << result << endl;
                        firstOperand = result;
                        break;
                    }
                    else{
                        cout << "Second operand is " << secondOperand << "Choose again!" << endl;
                    }
            case 's': result = sqrt(secondOperand);
                    cout << "The result of the calculation is " << result << endl;
                    firstOperand = result;
                    break;

            default: cout << "Invalid input. Enter again!" << endl;
                    break;
        }
    }
}

Upvotes: 2

Christina Jacob
Christina Jacob

Reputation: 685

Here is one possible version on the code:

   #include <iostream>
   #include <iomanip>
   #include <cmath>
   using namespace std; 
int select_operation()
{
    int choice;
    cout << "Choose an instruction code: " << endl << endl;
    cout << "1) (+) for addition. " << endl;
    cout << "2) (*) for multiplication. " << endl;
    cout << "3) (p) for power. " << endl;
    cout << "4) (c) to clear the current result. " << endl;
    cout << "5) (-) for subtraction. " << endl;
    cout << "6) (/) for divison. " << endl;
    cout << "7) (s) for square root. " << endl;
    cout << "8) (q) to quit the program. " << endl << endl;
    cin >> choice;
    cout << endl;
    return choice;
}
int main()
{
    float secondOperand;
    float firstOperand;
    float thirdOperand;
    float resultOne;
    int choice;

    cout << "Enter a number: ";
    cin >> firstOperand;
    cout << endl;
    choice = select_operation();
    if (choice == 8)
            return 0;
    else if(choice == 7)

 secondOperand = .5;
    else{
            cout << "Enter the second number: ";
            cin >> secondOperand;
    }
    cout << endl;

    while(1)
    {
            switch (choice) {
                    case 1:
                            resultOne = firstOperand + secondOperand;
                            cout << "The result of the calculation is " << resultOne << endl;
                            break;
                    case 2:
                            resultOne = firstOperand * secondOperand;
                            cout << "The result of the calculation is " << resultOne << endl;
                            break;
                    case 3:
                            resultOne = pow(firstOperand,secondOperand);
                            cout << "The result of the calculation is " << resultOne << endl;
                            break;
                    case 4:
                            resultOne = 0;
                            cout << "The result of the calculation is " << resultOne << endl;
                            break;
                    case 5:
                            resultOne = firstOperand - secondOperand;
                            cout << "The result of the calculation is " << resultOne << endl;
                            break;
                    case 6:
                           if(secondOperand){
                                  resultOne = firstOperand / secondOperand;
                                  cout << "The result of the calculation is " << resultOne << endl;
                            }
                            break;
                    case 7:
                            resultOne = pow(firstOperand,secondOperand);
                            cout << "The result of the calculation is " << resultOne << endl;
                            break;
            }

            choice = select_operation();
            if (choice == 8)
                    return 0;
            else if(choice == 7)
                    secondOperand = .5;
            else{
                    cout << "Enter another number ";
                    cin >> thirdOperand;
                    secondOperand = thirdOperand;
            }
    }
    cout << endl;
    firstOperand =  resultOne;
    return 0;

}

The above code accepts two inputs at first immediately returns if it is option 8(quit the program). if the operation is square root as it is a unary operation not taking in second operand. Reading the 3rd operand and operator is continued until it is requested to stop.

Upvotes: 2

Related Questions