-3

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;
}
  • Hint: when do you check whether `cin >> input;` succeeded? – molbdnilo Sep 17 '18 at 13:05
  • At the bottom with 'if (cin.fail())' – Hannan Abid Sep 17 '18 at 13:07
  • not the current problem, but it will be one of the next you run into: if the last operation of an expression was `-`, the first value of the next expression will not be added to `calc` – 463035818_is_not_a_number Sep 17 '18 at 13:07
  • 1
    Basically a dupe of [this](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). TL;DR: always use a read operation as the condition of the loop. – NathanOliver Sep 17 '18 at 13:08
  • spoiling molbdnilos comment: you first get the input, then you use the value, then you check if the input succeeded. Now try to find out what is wrong about this order ;) – 463035818_is_not_a_number Sep 17 '18 at 13:10
  • Got it to work it was a simple problem with not checking for input before using it and then using `break;` if input failed. Changed it to this `while (cont) //loop only continues while cont is true { cin >> input; //take in input if (cin.fail()) // if cin fails { cont = false; //continue is set to false break; }... ` – Hannan Abid Sep 17 '18 at 13:23
  • @user463035818 Thanks for pointing out the next problem. Fixed it – Hannan Abid Sep 17 '18 at 13:35
  • actually I find the structure of your code a little bit too complicated. currently you just keep reading input and process it. Imho it would be much clearer if one "formula" would have a representation in your code (eg another nested loop that breaks once it finds a `;`) – 463035818_is_not_a_number Sep 17 '18 at 13:49

1 Answers1

0

The problem came from your loop. You print 38. if(cin.fail()) is false, your previous cin insert ';' in sign so it's ok.

So one turn more. add is true so calc is calc + input. your last input is 25, so calc = 25.

cin >> sign fail. Your last sign was ';' so you print calc in cout -> 25.

toto
  • 1