家庭作业 输入/输出文件
Homework Assignment infile / outfiles
我的作业如下:
一家公司的三名员工将获得特别加薪。您将获得一个文件,
Ch3_Ex7Data.txt
,其中包含以下数据:米勒·安德鲁 65789.87 5
绿色希拉 75892.56 6
塞西·阿米特 74900.50 6.1每个输入行由员工的姓氏、名字、当前工资和加薪百分比组成。
例如,在第一个输入行中,员工的姓氏为Miller
,名字为Andrew
,当前工资为65789.87
,加薪为5 %
。
编写一个程序,从指定文件中读取数据并将输出存储在文件
Ch3_Ex7Output.dat
中。对于每个员工,数据必须按以下形式输出:
firstName lastName updatedSalary
将十进制数的输出格式化为小数点后两位。
我的代码如下。
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
//Declaring the variables
string firstName;
string lastName;
double payIncrease;
double pay;
ifstream inFile;
ofstream outFile;
inFile.open("C:\Users\Megan\Ch3_Ex7Data.txt"); //opens the input file
outFile.open("C:\Users\Megan\Ch3_Ex7Output.dat"); //opens a output file
outFile << fixed << showpoint;
outFile << setprecision(2); // Output file only having two decimal places
cout << "Processing Data....." endl; //program message
while (!inFile.eof() //loop
inFile >> lastName >> firstName >> pay >> payIncrease;
pay = pay*(pay*payIncrease);
outFile << firstName << " " << lastName << " " << pay << "/n";
inFile.close();
outFile.close();
return 0;
}
由于某种原因,我似乎无法获得代码来打开我现有的.txt
文件,读取它,然后将其转换为另一个文件。有没有人看到这有什么问题可以帮助我?
你的代码有很多问题。
两个最明显的阻止程序编译:
cout << "Processing Data....." endl; //program message
应该是:
cout << "Processing Data....." << endl; //program message
while (!inFile.eof() //loop
至少应while (!inFile.eof() )//loop
这还不是全部:
while (!inFile.eof())
是一个反习语:你测试文件结束,然后读取并进行处理,即使发生了错误或文件结束。您必须在阅读后进行测试。
正如您在评论中所说,没有{ }
,只有重复while
后的第一行,这不是您想要的。
增加百分比的正确公式至少pay = pay*(1 + payIncrease/100.);
pay = pay+(pay*payIncrease/100.);
添加'/n'
作为行尾是完全错误的。字符是'n'
(注意反斜杠),无论如何,您应该始终用C++写endl
。
一旦所有问题都固定下来,循环就会变成:
for (;;) { //loop
inFile >> lastName >> firstName >> pay >> payIncrease;
if (! inFile) break; // exit on eof or error
pay = pay*(1 + payIncrease/100.);
outFile << firstName << " " << lastName << " " << pay << endl;
}
输出为:
Andrew Miller 69079.36
Sheila Green 80446.11
Amit Sethi 79469.43
但是,如果你想学习好的实践,你还应该:
- 测试两个文件的打开
- 在循环结束后测试终止是否由错误引起并发出警告
- 最后,同样重要的是学习使用调试器...
的解决方案
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main() {
// Write your main here
//Declarations
string FirstName;
string LastName;
double Pay;
double Increase;
double UpdatedSalery;
//File object and open txt and dat files per instructions and use user input for the txt input file
ifstream FileIn;
string FileName;
cout << "enter a file name: ";
cin >> FileName;
FileIn.open(FileName);
ofstream FileOut("Ch3_Ex5Output.dat");
FileOut << setprecision(8);
while(FileIn >> LastName >> FirstName >> Pay >> Increase){
UpdatedSalery = ((Pay*(Increase/100))+Pay);
FileOut << " " << FirstName << " " << LastName << " " << UpdatedSalery << endl;
}
FileIn.close();
FileOut.close();
return 0;
}
- 如何在 的开头<x>和结尾<y>之间更改带有文件输出的字符串的值
- C++对象数组的文件输出
- bcdedit 文件输出不起作用,但其他命令可以
- 如何从文本文件输出数据?(学生成绩单计划)
- 使用 C++文件 I/O 输出 pdf 文件
- C 文件输出垃圾
- 从堆栈中读取字符后,如何修复不正确的文件输出
- C++:具有随机名称的文件输出
- 在命令提示符下看到换行符,但在文件输出中可以看到 相同的字符串
- 生成文件不会生成预期的文件输出
- C++11 fgetc 在"r+b"模式下使用时向我的文件输出 0
- 使用函数从文件输出
- GCC.obj文件输出不具有确定性(.debug_info,PROGPITS部分)
- C cstdiofile :: Writestring给出了不正确的文件输出
- C 输出:STD :: COUT和文件输出没有相同的内容
- GCC和MSVC之间的文件输出不一致
- C++文件输出奇怪的数字,并且部分代码没有运行
- 如何将特定字符从TXT文件输出到特定点
- C 调用从TXT文件输出信息的函数
- 将交易从文本文件输出到屏幕