For over the few months I've being trying to display LIST the of IPV4 Adresses in C++ programming , but unfotunately when I am going to compile the code it done properly but it shows nothing to me and error message generate please fix this problem you genius guy's...

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <stdlib.h>
#include <time.h>
#define MAX 30
#define TEMP 5

using namespace std;
void randomNumber_array(int *arr[]) {
    int r=0;
    srand(time(NULL));
    for(int i=0;i<4;++i)
    {
        r=0+rand()%246;
        *arr[i]=r;
    }

}
void ipAdress_listGenrator(char *ip_str,int num) {
    int i,j,temp=0,*a[TEMP],arr[TEMP];
    char temp_str[TEMP];

    for(i=0;i<num;++i)
    {
        for(j=0;j<4;++j)
        {
            a[i]=&arr[i];
        }
        randomNumber_array(a);
        for(j=0;j<4;++j)
        {
            sprintf(temp_str,"%d",*a[j]);
            if(i<3)
            {
                strcat(ip_str,temp_str);
                strcat(ip_str,".");
            } else {
                strcat(ip_str,temp_str);
            }
        }
        cout<<ip_str<<endl;
        *ip_str='\0'; //clearing a string 
    }
}
int main() {
    char ip_string[MAX]={'\0'};
    unsigned long int n;

    cout<<"Enter a number : "; cin>>n;
    ipAdress_listGenrator(ip_string,n);

    system("pause");
    return 0;
}

Recommended Answers

All 3 Replies

It crashes because you are using i instead of j in a couple of places. Look at line 28.

As for the program as a whole, it's too complicated. You have an array of integers and an array of pointers to integers. The array of pointers to integers point to the array of integers so they add nothing and only complicate things, so my vote would be to get rid of them. You have a few other unneeded or unused variables. For example, this function...

void randomNumber_array(int *arr[]) {
    int r=0;
    srand(time(NULL));
    for(int i=0;i<4;++i)
    {
        r=0+rand()%246;
        *arr[i]=r;
    }

}

Can and should be simplified to this...

void randomNumber_array(int arr[]) 
{
    for(int i=0;i<4;++i)
    {
        arr[i]=rand()%246;
    }
}

Stick the srand(time(NULL)); at the top op main. You call it once and only once.

Change line 30 to...

randomNumber_array(arr);

Line 33 becomes...

sprintf(temp_str,"%d",arr[j]);

With that you can get rid of the a array entirely and with it, the bug that crashed the program.

Thank you Brother you are the best in the world.

My code crashed again after I made certain changes in it and I still don't know what'swrong with my code

/*
here is my code which I made changes and run 
*/

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <stdlib.h>
#include <time.h>
#define MAX 30
#define TEMP 5
using namespace std;
void randomNumber_array(int arr[]) 
{
    for(int i=0;i<4;++i)
    {
        arr[i]=0+rand()%246;
    }
}
void ipAdress_listGenrator(char *ip_str,int num) {
    int i,j,temp=0,*a[TEMP],arr[TEMP];
    char temp_str[TEMP];
    for(i=0;i<num;++i)
    {
        for(j=0;j<4;++j)
        {
            a[j]=&arr[j];
        }
        randomNumber_array(arr);
        for(j=0;j<4;++j)
        {
            sprintf(temp_str,"%d",*a[j]);
            if(i<3)
            {
                strcat(ip_str,temp_str);
                strcat(ip_str,".");
            } else {
                strcat(ip_str,temp_str);
            }
        }
        cout<<ip_str<<endl;
        *ip_str='\0'; //clearing a string 
    }
}
int main() {
    srand(time(NULL));
    char ip_string[MAX]={'\0'};
    unsigned long int n;

    cout<<"====================="<<endl;
    cout<<" IPV4 List Generator "<<endl;
    cout<<"====================="<<endl;
    cout<<endl;
    cout<<"How many IP-Adresses you want to generate : "; cin>>n;
    ipAdress_listGenrator(ip_string,n);
    system("pause");
    return 0;
}
/*
It give me that output 
=====================
 IPV4 List Generator
=====================

How many IP-Adresses you want to generate : 10
121.37.107.11.
18.40.188.33.
132.84.208.156.
4118583214
58169052
1381792388
15210113049
9824417462
22073108174
529395173
Press any key to continue . . .

*/
// And here is the new which I made certain changes 
// in it because I want to store the IP-Adress string into the 2-D string array

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <stdlib.h>
#include <time.h>
#define MAX 30
#define TEMP 5
#define MAXIMUM 10000
using namespace std;
void randomNumber_array(int arr[]) 
{
    for(int i=0;i<4;++i)
    {
        arr[i]=0+rand()%246;
    }
}
void ipAdress_listGenrator(char *ip_str,int num,char ip_array[][MAXIMUM]) {
    int i,j,temp=0,arr[TEMP],c=0,k=0;
    char temp_str[TEMP];
    for(i=0;i<num;++i)
    {
        randomNumber_array(arr);
        for(j=0;j<4;++j)
        {
            sprintf(temp_str,"%d",arr[j]);
            if(i<3)
            {
                strcat(ip_str,temp_str);
                strcat(ip_str,".");
            } else {
                strcat(ip_str,temp_str);
            }
        }
        for(c=0;c<strlen(ip_str);++c)
        {
            ip_array[k][c]=ip_str[c];
            ++c;
        }
        ip_array[k][c]='\0';
        ++k;
        c=0;
        *ip_str='\0'; //clearing a string 
    }
}
int main() {
    srand(time(NULL));
    char ip_string[MAX]={'\0'},ip_string_arr[MAXIMUM][MAXIMUM];
    unsigned long int n;

    cout<<"====================="<<endl;
    cout<<" IPV4 List Generator "<<endl;
    cout<<"====================="<<endl;
    cout<<endl;
    cout<<"How many IP-Adresses you want to generate : "; cin>>n;
    ipAdress_listGenrator(ip_string,n,ip_string_arr);

   cout<<"List Generated "<<endl;
   for(int i=0;i<n;++i)
   {
       cout<<ip_string_arr[i]<<endl;
   }

   system("pause");
   return 0;
}

//Result Program Crashed
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.