试图将C 文件写入文件时获得错误的结果
Getting wrong results when trying to write out to a file in c++
因此,我的此程序的目的是根据对所读取的信息进行的计算读取输入并输出结果到文件。我不断得到的问题是,我在输出文件中不断获得相同的结果。
首先要注意我正在使用Visual Studio和预编译标头,因此我的标题配置有些不同。
当我编译您的代码时,编译器肯定会警告我使用" int"是一个问题。除非我确定我不需要,否则我总是会删除警告,除非我确定自己可以安全地忽略警告。
对于"雇员",我有:
#pragma once
using namespace std;
class employee
{
public:
void readData(ifstream& inf);
//Precondition: File must exist, be accessible, and have the appropriate contents
//Postcondition: The contents in the file are assigned to a variable.
void Compute(double basepay);
//Precondition: basepay = $800
//Postcondition: Salary is found using the basepay plus the three other basepays percentages.
void print(ofstream& outf) const;
//Precondition: The new values (ID and salary) must be updated and accessible.
//Postcondition: The ID and salary must be displayed in an output file. The salary must be in the proper format.
employee();
//Precondition: The variables ID, Job_class, Years, Ed, and sal must have been created already.
//Postcondition: the variables ID, Job_class, Years, Ed, and sal are all set to zero.
private:
int ID;
int Job_class;
int Years;
int Ed;
double sal;
};
请注意," sal"是双人。
for"雇员.cpp"我有:
#include "stdafx.h"
#include "employee.h"
using namespace std;
void employee::readData(ifstream& inf)
{
inf >> ID >> Job_class >> Years >> Ed;
}
void employee::Compute(double basepay)
{
//int extra = .01;
//int bp1 = 0;
//int bp2 = 0;
//int bp3 = 0;
double extra = .01;
double bp1 = 0;
double bp2 = 0;
double bp3 = 0;
if (Job_class == 1)
{
bp1 = basepay*.05;
}
if (Job_class == 2)
{
bp1 = basepay*.10;
}
if (Job_class == 3)
{
bp1 = basepay*.20;
}
if (Years > 0 && <= 10)
{
bp2 = basepay*.05;
}
if (Years> 10)
{
bp2 = basepay*(.05 + extra);
}
if (Ed == 1)
{
bp3 = 0;
}
if (Ed == 2)
{
bp3 = basepay * 05;
}
if (Ed == 3)
{
bp3 = basepay*.12;
}
if (Ed == 4)
{
bp3 = basepay*.20;
}
sal = basepay + bp1 + bp2 + bp3;
}
void employee::print(ofstream& outf) const
{
outf << ID << setw(15) << fixed << showpoint << setprecision(2) << "$ " <<
sal << endl;
}
employee::employee() // default constructor
{
ID = 0;
Job_class = 0;
Years = 0;
Ed = 0;
sal = 0.0;
}
对于主我有:
#include "stdafx.h"
using namespace std;
#include "employee.h"
int main()
{
double basepay = 800;
employee emp;
ifstream inf;
inf.open("G:\Temporary\employee.txt");
ofstream outf;
outf.open("G:\Temporary\employeeout.txt");
if (!inf)
{
cout << " *** Input file does not exist! *** " << endl;
return 1;
}
outf << "ID #" << setw(20) << "Salary" << endl;
outf << "****************************" << endl;
while (!inf.eof())
{
emp.readData(inf);
emp.Compute(basepay);
emp.print(outf);
}
inf.close();
outf.close();
return 0;
}
for"雇员.txt"我有:
1078 2 12 3
1070 3 10 4
请注意,没有逗号
for" employeeout.txt"我有:
ID # Salary
****************************
1078 $ 1024.00
1070 $ 1160.00
1070 $ 1160.00
请注意,该程序正在两次读取最后一行。我没有解决这个问题;我认为这是个人喜好如何处理的问题,以便您可以做到这一点。
整数舍入。这似乎是一个问题。从使用int
切换到使用double
。
相关文章:
- C++系统找不到指定的文件错误
- C++语法头文件错误
- C++生成文件错误"implicit entry/start for main executable"
- C++/生成文件错误:未定义对"main"的引用
- VS2019:资源文件错误:中性(默认)(未知子语言:0x8)
- C++程序不制作文件(错误)
- 安卓工作室 |CPP 文件错误错误: 位图库中对"AndroidBitmap_unlockPixels"的未定义引用
- "what does ":*** [可执行文件] 错误 1 " mean ?"
- 如何修复使用VScode调试器gcc调试时的"找不到文件"错误
- 在课堂上创建了一个朋友,给出"无输入文件"错误
- Qt .exe文件错误
- c++ 添加具有写入文件错误的文本变量
- 无法转换 .CATPart 文件.错误:输入文件路径似乎包含不支持的字符
- 写入文本文件错误,QT
- C++写入文件错误
- Windows Subsystem for Linux (WSL) 下的间歇性随机"找不到文件"错误
- 犰狳读取MAT文件错误
- 无法求解FullPathForFilename:COCOS2DX中可能缺少文件错误
- C Wininet FTP列出了许多文件错误
- 生成文件错误 - 找不到文件 - *.cpp