Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   file IO (http://www.programmingforums.org/showthread.php?t=15454)

1cookie Mar 22nd, 2008 12:25 PM

file IO
 
hi





Write a c++ program, which uses File Input/Output and performs all of the following tasks;


a. reads 10 numbers {1; 2; 4.5; 2.5*10^10; -3; 0.5; 8*10^-10; -10;3.5} from one file.


b. reads another 5 numbers: {5*10^-6; 2; -7; 4.5; 5*10^5} from another file.

c. calculates the product of all 15 numbers and writes result to the screen and to a third file.


my code at the moment is:

:

  1.  
  2. int main(){
  3.  
  4. float r[10], prod;
  5. fstream file_in;
  6.  
  7. file_in.open("C:\\Users\\Cookson\\Desktop\\file1.txt",ios::in);
  8.  
  9. int i =0;
  10. while(!file_in.eof())
  11. {
  12. file_in >> r[i];
  13. cout<<r[i]<<endl;
  14. i++;
  15. }
  16.  
  17.  
  18. }


I have a file named: 'file1.txt' on my desktop, it has the first ten values in it (as above!).


When i compile and run the program, sure enough it reads the ten numbers into my array r[]. But instead of displaying ten numbers to the screen i get 11, I get an extra number that i don't want namely: 1.4013e-044!!

Why is this number appearing and for what reason??



help please.



:X









I have a file named: 'file1.txt' on my desktop, it has the fist ten values in it (as above!).


When i compile and run the program, sure enough it reads the ten numbers into my array r[]. But instead of displaying ten numbers to the screen i get an extra number that i don't want namely: 1.4013e-044!!

Why is this number appearing and for what reason??

Ancient Dragon Mar 22nd, 2008 4:36 PM

Re: file IO
 
>>Why is this number appearing and for what reason??
Because you are using feof() incorrectly on line 10. Rearrange your program like below. You rarely, if ever, have to use feof().
:

while(file_in >> r[i])
{
 cout<<r[i]<<endl;
i++;
}


1cookie Mar 22nd, 2008 5:42 PM

Re: file IO
 
Quote:

Originally Posted by Ancient Dragon (Post 142768)
>>Why is this number appearing and for what reason??
Because you are using feof() incorrectly on line 10. Rearrange your program like below. You rarely, if ever, have to use feof().
:

while(file_in >> r[i])
{
 cout<<r[i]<<endl;
i++;
}



thanks....:)


All times are GMT -5. The time now is 6:17 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC