Random Number Generator (ipv4)

I'm trying to get this code to work and cannot figure it out where what exactly is wrong with this. I've switched it up so much that im just stuck now and need some guidance as to what it's doing and what I need to do to complete this.

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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int num = 100;

void ipv4(string createdAddress)
{
  int count = 0;
  time_t t;
  time(&t);
  srand(t);
    
  while(count < num)
    {
      for(int i = 0; i < 4; i++)
	{
	  string createdAddress << rand() % 255 +1;
	  ‪  if(i != 3)
	      {
	      string createdAddress <<  "." << endl;
	      }
	}
      count++;
    }
}

int main()
{ 
  string createdAddress; 
  ipv4(createdAddress); 
  cout << "createdAddress" << endl;
  return 0;
}


Currently, im getting these errors as it stands:
1
2
3
4
5
6
random.cpp:23: error: stray ‘\342’ in program
random.cpp:23: error: stray ‘\200’ in program
random.cpp:23: error: stray ‘\252’ in program
random.cpp: In function ‘void ipv4(std::string)’:
random.cpp:22: error: expected initializer before ‘<<’ token
random.cpp:25: error: expected initializer before ‘<<’ token


Thanks.
string is your data type. You have specified createdAddress as string as argument for your function. You cant define its type again in line 24, remove the string there and in line 22. Another thing is the way you pass your argument. Right now its read only. Try: void ipv4(string& createdAddress), if you want to change its value.
Anyway whats the purose of ‪
if(i != 3)
{
string createdAddress << "." << endl;
}

don't you want an random address in the style of 127.0.0.1 ??

what you would get now is
127.
0.
01.
Try this one and tell me how it suits you:
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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int num = 100;

void ipv4(string& createdAddress)
{
  int count = 0;
  time_t t;
  time(&t);
  srand(t);
  ostringstream myOutput;

  while(count < num)
    {
      for(int i = 0; i < 4; i++)
        {
        myOutput <<  rand() % 256;
        myOutput <<  ".";
        };
      myOutput << endl;
      count++;
    };
    createdAddress += myOutput.str();
};

int main()
{
  string createdAddress = "";
  ipv4(createdAddress);
  cout << createdAddress << endl;
  return 0;
};
Yes, that works out quite well. At one point I was really close to that solution, was passing by reference, etc. I'm sorta just now getting that under my belt though. For the life of me, I could not wrap my head around getting the periods in the right spot.

Now, with the code the way it is now, there is still an extra period attached to the end of the addresses. So far i've tried adding an a for loop after rand() to handle: myOutput << "."; ,but we know that doesn't work.

It would seem there must be something I can do with strings there's, but im just not familiar with using them beyond what i've done here thus far. My current idea is to erase anything with a period and a space. I'm gonna keep plugging away though.

Also, there was a warning about my srand(), but apparently doing it like this:srand((unsigned) t); sufficiently suppresses the warning. For whosoever is wandering the same thing.


Theses errors
1
2
3
random.cpp:23: error: stray ‘\342’ in program
random.cpp:23: error: stray ‘\200’ in program
random.cpp:23: error: stray ‘\252’ in program
are apparently from copy/paste operations on my others boxes text editor. And are easily solved by putting the code in a real IDE or something geared for coding. For whosoever is wandering that as well.
Last edited on
Removal of extra period - you if statement per se was correct, but you need to get rid of that endl

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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int num = 100;

void ipv4(string& createdAddress)
{
  int count = 0;
  time_t t;
  time(&t);
  srand(t);
  ostringstream myOutput;

  while(count < num)
    {
      for(int i = 0; i < 3; i++)
        {
        myOutput <<  rand() % 256;
        myOutput <<  ".";
        };
      myOutput <<  rand() % 256;
      // I didn't do the if because why would you check something a bazillion times which happens only at the beginning / end of a loop?
      myOutput << endl;
      count++;
    };
    createdAddress += myOutput.str();
};

int main()
{
  string createdAddress = "";
  ipv4(createdAddress);
  cout << createdAddress << endl;
  return 0;
};


Another thing: you might want to loop ipv4() instead of doing an inner loop, you could use it for other purposes then as well...in other words, why don't you let your function create one IP exactly and loop the whole function, then you could easily control the program...


As for
random.cpp:23: error: stray ‘\342’ in program
random.cpp:23: error: stray ‘\200’ in program
random.cpp:23: error: stray ‘\252’ in program

yes there was some copypasta there...
Last edited on
Yeah I got those weird errors after I opened the file with gedit, I guess it adds some chars in there apparently. This things been in gedit, visual studio and now im in Eclipse. But when I was going to do a transfer to a usb stick, that's when I opened it in gedit, and I started getting those weird errors...I think they usually work themselves out if you save them in a real code editor, just never really noticed I guess.

Man, I was damn close to your solution there, I had another rand(), but I was doing something weird with the loop when I tried that, so it passed me up that it was so simple. The way I was thinking about it was, " iterate the random numbers 3 times, then once without the period assignment. But was just getting some mess. I still don't fully understand why simply calling rand() again on it gets rid of the period.

I do have more functions I will be adding, so looping ipv4 might be a good idea. My plan was to call ipv4 from another function though. I have it going to stdout now just to troubleshoot it. Should have seem me trying to implement str::erase in there in an attempt to get that last period out of there :) I'll be around, thanks.
Last edited on
Topic archived. No new replies allowed.