Извините за заголовок не по теме, поздно правила прочитал.
Добро время суток! Помогите пожалуйста новичку опять. язык С++
Есть текстовый файл income.txt, c текстом:
Код:
Miss Informed
125432 32560.0
Sweet Tooth
5432 9500
Bad Data
1255 -4500.0
John Smith
1225 3500.0
Nancy Brown
1555 154500.00
Пограмма читает содержимое файла, распихивает его по переменным.
Потом обратабывает, выводит на экран и пишет в output.txt
Вопрос
При вытаскивании информации из текстового файла последняя строчка
"Nancy Brown 1555 154500.00" повторяется 2 раза, как от этого избавиться.
Пробовал while (inputFile >> name >> lastName >> id >> income)
но тогда не получается проверка на пустой файл...
Как решить проблему, посоветуйте пожалуйста
Код:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void Header(ofstream &);
void getIputFile(string , ifstream &);
void getOutputFile(string, ofstream &);
void showResult(int, string, string, double, double, double, ofstream&);
void incomeCheck(int, string, string, double, ofstream&);
void idCheck(int, string, string, double, ofstream&);
int main ()
{
// Initializing varialbles
ifstream inputFile;
ofstream outputFile;
string filenameIn, filenameOut, name, lastName;
int id, counter = 0, counter2=0;
double income,netIncome, taxes;
// Open input file
getIputFile(filenameIn, inputFile);
// Check inputfile for wrong name or location
while(inputFile.fail())
{
inputFile.clear();
cout<<endl<<"\aError opening the inputfile! Please try again"<<endl<<endl;
cout<<"Enter the name of the filename: ";
getline (cin,filenameIn);
inputFile.open(filenameIn.c_str());
}
// Open output file
getOutputFile(filenameOut, outputFile);
// Check output file for wrong name or location
while(outputFile.fail())
{
cout<<endl<<"\aError opening the output file! Please try again"<<endl<<endl;
cout<<"Enter the name of the output file: ";
getline (cin, filenameOut);
outputFile.open(filenameOut.c_str());
}
// Show Header
Header(outputFile);
// Reading data from the input file
while (!inputFile.eof())
{
inputFile >> name >> lastName >> id >> income;
// Processing the data
if (income <= 3500 && income>0)
{
if (id > 0 && id<9999)
{
netIncome = income;
taxes = 0;
showResult(id, name, lastName, income, taxes, netIncome, outputFile);
}
else
{
idCheck(id, name, lastName, income, outputFile);
}
}
else if (income > 3500 && income <= 8000)
{
if (id > 0 && id<9999)
{
taxes = (income-3500)*0.06;
netIncome= income - taxes;
showResult(id, name, lastName, income, taxes, netIncome, outputFile);
}
else
{
idCheck(id, name, lastName, income, outputFile);
}
}
else if (income > 8000 && income <= 20000)
{
if (id > 0 && id<9999)
{
taxes = (income-8000)*0.11+270;
netIncome = income - taxes;
showResult(id, name, lastName, income, taxes, netIncome, outputFile);
}
else
{
idCheck(id, name, lastName, income, outputFile);
}
}
else if (income > 20000 && income <= 34000)
{
if (id > 0 && id<9999)
{
taxes = (income - 20000) * 0.17 + 1590;
netIncome = income - taxes;
showResult(id, name, lastName, income, taxes, netIncome, outputFile);
}
else
{
idCheck(id, name, lastName, income, outputFile);
}
}
else if (income > 34000 && income <= 54000)
{
if (id > 0 && id<9999)
{
taxes = (income-34000 ) * 0.24+3970;
netIncome = income - taxes;
showResult(id, name, lastName, income, taxes, netIncome, outputFile);
}
else
{
idCheck( id, name, lastName, income, outputFile);
}
}
else if (income > 54000)
{
if (id > 0 && id<9999)
{
taxes = (income-54000) * 0.32+8770;
netIncome = income - taxes;
showResult(id, name, lastName, income, taxes, netIncome, outputFile);
}
else
{
idCheck(id, name, lastName, income, outputFile);
}
}
else
{
incomeCheck(id, name, lastName, income, outputFile);
}
counter++;
}
// Check for empty file
if (counter=0)
{
for(int i=0; i<3;i++) //for fun
cout<<"\n\n\a ---->The input file is empty. goodbye!<----\n\n";
exit(0);
}
// Close I/O files
inputFile.close();
outputFile.close();
cout << endl << endl;
return 0;
}