Code:#include<fstream.h> #include<iostream.h> void main() { int k=0; fstream f; f.open("num.txt",ios::in|ios::out); for(int i=1;i<=100;i++) { f<<i; for(int j=2;j<i/2;j++) { if(i%j==0) continue; if(i%j==0) f<<k; } } }
Code:#include<fstream.h> #include<iostream.h> void main() { int k=0; fstream f; f.open("num.txt",ios::in|ios::out); for(int i=1;i<=100;i++) { f<<i; for(int j=2;j<i/2;j++) { if(i%j==0) continue; if(i%j==0) f<<k; } } }
Last edited by Salem; 06-09-2007 at 12:40 AM. Reason: [CODE][/CODE] goes AROUND THE CODE!!!!!
Mmmkay... First of all.
1) Post with code tags.
2) Indent properly (Dunno if you did that, since not using code tags means it's not indented properly.).
3) void main() should be int main().
Not to mention that you are using fstream.h and iostream.h which are deprecated header files, use fstream and iostream (without .h) instead. You should also tell us what you are trying to do *in details*, what your code does instead, what you have tried to do to fix it (if it doesn't work) or what you have tried to do to implement it, if you have troubles figuring how to code whatever you are trying to code. Just don't put unformatted code bluntly like that, you won't get much from us that way. It's considered quite rude on programming forums.
And Why do you open file for reading if you only write?Code:if(i%j==0) continue; if(i%j==0) //this will never be true f<<k; //so this will neve execute //and even if it will - k is always 0 - you never change it
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Also why do you have two ifs for the same thing with the continue?
There now just fix it up so we can understandCode:#include<fstream> #include<iostream> using namespace std; int main(void){ int k=0; fstream f; f.open("num.txt",ios::in|ios:ut); for(int i=1;i<=100;i++){ f<<i; for(int j=2;j<i/2;j++){ if(i%j==0) continue; if(i%j==0) f<<k; } } return 0; }
Good call =D
Now there is a bit of JS which is supposed to check whether code has [code][/code] tags around it.
It is not sufficient to simply throw tags into the post just to make the error message go away.
Read the intro threads to learn how to do it properly.
This is the 2nd time I've fixed a post of theirs, it will not happen again.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.