[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

End of file? (Beginning of file??)



Jeg ville vende tilbage vedr. 'skoleløsningen', som jeg nu har afprøvet - og 
den virker OK!
Som sagt lå den nær min egen løsning, som jeg nu har ændret til:

#include<iostream>
#include<fstream>

void main( void )
  {
  float price = 0.0;
  float sum = 0;
  int count = 0;
  fstream infile;
  infile.open( "topic12.dat", ios::in );
  infile >> price;// ny linie!
  while( !infile.eof() )
  {
	sum += price;
	count++;
	cout << "Price number " << count
	<< " is kr. " << price << endl;
	infile >> price;
  }
  infile.close();
  cout << '\n' << "Samlet pris: kr. " << sum << endl;
  }

Den virker også - hovedforskellen fra mit første forsøg er linie 11 
(kommenteret: 'Ny linie') [Desuden er der ændret lidt i rækkefølgen i 
'while'-løkken]

Men jeg har stadig svært ved at gennemskue, hvad linie 11 gør (dér)?
Tilsyneladende overføres første 'pris' til infile, mens count = 0, hvorefter 
count inkrementeres, og øvrige priser overføres, summeres og udskrives(?)
Jeg ville dog finde det mere naturligt at overføre samtlige 'priser' inde i 
løkken - men det er åbenbart ikke muligt/hensigtsmæssigt??

mvh
Hans

PS	Jeg husker stadig, at opgaven bad om gennemsnit - ikke sum! :)


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:09 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *