您好我的作业将在几个小时后到期,我正在尝试编写我的代码来生成此输出,但它无法正常工作。我的程序根本没有运行,总是失败,我不知道问题是什么。我在int main()中有什么问题以及如何处理从文件到函数的数据有问题!我一直在努力......需要大力帮助!!!!!谢谢你的时间
示例输入文件:
Miss Informed
125432 32560.0
Sweet Tooth
5432 9500
Bad Data
1255 -4500.0
John Smith
1225 3500.0
Nancy Brown
1555 154500.0
CODE:
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main()
{
float CalcIncomeTax(float );
float CalcNetSalary(float, float );
bool OpenFile(ifstream& fin);
bool OpenFile(ofstream& fout);
void Instruct(void);
void ReadData(ifstream & fin, string& Name , int &Id, float& grossIncome);
void Print(ofstream&, string, int, float, float, float);
ifstream fin;
ofstream fout;
string Name;
int Id = 0;
float grossIncome = 0;
float netSalary;
float incomeTax = 0;
Instruct ();
netSalary = CalcNetSalary(grossIncome,incomeTax);
incomeTax = CalcIncomeTax(grossIncome);
Print(fout, Name, Id, grossIncome, incomeTax, netSalary);
ReadData(fin, Name, Id, grossIncome);
OpenFile(fin);
{
getline(fin, Name);
while (!fin.eof())
{
fin >> Id >> grossIncome;
cout << setw(20) << left << Name
<< setw(8) << right << Id
<< setw(10) << grossIncome << endl;
fin.ignore(10,'\n');
fin >> Id >> grossIncome;
}
getline(fin,Name);
}
OpenFile(fout);
ReadData(fin, Name, Id, grossIncome);
fin.close();
}
bool OpenFile(ifstream&fin)
{
cout <<"\nEnter the name and location of the input file: ";
string file_input;
getline(cin, file_input);
fin.open(file_input.c_str() ) ;
if(fin.fail())
return false;
else
return true;
}
bool OpenFile(ofstream &fout)
{
cout <<"Enter the name and location of the output file: ";
string file_output;
getline(cin, file_output);
fout.open( file_output.c_str() );
if (fout.fail())
return false;
else
return true;
}
void Instruct()
{
cout << "Programmer:"<< setw(25) << "//" << endl;
cout << "Programming Assignment" << setw(5) << "4" << endl;
cout << "This program will calculate and report tax liability" << endl;
}
float CalcIncomeTax(float grossIncome)
{
float incomeTax = 0;
if (grossIncome <= 3500)
{
incomeTax = 0.00;
}
else if (grossIncome >= 3500 && grossIncome <= 8000)
{
incomeTax = 0 + 0.06 * (grossIncome - 3500);
}
else if (grossIncome >= 8000 && grossIncome <= 20000)
{
incomeTax = 270.00 + 0.11 * (grossIncome - 8000);
}
else if (grossIncome >= 20000 && grossIncome <= 34000)
{
incomeTax = 1590.00 + 0.17 * (grossIncome - 20000);
}
else if (grossIncome >= 34000 && grossIncome <= 54000)
{
incomeTax = 3970.00 + 0.24 * ( grossIncome - 34000);
}
else if (grossIncome >= 54000)
{
incomeTax = 8770.00 + 0.32 * ( grossIncome - 52000);
}
else if (grossIncome < 0)
{
cout << "****Invalid Income";
}
return(incomeTax);
}
float CalcNetSalary( float grossIncome, float incomeTax)
{
float netSalary;
netSalary = grossIncome - incomeTax;
return (netSalary);
}
void Print(ofstream& fout, string Name, int Id, float grossIncome, float incomeTax, float netSalary)
{
cout << setfill(' ') << left << setw(18) << "\tName";
cout << setfill(' ') << left << setw(12) << "ID";
cout << setfill(' ') << left << setw(17) << "Gross Income";
cout << setfill(' ') << left << setw(12) << "Taxes";
cout << setfill(' ') << left << setw(16) << "Net Income";
cout << endl;
cout << setfill('=') << setw(70)<<"\t";
cout<<endl;
cout << setprecision(2) << showpoint << fixed;
cout << setfill(' ') << "\t" << setw(17)<< Name;
cout << setfill(' ') << setw(12) << Id;
cout << '$' << setfill(' ') << setw(16) << grossIncome;
cout << '$' << setfill(' ') << setw(11) << incomeTax;
cout << '$' << setfill(' ') << setw(16) << netSalary;
cout << endl;
}
输出应该如何
Name ID Gross Income Taxes Net Income
Miss Informed 125432 $32560.00 **** Invalid ID
Sweet Tooth 5432 $9500.00 $435.00 $9065.00
Bad Data 1255 $-4500.00 **** Invalid Income
John Smith 1225 $3500.00 $0.00 $3500.00
Nancy Brown 1555 $154500.00 $40930.00 $113570.00
答案 0 :(得分:2)
编写程序的方法是而不是来编写所有内容然后尝试运行它。 从小而简单开始,一次添加一点复杂性,在每一步测试,永远不会添加到不起作用的代码。
这将需要几次迭代。我们将从可以从文件中读取的内容开始:
#include <iostream>
#include <fstream>
#include <string>
using namespace std; // This is a good TEMPORARY SHORTCUT.
int main()
{
ifstream fin("inputdata");
string firstName, lastName;
fin >> firstName >> lastName;
cout << "First name is " << firstName << endl;
cout << "Last name is " << lastName << endl;
return(0);
}
当你有这个工作时发表评论,我们将进行下一步。