0

我在这个实验室的输出中遇到了一个相当有趣的错误,我想知道你们中是否有人能够暗示我的问题出在哪里。

目标是找到记录的最高、最低、平均、总和,并输出原始记录。我从一个相当基本的程序开始解决一条记录,当我实现这一点时,我扩展了程序以处理整个文本文件。最初程序会正确输出:

346 130 982 90 656 117 595 高# 低# 总和# 平均#

当我将它扩展为适用于整个记录时,我的输出停止了我想要的工作。

0 0 0 0 0 0 0 高:0 低:0 总和:0 平均:0 0 0 0 0 0 0 0 高:0 低:0 总和:0 平均:0 等等...

我无法弄清楚为什么我的 ifstream 完全停止了从文件中输入值的麻烦。

我会去散步,再试一次。如果这不起作用,我会回到这里检查任何回复=)

谢谢!

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
    int num;
    int high = 0;
    int low = 1000;
    double average = 0;
    double sum = 0;
    int numcount = 0;
    int lines = 1;
    char endoline;
    ifstream inData;
    ofstream outData;

    inData.open("c:\\Users\\Nikko\\Desktop\\record5ain.txt");
    outData.open("c:\\Users\\Nikko\\Desktop\\record5aout.txt");
    if(!inData) //Reminds me to change path names when working on different computers.
    {
        cout << "Could not open file, program will exit" << endl;
        exit(1);
    }
    while(inData.get(endoline))
    {
        if(endoline == '\n')
            lines++;
    }

    for(int A = 0; A < lines; A++)
    {
        for(int B = 0; B < 7; B++)
        {
            while(inData >> num)
            inData >> num;
            numcount++;
            sum += num;
            if(num < low)
                low = num;
            if(num > high)
                high = num;
            average = sum / numcount;
            outData << num << '\t';
        }
        outData << "High: " << high << " " << "Low: " << low << " " << "Sum: " << sum << " " << "Average: " << average << endl; 
    }   

inData.close();
outData.close();
return(0);
}
4

3 回答 3

0

感谢你们两位花时间帮助我解决这个问题。我从你的两个回答中借了一点。

在我的第一个 while 循环之后,我添加了 inData.clear(); & inData.seekg(0);。添加这些阻止了我的 inData >> num 被 -858993460 填充。

我还删除了讨厌的 while(inData >> num) 并简单地将其替换为 inData >> num;

最后,我在第一个 for 循环中将需要重新初始化的变量设置为 0。

于 2013-10-20T18:17:23.497 回答
0

In the inner loop you read until the stream enters failure-mode, e.g., because an incorrect format was received or the stream reached its end. Once the stream entered failure-mode it will stay in this mode until the error flag is used, e.g., using inData.clear(). I don't know how your input looks but assuming it is a file consisting entirely of numbers, it will just read the entire file. If you want to break out of reading earlier, you need to do something about. For example, you could use a manipulator to skip over whitespace and set the stream into a failure state when reaching a newline:

std::istream& skip(std::istream& in) {
    std::istream::sentry cerberos(in, false);
    if (in) {
        while (std::isspace(in.peek()) {
            if (in.get() == '\n') {
                std::in.setstate(std::ios_base::failbit);
            }
        }
    }
}

... later, when reading integers use this:

int value(0);
while (in >> skip >> value) {
    // ...
}
// ... 
if (!in.eof()) {
    in.clear();
}

When skip encounters a newline, the stream is set into failure-mode as a record (line) was read. After processing the line, the stream state is cleared unless the failure was due to also reaching EOF which would set the std::ios_base::eofbit which is tested using in.eof().

于 2013-10-19T21:49:47.070 回答
0

I think you have too many cycles :)

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int num;
int high = 0;
int low = 1000;
double average = 0;
double sum = 0;
int numcount = 0;
int lines = 1;
char endoline;

ifstream inData;
ofstream outData;

inData.open("c:\\Users\\Nikko\\Desktop\\record5ain.txt");
outData.open("c:\\Users\\Nikko\\Desktop\\record5aout.txt");
if(!inData) //Reminds me to change path names when working on different computers.
{
    cout << "Could not open file, program will exit" << endl;
    exit(1);
}
while(inData.get(endoline))
{
    if(endoline == '\n')
        lines++;

    sum = 0;
    average = 0.;
    high  = 0;
    low = 1000;
    //you have to reinitialize these values for every row
    while(inData >> num) 
    {
        numcount++;
        sum += num;
        if(num < low)
            low = num;
        if(num > high)
            high = num;
        average = sum / numcount;
        outData << num << '\t';
    }
    outData << "High: " << high << " " << "Low: " << low << " " << "Sum: " << sum << " " << "Average: " << average << endl; 
}


inData.close();
outData.close();
return(0);
}
于 2013-10-19T21:52:47.963 回答