[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
 

Re: [CPROG] End of file? (Beginning of file??)



Efter at have funderet endnu mindre tror jeg der er en anden løsning,
for den ændring fra while.... til do.... while ændrer ingenting. Jeg
tror der er en linefeed nederst i datafilen, så programmet faktisk læser
en linie mere og looper igen. Hvis datafilen ender med et \n, så går det
galt når der ikke er kontrol med om inddata er et legalt tal.

M.v.h.
Søren


Joachim Holst wrote:
> 
> Helt utan att fundera, så har jag kommit på följande:
> 
> do {
>      infile >> price; // follow-up read
>      sum += price; // add the current price to the total
>      cout << "Price number " << count
>           << " is kr. " << price << endl;
>      count++;
> } while( !infile.eof() );
> cout << '\n' << "Samlet pris: kr. " << sum << endl;
> cout << '\n' << "Snitt pris: kr. " << sum/(float)count << endl;
> 
> while/do looparna beter sig lite olika beroende på när och hur testet utförs.
> Kan tänkas att detta hjälper.
> 
> Skulle du inte sedan också ta genomsnittspriset på varorna ??
> 
> /Jocke!
> 
> Hans Henrik Hansen wrote:
> > I mit - tidligere omtalte - lille C++ kursus er jeg nu nået til en lektion,
> > hvor jeg skal lære at hente (numeriske) data fra en separat fil ind i et
> > program og bearbejde dem lidt. Opgavens ordlyd er:
> >
> > "Write a C++ program which reads floating point numbers from a file and
> > processes them. The numbers represent prices of individual items in a
> > shopping list. As each price is read, the program is to display the price on
> > the screen and accumulate the prices into a total. After all the prices have
> > been read and totalled, the program is to calculate and display the average
> > price of all the items."
> >
> > Jeg har valgt flg. 'priser': 75.90 7.86 4.34 18.14 9.99 10.25 34.90
> > , og lagt dem i en fil, 't12.dat' (i Home Dir)
> >
> > Dernæst har jeg (med lidt bistand fra bogen) lavet flg. program:
> >
> > #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 );
> >       while( !infile.eof() )
> >       {
> >               count++;
> >               infile >> price; // follow-up read
> >               sum += price; // add the current price to the total
> >               cout << "Price number " << count
> >                       << " is kr. " << price << endl;
> >
> >       }
> >       infile.close();
> >       cout << '\n' << "Samlet pris: kr. " << sum << endl;
> >       }
> >
> > , men jeg kan ikke få det til at virke (helt)!:
> >
> > Ovenstående resulterer nemlig i:
> >
> > sslug@sslug:~> t12.out
> > Price number 1 is kr. 75.9
> > Price number 2 is kr. 7.86
> > Price number 3 is kr. 4.34
> > Price number 4 is kr. 18.14
> > Price number 5 is kr. 9.99
> > Price number 6 is kr. 10.25
> > Price number 7 is kr. 34.9
> > Price number 8 is kr. 34.9
> >
> > Samlet pris: kr. 196.28
> >
> > Problemet er jo her, at sidste tal indlæses - og summeres - dobbelt.
> > Jeg har så prøvet at lave forskellige småændringer (initiering af 'count' -
> > flytte lidt omkring med 'count++' osv.
> > Herved kan jeg også undgå dubleringen af sidste tal - til gengæld får jeg så
> > en 'ekstra' pris med i listen (før kr. 75.9); denne ekstra post, som jeg ikke
> > fatter årsagen til, er på ca. -2!! ('sum' angives til 159.38 [skulle være
> > 161.38])
> >
> > Min egen 'forklaring' er, at programmet ikke (korrekt) finder
> > begyndelse/slutning (EOF) på datafilen - men hvorfor kan jeg altså ikke se!
> >
> > Bistand ønskes! :)
> >
> > mvh
> > Hans


 
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] *