Difficulty analyzing text from a file

Posted by Nikko on Stack Overflow See other posts from Stack Overflow or by Nikko
Published on 2013-10-19T21:37:44Z Indexed on 2013/10/19 21:55 UTC
Read the original article Hit count: 181

Filed under:

I'm running into a rather amusing error with my output on this lab and I was wondering if any of you might be able to hint at where my problem lies.

The goal is find the high, low, average, sum of the record, and output original record. I started with a rather basic program to solve for one record and when I achieved this I expanded the program to work with the entire text file. Initially the program would correctly output:

346 130 982 90 656 117 595 High# Low# Sum# Average#

When I expanded it to work for the entire record my output stopped working how I had wanted it to.

0 0 0 0 0 0 0 High: 0 Low: 0 Sum: 0 Average: 0 0 0 0 0 0 0 0 High: 0 Low: 0 Sum: 0 Average: 0 etc...

I cant quite figure out why my ifstream just completely stopped bothering to input the values from file.

I'll go take a walk and take another crack at it. If that doesn't work I'll be back here to check for any responses =)

Thank you!

#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);
}

© Stack Overflow or respective owner

Related posts about c++