Hello World!
i want to write a program that generates 100 random numbers.

the program should do the following tasks:

** store these 100 random numbers in a file called totalFile.txt
** read these numbers one by one from totalFile.txt and store the even numbers in a file called evenFile.txt and the odd numbers in a file called oddFile.txt.

** finally display the contents of the files evenFile.txt and oddFile.txt to the screen.
Can any one help me!?
Thanks in advance!

Recommended Answers

All 12 Replies

>Can any one help me!?
Yes, where are you having problems with?

BTW, Could you post your own try as well ?

One step at a time. Build your RNG and write the ASCII strings to your text file.
Once that works,
Then load them one at a time.
then finally organizing, and resave to files.

Hello World!
i want to write a program

awesome!

Can any one help me!?

sure can!

Thanks in advance!

you're welcome!

.

commented: Was that necessary with the previous 3 responses? Useless posts like this do get old. -4

edit lol I realized I was wrong.

Here is what i have tried. But it kept saying segmentation fault!

#include<stdio.h>
int main()
{
int in[10];
int out[10];
int i;
FILE *fp;
fp=fopen("totalFile.txt","w");
if(fp==NULL)
printf("Error\n");
for(i=0; i<10; i++)
{
in[i]=rand();
fprintf(fp,"%d\n",in[i]);
}
fclose(fp);
fp=fopen("totalFile.txt","r");
if(fp=NULL)
printf("Error\n");
i=0;
while(i<10)
{
fscanf(fp,"%d",&out[i]);
i++;
}
return 0;
}

Look at line 18: if(fp=NULL) , you assign the value NULL to a pointer, using a NULL pointer is a very dangerous thing to do, you probably meant: if(fp=[B]=[/B]NULL) :)

if(fp==NULL)  // changed assignment to comparison
    printf("Error\n");

Hope this helps!

Edit:: What you need is a good code formatting guide, like this one or AStyle, a program which automatically formats your code...

Here is what i have tried. But it kept saying segmentation fault!

#include<stdio.h>
int main()
{
int in[10];
int out[10];
int i;
FILE *fp;
fp=fopen("totalFile.txt","w");
if(fp==NULL)
printf("Error\n");
for(i=0; i<10; i++)
{
in[i]=rand();
fprintf(fp,"%d\n",in[i]);
}
fclose(fp);
fp=fopen("totalFile.txt","r");
if(fp=NULL)
printf("Error\n");
i=0;
while(i<10)
{
fscanf(fp,"%d",&out[i]);
i++;
}
return 0;
}

There's no need in posting the same thing twice, I have already answered your problem in post #8 of your thread :)

Oh! Thanks a lot!it really helped me!

So, what's stopping you from marking this thread as solved?

this is the answere:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <windows.h>
#include <time.h>
#include <fstream.h>

using namespace std;

int main()

{
      int a=100;
      int random; 
      srand(time(0));    
      while(a) {
       random = rand() % 99 + 1;
          if(random%2 !=0) {
                    ofstream randomN("oddFile.txt", ios::app);
                    randomN << random << endl;
          } else {
                 ofstream randomN2("evenFile.txt", ios::app);
                    randomN2 << random << endl;
          }
                    cout << random << endl;
                    --a;
}
         system("pause");
         return 0;           
}

well this is c++, thought you asked for it sorry...

commented: Go away with your rubbish! -2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.