1..
1..

Reputation: 1

Simple Calculator C++

I wrote this program for a homework assignment. I am not getting the correct output. I was told it is because I am not resetting PreviousResult between sets of calculations, yet I have no idea how to do that. The output should be:

Please enter a filename: calc.txt

The result of calculation 1 is: 26

The result of calculation 2 is: 2

The result of calculation 3 is: 0

Instead I am getting 4 calculations, with calculation 3 = 1, and 4 = 0

The calc.txt file is:

3 add 5 4 add_prev 4 mul_prev 2

1 sub 3 1

1 div_prev 2

My code:

// Header Files
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
// Declaring variables
int PreviousResult = 0;
string command;
string filename = "calc.txt";
int x,y,z;
int result = 0;
int numCommands = 0;
int operation = 1;

//Prompts the user to enter the text file name.
cout << "Please enter a filename: ";

cin >> filename;

//Declaring an ifstream object and opening the file
ifstream myfile(filename.c_str());
myfile.open("calc.txt");

//Will check to see if file exists. If not, it will output the following.
if(!myfile)
{
    cout << "The file doesn't exist." <<endl;

    return 0;
}

//While loop- read the file until the end is reached.
while(!myfile.eof())
{
    myfile >> numCommands;
    for(int i = 0; i < numCommands; i++)
    {
        myfile >> command;
        //Addition
        if (command=="add")
        {
            myfile >> x >> y;
            PreviousResult = x + y;
        }
        //Subtraction
        else if (command == "sub")
        {
            myfile >> x >> y;
            PreviousResult = x - y;
        }
        //Multiplication
        else if (command == "mul")
        {
            myfile >> x >> y;
            PreviousResult=x*y;
        } 
        //Division
        else if(command=="div")
        {
            myfile >> x >> y;
            PreviousResult = x / y;
        }
        else if (command == "add_prev")
        {
            myfile >> z;
            PreviousResult += z;
        }
        else if (command == "sub_prev")
        {
            myfile >> z;
            PreviousResult -= z;
        }
        else if (command == "mul_prev")
        {
            myfile >> z;
            PreviousResult *= z;
        }
        else if (command == "div_prev")
        {
            myfile >> z;
            PreviousResult /= z;
        }
    }

    result = PreviousResult;

    //Print the results.
    cout << "The result of calculation " << operation <<" is: " << result <<  endl;
    //The count is incremented by 1
    operation ++;
}
  return 0;
  }

Upvotes: 0

Views: 348

Answers (2)

Jacob Malachowski
Jacob Malachowski

Reputation: 981

The problem is that you aren't resetting PreviousResult after you perform the calculations. So in calculation 2, you are setting PreviousResult to 2, setting result = PreviousResult, and then displaying the result. The problem is that once you go into calculation 3, PreviousResult still equals 2. That means calculation 3 is computing 2 divided by 2 instead of 0 divided by 2, giving you the incorrect answer.

So after you set Result = PreviousResult, you need the line

PreviousResult = 0;

As for the 4th calculation, while(!myfile.eof()) can cause inconsistent results. There may be a newline character causing it to try and read the next empty line. Add myfile.ignore() right after the opening brace of the while loop and that should fix the issue.

Upvotes: 0

Huynh
Huynh

Reputation: 176

If I am interpreting your program correctly, you are right that one of your problems is from the PreviousResult not being reset. You declared and initialized PreviousResult at the beginning with this line

int PreviousResult = 0;

Resetting it is as simple as resassigning its value such as

PreviousResult = 0;

As for why you are getting 4 calculations instead of 3,

while(!myfile.eof())

will loop 1 more time than you intend it to because eof() only returns false after the input stream has read the end of file. That does not occur until myfile >> numCommands;. An Alternative is to do

while(myfile >> numCommands)

and the while loop will terminate when there are no more things to be read.

Upvotes: 1

Related Questions