Program to Evaluate Expression

I have an assignment due this Friday that I've committed to completing tonight. I've been working at it for about two hours now and I seem to be stumped. I'm meant to write a program which prompts the user to enter an expression in the form (value operator value) e.g (1 + 3)(1 * 6) and then evaluate that expression and push it into an output file.

I'm stumped because in my code I can't seem to figure out why the echo of the user input isn't outputted to the file "prog3_002out.txt" within the switch statements. That's the last part I need to figure out.

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
#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
    double expression,
           operand1,
           operand2;

    char operatr;

    ofstream fout;
    fout.open("prog3_002out.txt");

    cout << "Enter a binary expression of the form: "
         << "(operand operator operand) ";

    cin >> operand1 >> operatr >> operand2;

    cout << endl << endl << "Tommy Clark " << endl
         << "C.S.1428.002" << endl
         << "Lab Section: L11" << endl
         << "10/18/17" << endl << endl;

    cout << "Output can be found in the prog3_002out.txt file" << endl;

    if( !fout )
    {
        cout << "Opening file failed" << endl;
        exit(1);
    }

    fout << "Tommy Clark " << endl
         << "C.S.1428.002" << endl
         << "Lab Section: L11" << endl
         << "10/04/17" << endl << endl;

    switch (operatr)
    {
        case '+' :
            fout << operand1 << " " << operatr << " " << operand2;
            expression = (operand1 + operand2);
            fout << expression;
            break;

        case '-' :
            fout << operand1 << " " << operatr << " " << operand2;
            expression = (operand1 - operand2);
            fout << expression;
            break;

        case '*' :
            fout << operand1 << " " << operatr << " " << operand2;
            expression = (operand1 * operand2);
            fout << expression;
            break;

        case '/' :
            fout << operand1 << " " << operatr << " " << operand2;
            if (operand2 == 0)
            {
                fout << endl << "Division by zero produces an "
                     << "undefined result." << endl;
            }
            else
                expression = (operand1 / operand2);
                fout << expression;
            break;

        default :
            fout << operand1 << " " << operatr << " " << operand2;
            fout << "Encountered unknown operator.";
    }

    system("PAUSE>NULL");

    fout.close();

    return 0;
}
Last edited on
I can't seem to figure out why the echo of the user input isn't outputted to the file "prog3_002out.txt" within the switch statements.

I tested it with the input "2 + 3" and it seemed to work, though the output was "2 + 35" which is incorrectly formatted.

What output do you want to appear in the file?

That's so weird, "2 + 35" was printed in the output file? Everytime I build and run the program through and then open the text document/output file it only has the identifcation block that I put before the switch statements. I don't get any of the actual expression

"Tommy Clark "
"C.S.1428.002"
"Lab Section: L11"
"10/04/17"
I think I know what is happening. You need to check what is in the file after it has been closed. The file is closed either when explicitly closed such as fout.close(); or when the fout stream goes out of scope at the end of main(). But ... you have this system("PAUSE>NULL"); which holds the file open, because it has not yet been closed in either of the ways mentioned. Meanwhile, the contents of the fout buffer may not yet have been written to the file.

The simple solution. Move that pause statement so it is after the close() statement.


Ironically I was noticing the over-generous use of endl in the earlier parts of the program. endl has the effect of first outputting a newline and then flushing the buffer. That would cause its contents to be written to the file immediately.

See http://www.cplusplus.com/reference/ostream/endl/
Last edited on
Funny enough I had the thought that it would be exactly that, immediately fixed the problem I've been trying to solve for a while also the endl explanation was helpful to see why I could see the first line of output but not the last.

Thank you so much for the help, also the formatting this professor wants precisely is still strange to me and I've found the easiest way to match it is to just endl pretty much everything. What suggested alternatives would you have though?
Well, it was more a matter of reducing the calls to flush the buffer - flushing the buffer no less than seven times in a single statement just strikes me as unnecessary. At this point in time, with the type of program you are writing, it really doesn't matter, but if you were to later write a program which generated many thousands of lines, the use of a plain '\n' would be quicker than endl. It depends of course on the requirements.

But I was going to suggest replacing this:
1
2
3
4
    cout << endl << endl << "Tommy Clark " << endl
         << "C.S.1428.002" << endl
         << "Lab Section: L11" << endl
         << "10/18/17" << endl << endl;
and this
1
2
3
4
    fout << "Tommy Clark " << endl
         << "C.S.1428.002" << endl
         << "Lab Section: L11" << endl
         << "10/04/17" << endl << endl;


with something along these lines:
1
2
3
4
5
6
7
8
9
    const char * headings = 
        "Tommy Clark\n"
        "C.S.1428.002\n"
        "Lab Section: L11\n"
        "10/18/17\n";

    cout << "\n\n" << headings << endl;

    fout << headings << endl;


... but I noticed the date was different in the original - lines 26 and 39 have two different dates ... so my suggestion is either an improvement as it keeps them the same, or a disadvantage as you want them to be different.

Note I left a single endl right at the end of each output.
Last edited on
Topic archived. No new replies allowed.