Simple Calculator Program

Here is my code for a calculator program. I'm very much unsure what is wrong with the code, it isn't functioning properly. I have an infile to read from. Also, the infile has fractional parts, so I would need to use float, but I have modulus in the code, how would I turn just the modulus into integer?
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
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;

int main()
{
    ofstream out("Calculator.out");
    ifstream inf;
             inf.open("simplecal3.in");
    if(!inf)
    {
        cout << "ERROR";
        return 0;
    }
    //Variables
     int n1, n2, output;
    char operation;

    //Output
     out << "Calculator Program:\n";

    while(inf >> n1)
     {
         inf >> operation >> n2;

    //Switch Operation
    switch(operation)
    {
    case '+': output = n1 + n2;
        break;
    case '-': output = n1 - n2;
        break;
    case '*': output = n1 * n2;
        break;
    case '/': output = n1 / n2;
        break;
    case '<': output = n1 < n2;
        break;
    case '>': output = n1 > n2;
        break;
    case '%': output = n1 % n2;
        break;
    case '=': output = n1 == n2;
        break;
    case '^': output = pow(n1,n2);
        break;
    }
     }
    out << n1 << operation << n2 <<" = " << output;
    out.close();
    inf.close();
    return 0;
}


This is the infile data:
78.2 + 99.0
84.3 – 5.2
605.24 * 34.72
112.45 < 601.00
999.3 / 333.9
1010.10 = 1010.10
-5.5 * -3.8
79.84 % 22.7
5.89 * .00
0.04 - 0.09
-32.6 ^ 8.0
712.45 < 601.00
712.45 > 601.00
5.0 ^ 6.0
7.8234 - 7.8234
7.8234 % 7.8234
0.12 ^ 0.045
567.89 = 345.23
Last edited on
while floating mod is a little odd, there is a function for it:
fmod() look up and try it.

if that is not sufficient, explain *exactly* what you want to do with your mod?
Your output (line 51) is the wrong side of the end of the while loop.

Your n1 and n2 need to be doubles. What you expect to output from a boolean operation is unclear.
Last edited on
@lastchance, if you're speaking about the while(inf>>n1); the professor told us that if write it the way I have it, that it'll read the infile entirely.


@jonin, I assumed you could only have int mod not anything else. If I declare my variables as float/double then the mod portion will not run.
So did changing to double and using fmod solve your issue?
Repeated for emphasis :
explain *exactly* what you want to do with your mod
Last edited on
No @CodeNovice, I'm not speaking about your while() statement, I'm speaking about where you put your output line. Your code may or may not read the whole file, but it is only ever going to output the last result alone.
you can have floating point mod. Again, its the fmod function in <cmath>. It will run fine if you do that. % operator does NOT work: that is for integers. I find the concept odd and have only used it a time or two, but its there if you want it.
Last edited on
I placed the fmod into the code and it runs without error. But my output is still wonky. my outfile has only this as it's output:

Calculator Program:
78.2+99 = 177.2
84.3â0 = 177.2


I was thinking I need to put output = 0 on line 27, but that doesn't solve anything either. besides making the output look like this:

Calculator Program:
78.2+99 = 177.2
84.3â0 = 0


What needs to take place for the outfile to continue taking the infile data correctly?

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
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;

int main()
{
    ofstream out("Calculator.out");
    ifstream inf;
             inf.open("simplecal3.in");
    if(!inf)
    {
        cout << "ERROR";
        return 0;
    }
    //Variables
    double n1, n2, output=0;
    char operation;

    //Output
     out << "Calculator Program:\n";

    while(inf >> n1)
     {
          inf >> operation >> n2;

    //Switch Operation
    switch(operation)
    {
    case '+': output = n1 + n2;
        break;
    case '-': output = n1 - n2;
        break;
    case '*': output = n1 * n2;
        break;
    case '/': output = n1 / n2;
        break;
    case '<': output = n1 < n2;
        break;
    case '>': output = n1 > n2;
        break;
    case '%': output = fmod(n1,n2);
        break;
    case '=': output = n1 == n2;
        break;
    case '^': output = pow(n1,n2);
        break;
    }
        out << n1 << operation << n2 <<" = " << output << endl;
     }
out.close();
inf.close();
return 0;
}

Are you sure that your input file has a genuine "minus" sign in and not one of the alternate bars that wordprocessors like to create? It is failing to read that.
The input file is exactly as what I posted. I assume it is a genuine minus sign.
Try changing it to a + and see what happens.

It looks longer than the minus sign further down (at least on my ipad).
Last edited on
I placed // on line 33 and 34, and the output is still the same. Something has to be wrong with the infile declaration? It isn't reading the data from it.
Please do what I asked, not something completely different. Once it fails to read a character then your program will end, irrespective of what you have commented out. If changing it to a + works then I should go back to your input file and manually edit it for a minus sign.
Last edited on
If I change the '-' to '+' it gives an error of duplicate, this is why I commented it out.
OKKK I have no idea what just happened. But for shits and giggles I went into the input data and changed line 2 from the '-' that was there to the '-' on my keyboard. Saved the alteration and re-ran the code. Once I opened my output file this appeared.


Calculator Program:
78.2+99 = 177.2
84.3-5.2 = 79.1
605.24*34.72 = 21013.9
112.45<601 = 1
999.3/333.9 = 2.99281
1010.1=1010.1 = 1
-5.5*-3.8 = 20.9
79.84%22.7 = 11.74
5.89*0 = 0
0.04-0.09 = -0.05
-32.6^8 = 1.27568e+012
712.45<601 = 0
712.45>601 = 1
5^6 = 15625
7.8234-7.8234 = 0
7.8234%7.8234 = 0
0.12^0.045 = 0.908998
567.89=345.23 = 0



You were absolutlely correct, apologies for doing something entirely different.
Last edited on
What needs to be done to change the 1 and 0 value to print out the value of which is the correct answer for the specified operation?
What do your requirements say to print out? Show us what you want 712.45<601 = to print out.
Last edited on
You know now that I think about it, I'm unsure what one would put, I'd normally say the value of which makes the inequality t/f. But for this it makes sense to have 1 or 0. What value do you think would make it easier to read?
Perhaps having true/false is slightly more readable than 1/0, but I guess it's a matter of opinion.

If you want it to display true/false instead of 1/0, you can use the std::boolalpha flag.
See: https://stackoverflow.com/a/15960054/8690169 for example
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <iomanip>

int main() {
    std::cout<<false<<"\n";
    std::cout << std::boolalpha;   
    std::cout<<false<<"\n";
    return 0;
}
Topic archived. No new replies allowed.