Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 22nd, 2008, 12:25 PM   #1
1cookie
Newbie
 
Join Date: Mar 2008
Posts: 2
Rep Power: 0 1cookie is on a distinguished road
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:

c++ Syntax (Toggle Plain Text)
  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.













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??
1cookie is offline   Reply With Quote
Old Mar 22nd, 2008, 4:36 PM   #2
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 530
Rep Power: 4 Ancient Dragon is on a distinguished road
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++; 
}
__________________
I Like Ike. Vote for Dwight Eisenhower this November.
--This message brought to you by the the Procrastinators Club Of America.
Ancient Dragon is online now   Reply With Quote
Old Mar 22nd, 2008, 5:42 PM   #3
1cookie
Newbie
 
Join Date: Mar 2008
Posts: 2
Rep Power: 0 1cookie is on a distinguished road
Re: file IO

Quote:
Originally Posted by Ancient Dragon View Post
>>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....
1cookie is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem processing file into a char array csrocker101 C++ 1 May 8th, 2007 11:50 PM
add mutiple users to the smbpasswd file. Pizentios Bash / Shell Scripting 3 Oct 20th, 2005 12:48 PM
After execution - Error cannot locate /Skin File? wchar Visual Basic 1 Mar 5th, 2005 9:04 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM
Structure char field to a disk file ehab_aziz2001 C++ 0 Feb 10th, 2005 2:42 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:45 AM.

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