Trying to make an advanced calculator for a class project.

Trying to make an advanced calculator for a class project.
instructions are:

o Instead of using the variables: num1 and num2 create an array variable named NumberInputs of size 2 to save input in from the keyboard...
o Change the if-else statement in to a switch statement
o Presents errors cleanly, followed by providing instructions for the user
o After the calculation is complete, the program presents the menu again for a new calculation
o Ensure that the calculator can handle large numbers
o Make function that are called for each calculation. For example:
 <data_type> Addition(<data_type> <variable>, <data_type> <variable>)
 <data_type> Subtraction (<data_type> <variable>, <data_type> <variable>)
 <data_type> Multiplication (<data_type> <variable>, <data_type> <variable>)
 <data_type> Division (<data_type> <variable>, <data_type> <variable>)
 <data_type> Modulus (<data_type> <variable>, <data_type> <variable>)
 <data_type>Exponents (<data_type> <variable>, <data_type> <variable>)
• ***Note: <data_type> <variable> should be updated for each function with the correct parameter information. As well as the <data_type> at the begin of the function
o Save the output to the screen and to a file call “calculationOutput.out”


o Add a menu for the user to pick his/her calculation

To do Addition Press ‘A1’ :
To do Subtraction Press ‘S2’
To do Multiplication Press ‘M3’
To do Division Press ‘D4’
To do Modulus Press ‘M5’
To do Exponents Press ‘E6’
To Exit Press ‘E7’

Please choose a calculation: A1
Please enter the first number: 1.23
Pease enter the second number: 1.23
The answer is : 1.23 + 1.23 = 2.46
To do Addition Press ‘A1’ :
To do Subtraction Press ‘S2’
To do Multiplication Press ‘M3’
To do Division Press ‘D4’
To do Modulus Press ‘M5’
To do Exponents Press ‘E6’
To Exit Press ‘E7’

Please choose a calculation: E7
Good Bye


This is where I have my code so far...
Need some help please.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
 #include <iostream>
#include <iomanip> 
#include <cmath>
using namespace std;

double Addition(double x, double y);
double Subtraction(double x, double y);
double Multiplication(double x, double y);
double Divide(double x, double y);
double Modulus(double x, double y);
double Exponents(double x, double y);
 
int main() {
	    std::cout << std::fixed << std::setprecision(2);double result, x, y;
		char op, A1, S2, M3, D4, M5, E6, E7;
		char calculation;
	
while (true)


		1. To do Addition Press A1 
		2. To do Subtraction Press S2
		3. To do Multiplication Press ‘M3’
		4. To do Division Press ‘D4’
		5. To do Modulus Press ‘M5’
		6. To do Exponents Press ‘E6’
		7. To Exit Press ‘E7’

		cout << "Please choose a calculation: " <<endl;   
		cin >> calculation;
		cout << "Please enter the first number: " <<endl;
		cin >> x;
		cout << "Pease enter the second number: " <<endl;
		cin >> y;


    switch (calculation) 
	{
        case '+': A1 = Addition;
        case '-': S2 = Subtraction;
		case '*': M3 = Multiplication;
		case '/': D4 = Divide;
		case '^': M5 = Modulus;
		case '%': E6 = Exponents;  
		default : E7;
		break;

    }

   cout << "The answer is : " << x << calculation << y << " = " <<  result << endl;




	system ("pause");
    return 0;
}


double Addition(double x, double y) 
{
	return x+y; 
}

double Subtraction(double x, double y) 
{
	return x-y; 
}

double Multiplication(double x, double y)
{
	return x*y; 
}

double Divide(double x, double y)
{
	return x/y; 
}

double Modulus(double x, double y)
{
	return static_cast<int>(x) % static_cast<int>(y); 
}

double Exponents(double x, double y)
{
	return pow(x, y); 
}
Last edited on
My effort:
Didn't know what the keys A1, S2, M3, D4, M5, E6, E7 refer to so below you just select the menu number 1 to 7.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

double Addition(double x, double y);
double Subtraction(double x, double y);
double Multiplication(double x, double y);
double Divide(double x, double y);
double Modulus(double x, double y);
double Exponents(double x, double y);

int    choice = 0;
char   opTypes[6] = {'+','-','*','/','%','^'};
char   op;
double result=0.0;
double x=0.0;
double y=0.0;

int getChoice()
{
    int selection = 0;
    while (selection < 1 || selection > 7)
    {
        cout
        << "1. Addition" << endl
        << "2. Subtraction" << endl
        << "3. Multiplication" << endl
        << "4. Division" << endl
        << "5. Modulus" << endl
        << "6. Exponents" << endl
        << "7. Exit" << endl;
        cin >> selection;
    }
    return selection;
}

int main() {
    choice = getChoice();
    while (choice !=7)
    {
        cout << "Please enter the first number: " <<endl;
        cin  >> x;
        cout << "Pease enter the second number: " <<endl;
        cin  >> y;

        op = opTypes[choice-1];
        result = 9.9;
        switch (choice)
	    {
            case 1:
                 result = Addition(x,y);
                 break;
            case 2:
                 result = Subtraction(x,y);
                 break;
            case 3:
		  result = Multiplication(x,y);
		  break;
	    case 4:
		  result = Divide(x,y);
		  break;
	    case 5:
		  result = Modulus(x,y);
		  break;
	    case 6:
		   result = Exponents(x,y);
		   break;
	    }
	    cout << "The answer is : " << x << op << y << " = " <<  result << endl;
        choice = getChoice();
    }

	//system ("pause");
    return 0;
}


double Addition(double p, double q)
{
	return p+q;
}

double Subtraction(double x, double y)
{
	return x-y;
}

double Multiplication(double x, double y)
{
	return x*y;
}

double Divide(double x, double y)
{
	return x/y;
}

double Modulus(double x, double y)
{
	return static_cast<int>(x) % static_cast<int>(y);
}

double Exponents(double x, double y)
{
	return pow(x, y);
}
Last edited on
Building on what @Codewriter has done, it would be much cleaner to condense your program into one function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
int getChoice()
{
    int selection;
    cout<< "1. Addition" << endl
        << "2. Subtraction" << endl
        << "3. Multiplication" << endl
        << "4. Division" << endl
        << "5. Modulus" << endl
        << "6. Exponents" << endl
        << "7. Exit" << endl;
    cin >> selection;
    if((selection>7)||(selection<1)) selection=0;
    return selection;
}
void manipulator(double x,double y, int choice)
{
    char op, opTypes[6] = {'+','-','*','/','%','^'};
    double result;
    cout<<"\n Enter the numbers x & y";
    cin>>x>>y;
    op=opTypes[choice-1];
    switch(choice)
    {
        case 1: result=x+y;         break;
        case 2: result=x-y;         break;
        case 3: result=x*y;         break;
        case 4: result=x/y;         break; //auto-error if division by 0
        case 5: result=x%y;         break;
        case 6: result=pow(x,y);    break;
        case 7: exit(0);
    }
    cout<<endl<<x<<" "<<op<<" "<<y<<" = "<<result;    
}
main()
{
    int choice;
    do
    {
        double x,y;
        choice=getChoice();
        if(!choice) cout<<"\n Invalid Choice"<<endl;
        else manipulator(x,y,choice);
    } while(choice!=7);
}


Ps- I haven't run this, so if there are any syntax errors, please spare me..
Last edited on
UPDATE - I'm sorry I didn't see the instructions, but could you ask your teacher if this is okay? I mean it seems vain to make separate functions when you can incorporate it into one itself, right?
Also, writing to the file? Do you want it to be a binary file (unreadable to us) or a text (*.txt) file? Have you learnt streams?
this worked. I saw where I was off in thought and got it. Thanks guys.
Topic archived. No new replies allowed.