Re: [Dev-C++] How to add the total.......
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Daniel K. O. <dan...@ya...> - 2005-04-15 02:47:56
|
LETCHUMANAN SAMINADAN wrote: > I'm having a problem in in adding the total amount, my program as below... There are some serious errors in your code; you might want to read a book - or at least a good tutorial - on C programming. > while(!feof(col)) That's not the way to read a file. Unlike Pascal, in C (and in most of the existing languages) I/O functions can't predict the future; feof() will not tell you "in the future, when you try to read from this file, I'll find nothing to read"; instead, it tells you "last time you tried to read, I reached the end-of-file". See http://www.eskimo.com/~scs/C-faq/q12.2.html What you need is to test the result of the function you used to access the file (e.g. fscanf() will return the number of successful assignments). --- while (fscanf(col,"%f",&data.utl) == 1) { ... } --- > fscanf(col,"%f\n",&data.utl); Always avoid using a '\n' in *scanf() calls. See http://www.eskimo.com/~scs/C-faq/q12.17.html For each read operation, you might also want to accumulate the value; initialize a variable before the while with zero, and add the read value to it: --- float total = 0; while (fscanf(col,"%f",&data.utl) == 1) { total += data.utl; } --- > fflush(stdin); You shouldn't do this; See http://www.eskimo.com/~scs/C-faq/q12.26.html Daniel K. O. |