Simple Calculator using pointers help!

Hello! Would anyone happen to know why my program is not running correctly. In main I have a switch function, but it doesn't seem as I implemented it correctly. Every time I input my operator and two values the program just loops to the begging. It seems as though the functions I use to calculate aren't being called at all. Also, i am getting an error because I can't do % with floats. Should I temporarily set my inputs to ints??

My assignment is to : Write a simple calculator program that repeatedly allows the user to select from a menu of operations (comprising of the following operations: +,-,*,/ , % ) on real values (that the user enters) and then computes the value that results in applying the selected operation to those operands. However, the only named variables you may use are pointer variables; all others must be anonymous variables.

Thanks!!

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>

using namespace std;

void SumNum (float &a, float &b);
void SubNum (float &a, float &b);
void MulNum (float &a, float &b);
void DivNum (float &a, float &b);
void ModNum (float &a, float &b);

int main()
{
    int choice;
    int agian = 1;
    float *num1 = new float;
    float *num2 = new float;

    when( agian == 1){
        cout << "Welcome to my simple calculator!" << endl;
        cout << "********************************" << endl;
        cout << "1) Addition (x+y)" << endl;
        cout << "2) Subtraction (x-y)" << endl;
        cout << "3) Multiplication (x*y)" << endl;
        cout << "4) Division (x/y)" << endl;
        cout << "5) Modulation (x%y)" << endl;
        cout << "6) Exit the Calculator" << endl;
        cout << "*********************************" << endl;
        cout << "Please Enter the number of the operator you would like to compute:";
        cin >> choice;
        cout << "Enter the first number of the equation(x) :";
        cin >> *num1;
        cout << "Enter the second number of the equation(y) :";
        cin >> *num2;

        switch (choice){
            case'1':
                SumNum(*num1,*num2);
                break;
            case'2':
                SubNum(*num1,*num2);
                break;
            case'3':
                MulNum(*num1,*num2);
                break;
            case'4':
                DivNum(*num1,*num2);
                break;
            case'5':
                ModNum(*num1,*num2);
                break;
            case'6':
                    agian = 0;
                break;
        }

    }

    if ( agian == 0){
        cout << "Bye!" << endl;
    }


    return 0;
}

void SumNum(float &a, float &b){
    cout << "your equation is:" << a << " + " << b << " = ??"<<endl;
    cout << "your answer is:"<< a+b << endl;
}
void SubNum(float &a, float &b){
    cout << "your equation is:" << a << " - " << b << " = ??"<<endl;
    cout << "your answer is:"<< a-b << endl;
}
void MulNum(float &a, float &b){
    cout << "your equation is:" << a << " * " << b << " = ??"<<endl;
    cout << "your answer is:"<< a*b << endl;
}
void DivNum(float &a, float &b){
    if(b==0){
    cout << "invalid. cannont divide by 0";
    }
    else cout << "your equation is:" << a << " / " << b << " = ??"<<endl;
    cout << "your answer is:"<< a/b << endl;
}
void ModNum(float &a, float &b){
    cout << "your equation is:" << a << " % " << b << " = ??"<<endl;
    cout << "your answer is:"<< a/b << endl;
}
Last edited on
Make choice a char or change '1' to 1, '2' to 2, etc.

For floating point numbers you can use std::fmod instead of %.
Thank you Peter!! I knew the switch was something small and frustrating! however, I can't use the math library for this assignment. I was thinking about casting them to int? smart idea?
Topic archived. No new replies allowed.