🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #573

Summing a comma delinted file

#573
From: Wayne McDermott
Date: Sun, 23 Sep 2001 07:43
75 lines
3370 bytes
I have a file that consists of consumption values by month for stock. The
file begins with the material number for the item followed by a comma then
the consumption for July97, Aug97 etc. Each line represents one item and is
terminated by the endline character. If there was no consumption for the
month then there will be two commas, for instance the first item is
represented as

462,2,,5,7,8,13,,,5,1
481,,12,5,8,32,9,81,,,5,4

where 462 is the stock number for the first item, 481 is the stock number
for the second etc.

I am attempting to write code that will read the input file, sum the
consumption till it reaches the endl and write the sum to outfile.dat. At
this stage I don't mind that the stock number itself will also be added to
the sum, I can't get it to step past the first number anyway. Here is the
code I have so far

#include <fstream>
int main()
{
    using namespace std;

    ifstream instream;
    ofstream outstream;

    instream.open("tblConsumptionByPeriod.txt");
    if (instream.fail())
    {
        cout << "Input file opening failed.\n";
        exit(1);
    }
    outstream.open("outfile.dat");
    if (outstream.fail())
    {
        cout << "Output file opening failed.\n";
        exit(1);
    }
    int sum = 0;
    double next;


    instream >> next;
    while (!instream.eof())   //While we haven't read to the end of the file
        {
        while (instream >> next);
            {
            instream >> next;
            do
            {
                sum = sum + next;
                cout << sum << '\n';
                outstream << sum << '\n';
                instream >> next;
            }
            while (next != '\n'); //Do till we reach the end of the line
             }
        }
        instream.close();
        outstream.close();

        return 0;
}

Stepping through the code indicates that the instream >> next command is not
actually moving to the next double and it has me stumped. I probably have
problems to overcome after that but all it does at the moment is endlessly
add 462 to itself and write that to outfile.dat.

Any hints greatly appreciated, I have tried using get and trapping the
commas but that doesn't appear to work either.

TIA
Wayne
Technical Details
Message-ID: <5vgr7.27572$c5.208712@newsfeeds.bigpond.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!intgwpad.nntp.telstra.net!newsfeeds.bigpond.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 114592,a8ecdb8ba824bdd3,start
X-Google-Attributes: gid114592,public
X-Google-Thread: 103c8d,a8ecdb8ba824bdd3,start
X-Google-Attributes: gid103c8d,public
X-Google-ArrivalTime: 2001-09-23 00:52:32 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!intgwpad.nntp.telstra.net!newsfeeds.bigpond.com!not-for-mail
From: "Wayne McDermott" <elhombre@bigpond.com>
Newsgroups: alt.comp.lang.learn.c-c++,borland.public.cppbuilder.language
Subject: Summing a comma delinted file
Lines: 80
X-Newsreader: Microsoft Outlook Express 4.72.3110.1
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
Message-ID: <5vgr7.27572$c5.208712@newsfeeds.bigpond.com>
Date: Sun, 23 Sep 2001 17:43:37 +1000
NNTP-Posting-Host: 144.134.89.149
X-Trace: newsfeeds.bigpond.com 1001231425 144.134.89.149 (Sun, 23 Sep 2001 17:50:25 EST)
NNTP-Posting-Date: Sun, 23 Sep 2001 17:50:25 EST
Organization: Telstra BigPond Internet Services (http://www.bigpond.com)