首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >作业作业幼稚/外文

作业作业幼稚/外文
EN

Stack Overflow用户
提问于 2016-03-13 17:38:48
回答 2查看 4.3K关注 0票数 2

我的任务规定如下:

一家公司有三名雇员要求特别加薪。您将获得一个文件Ch3_Ex7Data.txt,其中包含以下数据: 米勒安德鲁65789.87 5 格林希拉75892.56 6 Sethi Amit 74900.50 6.1 每个输入行由员工的姓氏、姓名、当前薪资和加薪百分比组成。

例如,在第一个输入行中,员工的姓氏为Miller,名为Andrew,当前的薪资为65789.87,加薪为5 %

编写一个从指定文件中读取数据并将输出存储在文件Ch3_Ex7Output.dat中的程序。对于每个雇员,数据必须以下列形式输出: firstName lastName updatedSalary 将十进制数的输出格式化为小数的两位。

我的代码如下。

代码语言:javascript
复制
#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文件的代码,读取它,然后将其转换为另一个文件。有人觉得这对我有帮助吗?

EN

回答 2

Stack Overflow用户

发布于 2016-03-13 19:16:48

您的代码有很多问题。

两个最明显的阻止程序编译:

代码语言:javascript
复制
cout << "Processing Data....." endl;  //program message

应:

代码语言:javascript
复制
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

一旦所有这些都被修复,循环就变成:

代码语言:javascript
复制
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;
}

产出如下:

代码语言:javascript
复制
Andrew Miller 69079.36
Sheila Green 80446.11
Amit Sethi 79469.43

但是如果你想学习好的生活方式,你也应该:

  • 测试两个文件的打开情况
  • 在循环结束后测试终止是否由错误引起,并发出警告
  • 最后,学习使用调试器.
票数 1
EN

Stack Overflow用户

发布于 2020-06-01 02:45:33

这是我的解决方案

代码语言:javascript
复制
#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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35973478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档