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