1
Reply

Calculator Using Call Functions and Switch Statement, not working can you help me I am a beginner!

Q F

Q F

Oct 2 2012 7:32 PM
1.9k


/*This program will perform calculations similar to a calculator by asking user what type of mathematical mode to perform, obtaining 2 inputs and performing operation. Contains 1 function for input, one function for outputs, and one function to perform mathematical functions.*/

#include <iostream>
#include <cstdlib>
using namespace std;

int menu_func();
int user_input();
int calc_output(int, int, int);

int main()
{
    int function= menu_func();
    int userinput1= user_input();
    int userinput2= user_input();
    int answer= calc_output(function, userinput1, userinput2);

    cout << function << endl;
       cin >> function;

    cout << userinput1;
       cin >> userinput1;

    cout << userinput2;
       cin >> userinput2;

    cout <<" The answer is " << answer << endl;

    return 0;
}


//request user to choose a mathematical operation
int menu_func()
{
    int choice;

    cout << "Please choose a mathematical function by selecting "
         << "a number " << endl;
    cout << "1 Addition " << endl;
    cout << "2 Subtraction " << endl;
    cout << "3 Multiplication " << endl;
    cout << "4 Division " << endl;
    cout << "5 Exit " << endl;
    cout << "Enter choice: ";
    cin >> choice;

    return (choice);
}

//obtain number inputs from user
int user_input()
{
    int numberchoice;

    cout << "Please input a number: ";
    cin >> numberchoice;

    return (numberchoice);
}

//calculations of chosen operator and numbers
int calc_output(int option, int input1, int input2)
{
    while ( option < 5 || option > 0 && option != 5 );

    switch (option)
    {
        case '1':
            int add = (input1 + input2);
            return (add);
            break;
        case 2:
            int subtract = (input1 - input2);
            return (subtract);
            break;
        case 3:
            int multiply = (input1 * input2);
            return (multiply);
            break;
        case 4:
            int divide = (input1 / input2);
            return (divide);
            break;
        default:
            cout << "Error " << endl;
    }

}
//calculations of chosen operator and numbers
int calc_output(int option, int input1, int input2)
{
    while ( option < 5 || option > 0 && option != 5 );

    switch (option)
    {
        case '1':
            int add = (input1 + input2);
            return (add);
            break;
        case 2:
            int subtract = (input1 - input2);
            return (subtract);
            break;
        case 3:
            int multiply = (input1 * input2);
            return (multiply);
            break;
        case 4:
            int divide = (input1 / input2);
            return (divide);
            break;
        default:
            cout << "Error " << endl;
    }

}


Answers (1)