gpt4 book ai didi

c++ - int main()/函数的问题

转载 作者:行者123 更新时间:2023-11-28 07:18:49 24 4
gpt4 key购买 nike

嗨,我的作业将在几个小时后到期,我正在尝试编写我的代码来生成此输出,但它不起作用。我的程序甚至根本不运行,总是失败,我不知道问题出在哪里。我对在 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

代码:

#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

最佳答案

编写程序的方法是不是编写所有内容然后尝试运行它。 从小而简单开始,一次增加一点复杂性,在每一步都进行测试,永远不要添加不起作用的代码。

这将需要几次迭代。我们将从可以从文件中读取的内容开始:

#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);
}

当您完成此操作后发表评论,我们将进行下一步。

关于c++ - int main()/函数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19827246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com