Issues with int main()/functions
Issues with int main()/functions
嗨,我的作业将在几个小时后到期,我正在尝试编写代码以生成此输出,但它不起作用。我的程序甚至根本无法运行,总是失败,我不知道问题是什么。我在 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);
}
当您完成此操作时,请发表评论,我们将进行下一步。
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 代码在main()中运行,但在函数中出现错误
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 函数是否可以访问传递给main()的参数
- 在main()之外初始化std::vector会导致性能下降(多线程)
- 我的 int main() 中出现堆栈溢出错误
- C++变量名(可以将 main 声明为变量,但对于其他函数名称则不然)
- 当我尝试在 Main 中调用插入函数时,它不是取数字?
- File.cpp.o:OpenPose 标志 CMakeFiles/.. 的多重定义/main.cpp.o:首先在这里定
- 为什么类和 main() 函数中也有动态内存分配
- 在 main.cpp 的上下文中找不到目录(带有 CMake 的快板)
- 我想在 Main 中用 C++ 调用其他类中的一个类,但我做不到
- "main"函数堆栈中的对象在第一个任务运行时被覆盖 (FreeRTOS)
- 为什么 'main' 函数中的局部 int 变量会自动初始化?
- C++ assigment std::list:<typename>:itrator 在 main 中工作,但在方法中它不起作用
- 为什么在我的 main 函数中声明整数后我的整数数组中会出现错误?
- 为什么要从main()返回NULL?
- Issues with int main()/functions