Need help. I don\'t know why the last line of output always read twice #include<
ID: 3879059 • Letter: N
Question
Need help. I don't know why the last line of output always read twice
#include<iostream> /*Header file for input and output stream (cin, and cout)*/
#include<fstream> /*Header file for file input stream and file output stream*/
using namespace std;
int main() /*main() function definition starts here*/
{
ifstream in; /*in is an object for ifstream class, ifstream is for input file stream*/
in.open("C:\Users\cartoon1961\Desktop\transaction.txt");/*opening the file "transaction.txt" in read mode*/
float balance, bankfee;/*variable declaration*/
int checks;/*variable declaration*/
while (!in.eof())/*while it is not the end-of-the file, loop repeats*/
{
in >> balance;/*reading balance from the file into variable called 'balance'*/
in >> checks;/*reading checks from the file into variable called 'checks'*/
if (balance<0)/* if the balance is less than zero, enters into body of if */
{
if (checks<20 && checks >= 0) /*if checks are more than or equal to zero and less than 20*/
{
bankfee = 10 + (0.1*checks) + 15; /*claculates the bankfee*/
}
else if (checks >= 20 && checks<40)/*if checks are more than or equal to 20 and less than 40*/
{
bankfee = 10 + (0.08*checks) + 15;/*claculates the bankfee*/
}
else if (checks >= 40 && checks<60)/*if checks are more than or equal to 40 and less than 60*/
{
bankfee = 10 + (0.06*checks) + 15;/*claculates the bankfee*/
}
else if (checks >= 60)/*if checks are more than or equal to 60*/
{
bankfee = 10 + (0.04*checks) + 15;/*claculates the bankfee*/
}
cout << "Beginning balance:$" << balance << endl; /*printing details*/
cout << "Number of checks written:" << checks << endl;
cout << "Your account is overdrawn!" << endl;
cout << "The bank fee this month is:$" << bankfee << endl << endl;
} /*end of if*/
else if (balance<400 && balance >= 0) /*otherwise, if balance is greater than or equal to 0 and less than 400 */
{
if (checks<20 && checks >= 0)/*if checks are more than or equal to zero and less than 20*/
{
bankfee = 10 + (0.1*checks) + 15;/*claculates the bankfee*/
}
else if (checks >= 20 && checks<40)/*if checks are more than or equal to 20 and less than 40*/
{
bankfee = 10 + (0.08*checks) + 15;/*claculates the bankfee*/
}
else if (checks >= 40 && checks<60)/*if checks are more than or equal to 40 and less than 60*/
{
bankfee = 10 + (0.06*checks) + 15;/*claculates the bankfee*/
}
else if (checks >= 60)/*if checks are more than or equal to 60*/
{
bankfee = 10 + (0.04*checks) + 15;/*claculates the bankfee*/
}
cout << "Beginning balance:$" << balance << endl;/*printing details*/
cout << "Number of checks written:" << checks << endl;
cout << "The bank fee this month is:$" << bankfee << endl << endl;
}/*end of else*/
else if (balance >= 400)/*otherwise, if balance greater than or equal to 400*/
{
if (checks<0) /*if checks are less than 0*/
{
cout << "Beginning balance:$" << balance << endl;/*printing details*/
cout << "Number of checks written:" << checks << endl;
cout << "Number of checks must be zero or more." << endl << endl;
}/*end of inner if*/
else /*otherwise*/
{
if (checks<20 && checks>0)/*if checks are more than or equal to zero and less than 20*/
{
bankfee = 10 + (0.1*checks);/*claculates the bankfee*/
}
else if (checks >= 20 && checks<40)/*if checks are more than or equal to 20 and less than 40*/
{
bankfee = 10 + (0.08*checks);/*claculates the bankfee*/
}
else if (checks >= 40 && checks<60)/*if checks are more than or equal to 40 and less than 60*/
{
bankfee = 10 + (0.06*checks);/*claculates the bankfee*/
}
else if (checks >= 60)/*if checks are more than or equal to 60*/
{
bankfee = 10 + (0.04*checks);/*claculates the bankfee*/
}
cout << "Beginning balance:$" << balance << endl;/*printing details*/
cout << "Number of checks written:" << checks << endl;
cout << "The bank fee this month is:$" << bankfee << endl << endl;
}/*end of inner else*/
}/*end of else*/
}/*end of loop*/
return 0;
}/*end of main() function*/
Explanation / Answer
#include<iostream> /*Header file for input and output stream (cin, and cout)*/
#include<fstream> /*Header file for file input stream and file output stream*/
using namespace std;
int main() /*main() function definition starts here*/
{
ifstream in; /*in is an object for ifstream class, ifstream is for input file stream*/
in.open("C:\Users\cartoon1961\Desktop\transaction.txt");/*opening the file "transaction.txt" in read mode*/
float balance, bankfee;/*variable declaration*/
int checks;/*variable declaration*/
while (!in.eof())/*while it is not the end-of-the file, loop repeats*/
{
in >> balance;/*reading balance from the file into variable called 'balance'*/
in >> checks;/*reading checks from the file into variable called 'checks'*/
if (balance<0)/* if the balance is less than zero, enters into body of if */
{
if (checks<20 && checks >= 0) /*if checks are more than or equal to zero and less than 20*/
{
bankfee = 10 + (0.1*checks) + 15; /*claculates the bankfee*/
}
else if (checks >= 20 && checks<40)/*if checks are more than or equal to 20 and less than 40*/
{
bankfee = 10 + (0.08*checks) + 15;/*claculates the bankfee*/
}
else if (checks >= 40 && checks<60)/*if checks are more than or equal to 40 and less than 60*/
{
bankfee = 10 + (0.06*checks) + 15;/*claculates the bankfee*/
}
else if (checks >= 60)/*if checks are more than or equal to 60*/
{
bankfee = 10 + (0.04*checks) + 15;/*claculates the bankfee*/
}
cout << "Beginning balance:$" << balance << endl; /*printing details*/
cout << "Number of checks written:" << checks << endl;
cout << "Your account is overdrawn!" << endl;
cout << "The bank fee this month is:$" << bankfee << endl << endl;
} /*end of if*/
else if (balance<400 && balance >= 0) /*otherwise, if balance is greater than or equal to 0 and less than 400 */
{
if (checks<20 && checks >= 0)/*if checks are more than or equal to zero and less than 20*/
{
bankfee = 10 + (0.1*checks) + 15;/*claculates the bankfee*/
}
else if (checks >= 20 && checks<40)/*if checks are more than or equal to 20 and less than 40*/
{
bankfee = 10 + (0.08*checks) + 15;/*claculates the bankfee*/
}
else if (checks >= 40 && checks<60)/*if checks are more than or equal to 40 and less than 60*/
{
bankfee = 10 + (0.06*checks) + 15;/*claculates the bankfee*/
}
else if (checks >= 60)/*if checks are more than or equal to 60*/
{
bankfee = 10 + (0.04*checks) + 15;/*claculates the bankfee*/
}
cout << "Beginning balance:$" << balance << endl;/*printing details*/
cout << "Number of checks written:" << checks << endl;
cout << "The bank fee this month is:$" << bankfee << endl << endl;
}/*end of else*/
else if (balance >= 400)/*otherwise, if balance greater than or equal to 400*/
{
if (checks<0) /*if checks are less than 0*/
{
cout << "Beginning balance:$" << balance << endl;/*printing details*/
cout << "Number of checks written:" << checks << endl;
cout << "Number of checks must be zero or more." << endl << endl;
}/*end of inner if*/
else /*otherwise*/
{
if (checks<20 && checks>0)/*if checks are more than or equal to zero and less than 20*/
{
bankfee = 10 + (0.1*checks);/*claculates the bankfee*/
}
else if (checks >= 20 && checks<40)/*if checks are more than or equal to 20 and less than 40*/
{
bankfee = 10 + (0.08*checks);/*claculates the bankfee*/
}
else if (checks >= 40 && checks<60)/*if checks are more than or equal to 40 and less than 60*/
{
bankfee = 10 + (0.06*checks);/*claculates the bankfee*/
}
else if (checks >= 60)/*if checks are more than or equal to 60*/
{
bankfee = 10 + (0.04*checks);/*claculates the bankfee*/
}
cout << "Beginning balance:$" << balance << endl;/*printing details*/
cout << "Number of checks written:" << checks << endl;
cout << "The bank fee this month is:$" << bankfee << endl << endl;
}/*end of inner else*/
}/*end of else*/
}/*end of loop*/
return 0;
}/*end of main() function*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.