Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,500 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,379 people online right now. Registration is fast and FREE... Join Now!




File operations

 
Reply to this topicStart new topic

File operations

k0b13r
1 Mar, 2007 - 04:50 AM
Post #1

D.I.C Head
Group Icon

Joined: 18 Jul, 2006
Posts: 198



Thanked: 1 times
Dream Kudos: 250
My Contributions
Ok, I have a homework to write a program, that get numbers typed by user, than save them to file (liczby.txt), next read them from file, add all of them and get arythmetic average, and output this to another file (I know, it's stupid, but this is my homework)

I have some code here:
CODE

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    int nLiczba = 0, nSuma = 0, nZ_pliku[41];
    float fSrednia = 0;
    int nI = 0;
    ofstream in("liczby.txt");
    do
    {
        cout << "Wpisz liczbe: ";
        cin >> nLiczba;
        in << nLiczba << endl;
    } while(nLiczba != 0);
    in.close();
    ifstream out("liczby.txt");
    while(!out.eof())
    {
        out >> nZ_pliku[nI];
        nSuma+=nZ_pliku[nI];
        nI++;
    }
    out.close();
    fSrednia = static_cast<float>(nSuma) / static_cast<float>(nI);
    ofstream plik("wyjscie.txt");
    plik << "W pliku liczby.txt było " << nI << " liczb, ich suma wynosila " << nSuma << " a średnia ";
}


Everything is almost ok, but this numbers are entered every in next line.
I don't know how to jump to another line of file when program is reading and couting this integers in this place:
CODE

while(!out.eof())
    {
        out >> nZ_pliku[nI];
        nSuma+=nZ_pliku[nI];
        nI++;
    }


How I can do it ?! Thanks for fast answers

This post has been edited by k0b13r: 1 Mar, 2007 - 05:31 AM
User is offlineProfile CardPM
+Quote Post

horace
RE: File Operations
1 Mar, 2007 - 05:02 AM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
you problem is in your loop
CODE

while(!out.eof())
    {
        out >> nZ_pliku[nI];
        nSuma+=nZ_pliku[nI];
        nI++;
    }

the
CODE

out >> nZ_pliku[nI];

reads EOF but you then add a corrupt value to nSuma

try
CODE

    while(out >> nZ_pliku[nI])
    {
        nSuma+=nZ_pliku[nI];
        nI++;
    }

where you attempt a read and if it is OK add to nSuma otherwise exit the while()

User is offlineProfile CardPM
+Quote Post

k0b13r
RE: File Operations
1 Mar, 2007 - 05:06 AM
Post #3

D.I.C Head
Group Icon

Joined: 18 Jul, 2006
Posts: 198



Thanked: 1 times
Dream Kudos: 250
My Contributions
And now everything works perfect, thanks Horace smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 06:28PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month