I am not able to understand the logic of if statement present in this program .Please help
#include<fstream.h> #include<conio.h> #include<process.h> void main() { clrscr(); int n,no,i; ofstream out("D:\\FROMC\\TURBOCPP\\12LAB\\NUMBER.DAT"); cout<<"\n\nEnter n value"; cin>>n; cout<<"\nEnter the numbers:\n"; for(i=0;i<n;i++) { cin>>no; out<<no<<"\n"; } out.close(); ifstream in("D:\\FROMC\\TURBOCPP\\12LAB\\NUMBER.DAT"); ofstream ev("D:\\FROMC\\TURBOCPP\\12LAB\\EVEN.DAT"); ofstream od("D:\\FROMC\\TURBOCPP\\12LAB\\ODD.DAT"); if(in.fail()) { cout<<"File does not exist "; exit(1); } while(!in.eof()) { in>>no; cout<<"\n"; if(!in.eof()) -------------->>>>why is this needed here ? { if(no%2==0) { cout<<no; ev<<no<<"\n"; } else {cout<<"\t\t\t"<<no; od<<no<<"\n"; } } } in.close(); ev.close(); od.close(); getch(); }
@Opcode help please
Hmm. The eof function indicates when the file is done. Let me comment on your code: ``` #include<fstream.h> #include<conio.h> #include<process.h> void main() { clrscr(); int n,no,i; ofstream out("D:\\FROMC\\TURBOCPP\\12LAB\\NUMBER.DAT"); cout<<"\n\nEnter n value"; cin>>n; cout<<"\nEnter the numbers:\n"; for(i=0;i<n;i++) { cin>>no; out<<no<<"\n"; } out.close(); ifstream in("D:\\FROMC\\TURBOCPP\\12LAB\\NUMBER.DAT"); ofstream ev("D:\\FROMC\\TURBOCPP\\12LAB\\EVEN.DAT"); ofstream od("D:\\FROMC\\TURBOCPP\\12LAB\\ODD.DAT"); if(in.fail()) { cout<<"File does not exist "; exit(1); } while(!in.eof()) //if not at end of file, continue reading numbers { in>>no; cout<<"\n"; if(!in.eof())//How do I explain this, but this part is like the switch off. //(Try deleting the line above and read the output) { if(no%2==0) { cout<<no; ev<<no<<"\n"; } else {cout<<"\t\t\t"<<no; od<<no<<"\n"; } } } in.close(); ev.close(); od.close(); getch(); } ```
switch off?
Okay that's a bad way to put it -.- I'm not exactly sure how to word this but: ``` if(!in.eof()) ``` Means to execute the next statement when the file is done. Is that better wording?
what I feel is like this is not required because we checked this in while loop itself
No it's needed. With out it you're program would end itself early I presume? Imagine this: You have to program a robots every move. If you did not it wouldn't do anything. if(!in.eof()) is like that, when we reach the end of file it executes the next command. If you removed it I would think the program would skip the a portion of the code, or maybe not even run at all. http://mathbits.com/MathBits/CompSci/Files/End.htm
ok its very late here ..nothing seems to sink in ... i will read this tomorrow and reply if I have doubt ..Thanks for the help anyways :)
Okay Okay. Meanwhile I'm moving to my next class. I'll try elaborating more on this later.
ok seeya :)
if(!in.eof()) if( not in,eof() ) The exclamation point means not in C derived languages. This if statement evaluates to: while we haven't finished the file, execute the following code block.
@rsmith6559 i get it now thanks a lot ! :)
Join our real-time social learning platform and learn together with your friends!