Hi I know this might sound like a repeat question but I have spent many days on this problem and cannot find out a solution. In my code I have a while(true) loop and at the end of the file I have an if statement with the end of file function and break but in the output it repeats the last number twice.
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int main ()
{
int line=0;
ifstream infile; //INPUT input file stream
ofstream outfile; //OUTPUT output file stream
infile.open("inputPartb.txt");
if (! infile)
{
cout <<"Problem opening input file inputPartb.txt"<<endl;
return 1; //not successful
}
outfile.open("resultsPartb.txt");
if (! outfile)
{
cout <<"Problem opening output file resultsPartb.txt"<<endl;
return 1; //not successful
}
while (true)
{
double num1;
double num2;
int intnum1;
int intnum2;
char mathOperator;
double addition;
double subtraction;
double multiplication;
int division; //division is using remainders so float isnt needed
double power;
double remainder;
line = line +1;
//reading numbers from the file
infile >> num1 >> num2 >> mathOperator;
intnum1 = num1;
intnum2 = num2;
//if statement for addition
if (mathOperator == '+')
{
//one for both num1 and num2 being integers
if ((num1-intnum1==0)&&(num2-intnum2==0))
{
addition = num1+num2;
outfile << fixed<< setprecision(0)<< num1 << " + " << num2 << " = " <<addition <<endl;
}
//one for either num1 or num2 being a float
else
{
addition = num1+num2;
outfile << fixed << setprecision(4)<< num1 << " + " << num2 << " = " <<addition <<endl;
}
}
//if statement for subtraction
else if (mathOperator == '-')
{
//one for both num1 and num2 being integers
if ((num1-intnum1==0)&&(num2-intnum2==0))
{
subtraction = num1-num2;
outfile << fixed<< setprecision(0)<< num1 << " - " << num2 << " = " <<subtraction <<endl;
}
//one for either num1 or num2 being a float
else
{
subtraction = num1-num2;
outfile << fixed<< setprecision(4)<< num1 << " - " << num2 << " = " << subtraction <<endl;
}
}
//if statement for multiplication
else if (mathOperator == '*')
{
//one for both num1 and num2 being integers
if ((num1-intnum1==0)&&(num2-intnum2==0))
{
multiplication = num1*num2;
outfile << fixed<< setprecision(0)<< num1 << " * " << num2 << " = " <<multiplication <<endl;
}
//one for either num1 or num2 being a float
else
{
multiplication = num1*num2;
outfile << fixed<< setprecision(4)<< num1 << " * " << num2 << " = " << multiplication <<endl;
}
continue;
}
//if statement for division
//one for both num1 and num2 being integers
else if (mathOperator == '/')
{
if ((num1==intnum1)&&(num2==intnum2))
{
if (num2 == 0)
{
outfile << num1 << " / " << num2 << " = ERROR" <<endl;
cout << "ERROR encountered on line "<<line<<endl;
}
else if ((num2==0)&&(num1==0))
{
division = static_cast<int>(num1)/static_cast<int>(num2);
remainder = (static_cast<int>(num1) % static_cast<int>(num2));
outfile <<setprecision(0)<<fixed<< num1 << " / " << num2 << " = " << division<<"R"<<remainder<<endl;
}
else
{
division = static_cast<int>(num1)/static_cast<int>(num2);
remainder = (static_cast<int>(num1) % static_cast<int>(num2));
outfile <<setprecision(0)<<fixed<< num1 << " / " << num2 << " = " << division<<"R"<<remainder<<endl;
}
}
//one for either num1 or num2 being a float
else
{
if (num2 == 0)
{
outfile << num1 << " / " << num2 << " = ERROR" <<endl;
cout << "ERROR encountered on line "<<line<<endl;
}
else if ((num2==0)&&(num1==0))
{
division = static_cast<int>(num1)/static_cast<int>(num2);
remainder = (static_cast<int>(num1) % static_cast<int>(num2));
outfile <<setprecision(4)<<fixed<< num1 << " / " << num2 << " = " << division<<"R"<<remainder<<endl;
}
else
{
division = static_cast<int>(num1)/static_cast<int>(num2);
remainder = (static_cast<int>(num1) % static_cast<int>(num2));
outfile <<setprecision(4)<<fixed<< num1 << " / " << num2 << " = " << division<<"R"<<remainder<<endl;
}
}
}
//if statement for power
else if (mathOperator == '^')
{
if ((num1-intnum1==0)&&(num2-intnum2==0))
{
if ((num1 == 0)&& (num2 == 0))
{
outfile << num1 << " ^ " << num2 << " = " << "ERROR" <<endl;
cout << "ERROR encountered on line "<<line<<endl;
}
else
{
power = pow(num1 , num2);
outfile <<setprecision(0)<<fixed<< num1 << " ^ " << num2 << " = " << power <<endl;
}
}
else
{
if ((num1 == 0)&& (num2 == 0))
{
outfile << num1 << " ^ " << num2 << " = " << "ERROR" <<endl;
cout << "ERROR encountered on line "<<line<<endl;
}
else
{
power = pow(num1 , num2);
outfile <<setprecision(4)<<fixed<< num1 << " ^ " << num2 << " = " << power <<endl;
}
}
}
//else statement for something other than the listed choices
else
{
if ((num1-intnum1==0)&&(num2-intnum2==0))
{
outfile <<setprecision(0)<<fixed<<num1 << " " << mathOperator << " "<< num2 << " = ILLEGAL"<<endl;
cout <<"ILLEGAL operator encountered on line "<<line<<endl;
}
else
{
outfile <<setprecision(4)<<fixed<< num1 << " " << mathOperator << " "<< num2 << " = ILLEGAL"<<endl;
cout <<"ILLEGAL operator encountered on line "<<line<<endl;
}
}
if( infile.eof() ) break;
}
outfile.close();
infile.close();
return 0;
}
Any help would be greatly appreciated.