0

我正在做以下练习:

在此处输入图像描述

我的代码:

#include <string>
#include <fstream>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    ifstream inFile;
    ofstream outFile;

    double currentSalary, increaseRate, updatedSalary;
    string firstName, lastName;

    inFile.open ("Data.txt");
    outFile.open("Output.dat");
    outFile << fixed << showpoint << setprecision(2);
    inFile >> lastName >> firstName;
    inFile >> currentSalary >> increaseRate;
    updatedSalary = currentSalary * (1 + increaseRate / 100);
    outFile << firstName << " " << lastName<< " " << updatedSalary << endl;
    inFile >> lastName >> firstName;
    inFile >> currentSalary >> increaseRate;
    updatedSalary = currentSalary * (1 + increaseRate / 100);

    outFile << firstName << " " << lastName<< " " << updatedSalary << endl;
    inFile >> lastName >> firstName;
    inFile >> currentSalary >> increaseRate;
    updatedSalary = currentSalary * (1 + increaseRate / 100);
    outFile << firstName << " " << lastName<< " " << updatedSalary << endl;

    system("PAUSE");
    return 0;
}

但是当我用 MS VS 调试它时,它只是说“按任意键继续......”

在哪里添加 Data.txt 文件?

4

2 回答 2

3

好吧,鉴于您没有向屏幕输出任何内容,我一点也不感到惊讶,这就是您所看到的。

如果我是你,我会看一下Output.dat文件,看看它是否在写任何东西。

如果您在该文件中看不到任何内容,那么可能是因为您在运行的目录中没有您的Data.txt文件。使用 VS,这通常位于解决方案目录中某处的binordebug目录下。

system("cd");您可以通过将代码放在开头并运行它来找出哪个目录。

于 2011-02-10T02:41:08.937 回答
1

您必须将 Data.txt 放在您将执行二进制文件的同一目录中,或者将 Data.txt 的绝对路径放在,如inFile.open ("C:\Documents\Data.txt"),否则将永远找不到。

于 2011-02-10T02:44:08.447 回答