So for class project I need to create a calculator program that takes input from a text file formula.txt
which contains the following formulas:
'15 ;
10 + 3 + 0 + 25 ;'
When the program runs it should calculate and print the results of the formulas, with line break as such:
15
38
However, every time I run the program, it gives me the following result:
15
38
25
I've worked through my code and haven't been able to find a problem. Any help would be appreciated. By code is found below.
#include <iostream>
using namespace std;
int main ()
{
double input; //initialize input variable
char sign; //intialize sign character
double calc = 0; //initial calculation value set to 0
bool add= true; // add to use whether to add or not
bool cont = true; // boolean for continuing loop
while (cont) //loop only continues while cont is true
{
cin >> input; //take in input
if (add) //if add is true
{
calc = calc + input; // adds the input to calc
}
else //if add is false
{
calc = calc - input; //subtracts input from calc
}
cin >> sign; //take in sign
if (sign == '+') //if sign is '+'
{
add = true; //add is true
}
else if (sign == '-') //if sign is '-'
{
add = false; //add is false
}
else if (sign == ';') //if sign is ';'
{
cout << calc << endl; //outputs calc to console
calc = 0;
}
if (cin.fail()) // if cin fails
{
cont = false; //continue is set to false
}
}
return 0;
}