Programming output incorrect

Hi, so I'm writing this code for my c++ course that acts as a mini calculator by using <fstream> to refer to a txt file given to us with seven rows of two integers and a mathematical operator. We're told what to output for each operator. This is my code:

#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, const char * argv[]) {

int first, second, i;
char mathOper;
ifstream calc;


calc.open ("calc.txt");



for(i=0;i<7;i++){

calc>>first>>second>>mathOper;

if(mathOper=='*')
cout<<first<<" * "<<second<<" = "<<first*second<<endl;
else if(mathOper=='/' && second=='0')
cout<<first<<" / "<<second<<" = infinity as denominator is zero <<endl;
else if(mathOper=='/')
cout<<first<<" / "<<second<<" = "<<first<<" % "<<second<<" = "<<first%second<<endl;
else if(mathOper=='+')
cout<<first<<" + "<<second<<" = "<<first+second<<endl;
else if(mathOper=='-')
cout<<first<<" - "<<second<<" = "<<first-second<<endl;
else
cout<<first<<" "<<mathOper<<" "<<second<<" = .....Invalid mathematical operator."<<endl;

}

calc.close();


return 0;
}

For some reason the output is always "0 0 = ...Invalid mathematical operator." even though the numbers in the calc.txt are not zero's, and its not showing the mathematical operator. I have the calc.txt in the same folder as the xcode file. Help please?
Your program seems to work. Only problem is that it performs a modulo op when it should perform a division on lines
1
2
else if(mathOper=='/')
cout<<first<<" / "<<second<<" = "<<first<<" % "<<second<<" = "<<first%second<<endl;

Maybe you have a problem with your input file. The one I used was
1 2 +
2 3 -
3 4 /
5 5 *
1 1 +
2 2 +
1 3 /

OUTPUT

1 + 2 = 3
2 - 3 = -1
3 / 4 = 3 % 4 = 3
5 * 5 = 25
1 + 1 = 2
2 + 2 = 4
1 / 3 = 1 % 3 = 1
Turns out it was a mac problem and the input file was not being read. I've fixed it and it runs but I've encountered another problem. This is the file that is being read:

10 3 *
10 3 /
10 3 %
10 3 +
10 0 /
10 3 -
10 5 !
10 10 *

The program runs fine until it reaches the 5th line with zero as the denominator and I get the error message "Thread 1: EXC_ARITHMETIC(code = EXC_1386_DIV, subcode = (0x0)", and the code stops working. I think it's having a problem with the math because its not going to my
1
2
else if(mathOper=='/' && second=='0')
cout<<first<<" / "<<second<<" = infinity as denominator is zero <<endl; 


but rather is going to the line

1
2
else if(mathOper=='/')
cout<<first<<" / "<<second<<" = "<<first<<" % "<<second<<" = "<<first%second<<endl;


OUTPUT
10 * 3 = 30
10 / 3 = 10 % 3 = 1
10 % 3 = .....Invalid mathematical operator.
10 + 3 = 13
(lldb) 


Also to answer your question about the modulus operator being used instead of division, thats actually just what we were instructed to do so thats fine x)

Can anyone help?
else if(mathOper=='/' && second=='0') should be else if(mathOper=='/' && second== 0)
closed account (48T7M4Gy)
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
#include <iostream>
#include <fstream>


int main() {
    
    int first, second;
    char mathOper;
    
    std::ifstream calc;
    calc.open ("calc.txt");
    while( calc >> first >> second >> mathOper)
    {
        if(mathOper=='*')
            std::cout << first << " * " << second << " = " << first*second << std::endl;
        
        else if(mathOper=='/' && second == 0) // <-- ***
            std::cout << first << " / " <<second<< " = infinity as denominator is zero" << std::endl;
        
        else if(mathOper=='/' && second != 0) // <-- ***
            std::cout<<first<<" / "<<second<<" = "<<first<<" % "<<second<<" = "<<first%second<<std::endl;
        
        else if(mathOper=='+')
            std::cout<<first<<" + "<<second<<" = "<<first+second<<std::endl;
        
        else if(mathOper=='-')
            std::cout<<first<<" - "<<second<<" = "<<first-second<<std::endl;
        
        else
            std::cout << first << " " << mathOper << " " << second << " = .....Invalid mathematical operator." <<std::endl;
        
    }
    
    calc.close();
    
    
    return 0;
}
10 * 3 = 30
10 / 3 = 10 % 3 = 1
10 % 3 = .....Invalid mathematical operator.
10 + 3 = 13
10 / 0 = infinity as denominator is zero
10 - 3 = 7
10 ! 5 = .....Invalid mathematical operator.
10 * 10 = 100
Program ended with exit code: 0


Hint:

1. use more whitespace in writing your code. Clearly laying out the code makes debugging a lot quicker and easier.

2. Avoid duplicating all the messaging.

3. A switch control is ideal for this job.

4. Note the use of while loop.
Last edited on
closed account (48T7M4Gy)
Just for interest to demonstrate:
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
#include <iostream>
#include <fstream>

void print_result(int, int, char, int);

int main() {
    
    int first = 0, second = 0;
    char mathOperation;
    
    std::ifstream calc;
    calc.open ("calc.txt");
    while( calc >> first >> second >> mathOperation)
    {
        switch (mathOperation){
            case '*':
                print_result(first, second, mathOperation, first * second);
                break;
                
            case '/':
                if( second == 0 ){
                    std::cout
                    << first << ' ' << mathOperation << ' ' << second
                    << "  *** Division by zero\n";
                }
                else
                    print_result(first, second, mathOperation, first / second);
                break;
                
            case '+':
                print_result(first, second, mathOperation, first + second);
                break;
                
            case '-':
                print_result(first, second, mathOperation, first - second);
                break;
                
            default:
                std::cout
                << first << ' ' << mathOperation << ' ' << second
                << " *** Operator unknown\n";
        }
    }
    
    calc.close();
    return 0;
}

void print_result(int aFirst, int aSecond, char aOperation, int aAnswer)
{
    std::cout
    << aFirst << ' ' << aOperation << ' ' << aSecond << " = " << aAnswer << '\n';
}

10 * 3 = 30
10 / 3 = 3
10 % 3 *** Operator unknown
10 + 3 = 13
10 / 0  *** Division by zero
10 - 3 = 7
10 ! 5 *** Operator unknown
10 * 10 = 100
Program ended with exit code: 0
Okay I understand what I was doing wrong now, thanks guys!!
Topic archived. No new replies allowed.