for loop not working

why isnt the for loop running???i want to enter 1000 numbers in 10 different files....following is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
#include<conio.h>
#include<ctime>
#include "processdata.h"
using namespace std;
int main()
{
    int count;
    for(count=1;count<=1000;count++)
    {
    srand((unsigned)time(0));
    int num;
    num=rand()%1000;
    processdata(num);
    }
getch();
}


processdata is as follows..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<fstream>
using namespace std;
ofstream fout;
void processdata(int num)
{
    if(num>=0 && num<100)
    {
        fout.open("1.txt",ios::app);
        
    }
    else if(num>=101 && num<200)
    {
        fout.open("2.txt",ios::app);
    }
    
    else if(num>=201 && num<300)
    {
        fout.open("3.txt",ios::app);
    }
    else if(num>=301 && num<400)
    {
        fout.open("4.txt",ios::app);
    }
    else if(num>=401 && num<500)
    {
        fout.open("5.txt",ios::app);
    }
    else if(num>=501 && num<600)
    {
        fout.open("6.txt",ios::app);
    }
    else if(num>=601 && num<700)
    {
        fout.open("7.txt",ios::app);
    }
    else if(num>=701 && num<800)
    {
        fout.open("8.txt",ios::app);
    }
    else if(num>=801 && num<900)
    {
        fout.open("9.txt",ios::app);
    }
    else if(num>=901 && num<=1000)
    {
        fout.open("10.txt",ios::app);
    }
    fout<<num<<endl;
}

plz help me....
1. ofstream fout; should be inside the function.
2. srand() should be seeded only once outside the for() loop.
3. In case this is not what you want, the hundredths will get skipped. If num < 800 the next should be num >= 800 and not 801 so 800 doesn't get skipped.
One problem you have is that you are leaving out 100, 200, 300, 400, 500, 600, 700, 800, and 900. An example is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

if( num >= 0 && num < 100 )
    will get numbers 0 through 99 inclusive:

else if( num >= 101 && num< 200 )
    will get numbers 101 through 199 inclusive.

Either change your first compare to be:

if( num >= 0 && num < 101 )
    or
if( num >= 0 && num <= 100 )


Topic archived. No new replies allowed.