Calculator Problem

Ok, I realize I have ALL sorts of issues with this program. I'm trying to write a program that prompts the user to put in an expression with 3 numbers and 2 operators (i.e. 4+6/2). Once it's finished, it asks the user if it would like to enter another expression. Y should start the program again and N should end it. This is currently the result I'm getting:

Enter a mathematical expression using three numbers separated by two operators. Ex: 2+3*5: 2+2+2
The answer is: 0
Do you want to enter another expression? Y or N:
n
Program ended with exit code: 0

So, two questions: How do I get it to restart the program if the user says yes?
Also, why am I getting an answer of 0 instead of the correct mathematical result?
Please be gentle with me. I've only been doing this a couple weeks :-)

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  #include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;


int main()
{
    double num1, num2, num3;
    double solution = 0;
    char op1, op2;
    char repeat;
    
    cout << "Enter a mathematical expression using three numbers separated by two operators. Ex: 2+3*5: ";
    cin >> num1>> op1>> num2>> op2>> num3;
    cout << "The answer is: " << solution <<endl;
    cout << "Do you want to enter another expression? Y or N: " <<endl;
    cin >> repeat;
    
    {
        do  (repeat == 'Y' || 'y')
        {
            
        }
        if (repeat == 'N' || 'n')
        {
            return 0;
        }
        if (repeat != 'Y' || 'y' || 'N' || 'n')
        {
            cout << "Please enter Y or N: " <<endl;
        }
    {
        if ((op1 == '+') && (op1 == '+'))
        {
            solution = (num1+num2+num3);
        }
        else if ((op1 == '+') && (op2 == '-'))
        {
            solution = ((num1+num2)-num3);
        }

        else if ((op1 == '+') && (op2 == '*'))
        {
            solution = (num1+(num2*num3));
        }
        else if ((op1 == '+') && (op2 == '/'))
        {
            solution = (num1+(num2/num3));
        }
        else if ((op1 == '-') && (op2 == '+'))
        {
            solution = (num1-(num2+num3));
        }
        else if ((op1 == '-') && (op2 == '-'))
        {
            solution = (num1-num2-num3);
        }
        else if ((op1 == '-') && (op2 == '*'))
        {
            solution = (num1-(num2*num3));
        }
        else if ((op1 == '-') && (op2 == '/'))
        {
            solution = (num1-(num2/num3));
        }
        else if ((op1 == '*') && (op2 == '+'))
        {
            solution = ((num1*num2)+num3);
        }
        else if ((op1 == '*') && (op2 == '-'))
        {
            solution = ((num1*num2)-num3);
        }
        else if ((op1 == '*') && (op2 == '*'))
        {
            solution = (num1*num2*num3);
        }
        else if ((op1 == '*') && (op2 == '/'))
        {
            solution = ((num1*num2)/num3);
        }
        else if ((op1 == '/') && (op2 == '+'))
        {
            solution = ((num1/num2)+num3);
        }
        else if ((op1 == '/') && (op2 == '-'))
        {
            solution = ((num1/num2)-num3);
        }
        else if ((op1 == '/') && (op2 == '*'))
        {
            solution = (num1/(num2*num3));
        }
        else if ((op1 == '/') && (op2 == '/'))
        {
            solution = (num1/num2/num3);
        }
       
    }
    return 0;

            }
}
Some of the code is out of order. Here is what it should look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
   double num1, num2, num3, solution = 0;
   char op1, op2,  repeat;
   do
   {
      cout << "Enter a mathematical expression...: ";
      cin >> num1 >> op1 >> num2 >> op2 >> num3;
         if ((op1 == '+') && (op1 == '+'))
            solution = (num1+num2+num3);
         ...
         cout << "The answer is: " << solution << endl;
         cout << "Do you want to enter... " << endl;
         cin >> repeat;
    } while (repeat == 'Y' || repeat == 'y');
    return 0;
}

Okay, the code runs line by line, so you have to set the value of solution before displaying it to the screen. That's why you're getting an answer of 0. For the repeat option, everything between do { and } while is going to be repeated as long as repeat is Y or y.
That did it! I can't believe it was something as obvious as the order I wrote the code in. I haven't done anything with this many lines of code yet. Thank you!!
Topic archived. No new replies allowed.