Re: Writing a .txt file
"Lucress Carol" <incognito.me@gmx.de> ha scritto nel messaggio
news:1e004544-6c9c-4841-affb-c8fd4f72e845@c65g2000hsa.googlegroups.com...
3- finally write in a .txt file the
number j(number of time that the for-loop has been executed)
and the number S for each j like this:
j=0 S=x
j=1 S=x
j=2 S=x
Steps 1 and 2 work fine.
Does someone has an idea how I can write
the js and the number S in a .txt file?
Hi Lucress,
add #include <fstream> to your include file list, and try this adjustment to
your main() function:
<code>
int main()
{
srand((unsigned)time(0));
int p=3;
int j;
double S,array[3];
// *** Create output file
ofstream outFile;
outFile.open( "I:\\OutFile.txt" );
for(j=0;j<3;j++){
RandomNum(-1.5,1.5 ,p,&array[0]);
S=AdditionArr (&array[0],p);
for (int k=0; k<p; k++){
cout << "array"<<"["<< k << "]" <<" = " << array[k] << endl;
}
cout << "S=" << S << endl;
// *** Write to file
outFile << "j=" << j << " S=" << S << endl;
cout << endl;
}
// *** Closes the file
// (The destructor does that, too)
outFile.close();
return 0;
}
</code>
I used ofstream class to write text data to file.
My comments to explain new code are identified by // ***
I've tested that code and it seems to work...
BTW: I prefer using std::vector< double > or valarray of doubles, instead of
raw C arrays... just IMHO.
HTH,
Giovanni