家庭作业 输入/输出文件

Homework Assignment infile / outfiles

本文关键字:文件 输出 输入 家庭作业      更新时间:2023-10-16

我的作业如下:

一家公司的三名员工将获得特别加薪。您将获得一个文件,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;
}