Calculator(Read From File)

.
Last edited on
Lots of syntax errors.
On lines 43 and 78, there is for instead of if and while respectively. Also, using the = assignment operator instead of the == comparison operator.

Later, you have lots of if-else statements with missing braces.
should look like this:

1
2
3
4
5
6
7
8
if (condition) 
{
   // statements
}
else if
{
  // statements
}
Last edited on
.
Last edited on
,
Last edited on
The = instead of == problem hasn't been fixed.
The missing braces problems still haven't been fixed. I'll be more specific - but this is just an example, you need to go through the entire program.
Current version (notice the first line uses '=' instead of '=='):
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
while (type = "Math")
{
inFile >> type >> Symbol >> Value1 >> Value2;

if (Symbol == '+')
    ans = Value1 + Value2;
    Operation="Add";
    cout<<Operation<<": "<<Value1<<Symbol<<Value2<<" = "<<ans<<endl<<endl;
else if (Symbol =='-')
    ans = Value1 - Value2;
    Operation="Sub";
    cout<<Operation<<": "<<Value1<<Symbol<<Value2<<" = "<<ans<<endl<<endl;
else if (Symbol == '*')
    ans = Value1*Value2;
    Operation="Mul";
    cout<<Operation<<": "<<Value1<<Symbol<<Value2<<" = "<<ans<<endl<<endl;
else if (Symbol == '/')
    ans = Value1/Value2;
    Operation="Div";
    cout<<Operation<<": "<<Value1<<Symbol<<Value2<<" = "<<ans<<endl<<endl;
else if (Symbol == '%')
    ans = Value1%Value2;
    Operation="Mod";
    cout<<Operation<<": "<<Value1<<Symbol<<Value2<<" = "<<ans<<endl<<endl;
else
    cout<<"Invalid Symbol"<<endl;
}


Corrected version:
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
while (type == "Math")
{
    inFile >> type >> Symbol >> Value1 >> Value2;

    if (Symbol == '+')
    {
        ans = Value1 + Value2;
        Operation="Add";
        cout<<Operation<<": "<<Value1<<Symbol<<Value2<<" = "<<ans<<endl<<endl;
    }
    else if (Symbol =='-')
    {
        ans = Value1 - Value2;
        Operation="Sub";
        cout<<Operation<<": "<<Value1<<Symbol<<Value2<<" = "<<ans<<endl<<endl;
    }
    else if (Symbol == '*')
    {
        ans = Value1*Value2;
        Operation="Mul";
        cout<<Operation<<": "<<Value1<<Symbol<<Value2<<" = "<<ans<<endl<<endl;
    }
    else if (Symbol == '/')
    {
        ans = Value1/Value2;
        Operation="Div";
        cout<<Operation<<": "<<Value1<<Symbol<<Value2<<" = "<<ans<<endl<<endl;
    }
    else if (Symbol == '%')
    {
        ans = Value1%Value2;
        Operation="Mod";
        cout<<Operation<<": "<<Value1<<Symbol<<Value2<<" = "<<ans<<endl<<endl;
    }
    else
        cout<<"Invalid Symbol"<<endl;
}


Though the above might also be written like this:
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
while (type == "Math")
{
    inFile >> type >> Symbol >> Value1 >> Value2;

    if (Symbol == '+')
    {
        ans = Value1 + Value2;
        Operation="Add";
    }
    else if (Symbol =='-')
    {
        ans = Value1 - Value2;
        Operation="Sub";
    }
    else if (Symbol == '*')
    {
        ans = Value1*Value2;
        Operation="Mul";
    }
    else if (Symbol == '/')
    {
        ans = Value1/Value2;
        Operation="Div";
    }
    else if (Symbol == '%')
    {
        ans = Value1%Value2;
        Operation="Mod";
    }
    else
    {
        cout<<"Invalid Symbol"<<endl;
        continue;    // go back to start of this while loop
    }
    
    cout<<Operation<<": "<<Value1<<Symbol<<Value2<<" = "<<ans<<endl<<endl;    
}


At any rate, try to get the basic syntax sorted so the code will at least compile, even if it doesn't necessarily give the correct results.

Then take it from there.
.
Last edited on
Topic archived. No new replies allowed.