Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 02.11.2013, 14:19   #1
elCamino
Новичок
Джуниор
 
Регистрация: 01.11.2013
Сообщений: 6
По умолчанию cout<<"Помогите новичку 2"<<endl;

Извините за заголовок не по теме, поздно правила прочитал.
Добро время суток! Помогите пожалуйста новичку опять. язык С++
Есть текстовый файл 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;
}

Последний раз редактировалось elCamino; 02.11.2013 в 14:47.
elCamino вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
cout<<"Помогите новичку :)"<<endl; elCamino Помощь студентам 2 02.11.2013 06:29
Постоянно слетает галочка "автоматически" в "Параметры Excel", "Формулы", "Вычисления в книге" Alexsandrr Microsoft Office Excel 4 19.10.2013 14:22
Как написать это-> printf("%4.2f",n) через cout Виндовс 7 Общие вопросы C/C++ 1 23.01.2012 14:17
cout << "Можно ли остановить закрытие?" Byurrer Общие вопросы C/C++ 16 20.01.2011 12:21