找不到解决方案

Can't Find a solution

本文关键字:解决方案 找不到      更新时间:2023-10-16

我使用的是visualstudios,这个程序从一个名为inmpg的txt文件中获取数据,然后打印到一个生成的名为outmpg.txt的文件中。代码没有错误,但当控制台窗口出现时,它只会进入"从文件读取",而不会继续。"outmpg.txt"已创建,但为空。感谢您的帮助。

#include <iostream> 
#include <fstream> // For file I/O 
using namespace std;
int main()
{
float amt1; // # of gallons for fillup 1 
float amt2; // # of gallons for fillup 2 
float amt3; // # of gallons for fillup 3 
float amt4; // # of gallons for fillup 4 
float startMiles; // Starting mileage 
float endMiles; // Ending mileage 
float mpg; // Computed miles per gallon 
ifstream inMPG; // Holds gallon amts & mileages. Input 
ofstream outMPG; // Holds miles per gall. Output 
                 // Open the files 
inMPG.open("inmpg.txt");
if (inMPG.fail())
{
    cout << "can't find inmpg.txt" << endl;
    return 0;
}
outMPG.open("outmpg.txt");
if (outMPG.fail())
{
    cout << "can't create/ open outmpg.txt" << endl;
    return 0;
}
// Get data (priming read) 
cout << "Reading from file" << endl;
inMPG >> amt1 >> amt2 >> amt3 >> amt4 >> startMiles >> endMiles;
while (!inMPG.eof())
{
    // Compute miles per gallon 
    mpg = (endMiles - startMiles) / (amt1 + amt2 + amt3 + amt4);
    // Output results 
    cout << "wrote to file outmpg.txt" << endl;
    outMPG << "For the gallon amounts" << endl;
    outMPG << amt1 << ' ' << amt2 << ' ' << amt3 << ' ' << amt4 << endl;
    outMPG << "and a starting mileage of " << startMiles << endl;
    outMPG << "and an ending mileage of " << endMiles << endl;
    outMPG << "the mileage per gallon is " << mpg << endl;
    cout << "n Reading the next set of data" << endl;
    inMPG >> amt1 >> amt2 >> amt3 >> amt4 >> startMiles >> endMiles;
}
return 0;

}

问题来自于您第一次开始读取inMPG文件时。如果只有一个条目,则循环永远不会执行,并且会跳过它,因为它已经到达文件的末尾。因此永远不会写入outMPG文件。

我所做的是删除while循环上方的两行,并将它们放在while循环内部的开头,还删除了循环内部的最后两行。

另一个问题是.eof()调用。至于为什么它在这种情况下实际上不起作用,我无法理解,但我使用了.peek()。因此,该语句可以更改为while(inMPG.peek()!=EOF)。

这是为我工作的代码片段

#include <iostream>
#include <fstream> // For file I/O
using namespace std;
int main()
{
    float amt1; // # of gallons for fillup 1
    float amt2; // # of gallons for fillup 2
    float amt3; // # of gallons for fillup 3
    float amt4; // # of gallons for fillup 4
    float startMiles; // Starting mileage
    float endMiles; // Ending mileage
    float mpg; // Computed miles per gallon
    ifstream inMPG; // Holds gallon amts & mileages. Input
    ofstream outMPG; // Holds miles per gall. Output
    // Open the files
    inMPG.open("inmpg.txt");
    if (inMPG.fail())
    {
        cout << "can't find inmpg.txt" << endl;
        return 0;
    }
    outMPG.open("outmpg.txt");
    if (outMPG.fail())
    {
        cout << "can't create/ open outmpg.txt" << endl;
        return 0;
    }
    while(inMPG.peek() != EOF)
    {
        cout << "Reading the next set of data" << endl;
        inMPG >> amt1 >> amt2 >> amt3 >> amt4 >> startMiles >> endMiles;
        // Compute miles per gallon
        mpg = (endMiles - startMiles) / (amt1 + amt2 + amt3 + amt4);
        // Output results
        cout << "wrote to file outmpg.txt" << endl;
        outMPG << "For the gallon amounts" << endl;
        outMPG << amt1 << ' ' << amt2 << ' ' << amt3 << ' ' << amt4 << endl;
        outMPG << "and a starting mileage of " << startMiles << endl;
        outMPG << "and an ending mileage of " << endMiles << endl;
        outMPG << "the mileage per gallon is " << mpg << endl;
    }
    return 0;
}

我的输入文件是这样的;

24.0 25.0 23.0 20.0 30.0 50.0