separating two loops a part

EDIT: Please read my last reply, if this is confusing

I want to loop each loop by itself, as you can see the first one and the second one will make random Strings based on how LONG the String should be, However, when I generate them, the first loop comes correct( if the user input 2 I get two random chars) the second one comes wrong( if the user input 3 I get 3 chars the same repeated not random which is wrong ), the random chars are the same:

what I get with my code


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
How Long The 1st String Should Be? (normal is: 2) 2
How Long the 2nd String Should Be? 3
Add A prefix O 

here is your resualts 
/em/aaa/o
/xo/aaa/o
/hh/aaa/o
/be/aaa/o
/nj/aaa/o
/fj/aaa/o
/xj/aaa/o
/zd/aaa/o
/iq/aaa/o
/qc/aaa/o
/pu/aaa/o
/bl/aaa/o
/em/aaa/o
/vs/aaa/o
/tc/aaa/o
/yc/aaa/o
/on/aaa/o
/dp/aaa/o
/jw/aaa/o
/ct/aaa/o



what I want to accomplish is 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
How Long The 1st String Should Be? (normal is: 2) 2
How Long the 2nd String Should Be? 3
Add A prefix O 

here is your resualts 
/em/wer/o
/xo/sdf/o
/hh/rwf/o
/be/sfs/o
/nj/sfc/o
/fj/sxf/o
/xj/hbd/o
/zd/enw/o
/iq/ndr/o
/qc/bfs/o
/pu/bdr/o
/bl/rgd/o
/em/gsv/o
/vs/ssr/o
/tc/gsr/o
/yc/hrb/o
/on/ert/o
/dp/bre/o
/jw/hfx/o
/ct/xer/o



so 1stRandomStrings/2ndRandomStrings/Prefix
    
example : /ct/sda/o
          /ca/dac/o


what I get is /ca/dac/o
              /xd/dac/o




as you can see "dac" string is the same!! it should be random etc.. I used seeding/timer but it didn't work
also the program takes the user input and adds "/" char ( you can ignore this I know how to add it )



the first one is
1
2
        
lengthOfStringZ


the second one is lengthOfString


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

char genRandom()
{
    return 'a' + rand() % 26;  // <--- this to be assigned for the 1st loop to //make random letters from A TO Z variables.
}
char genRandom2()

{
    srand (time(NULL));
    return 'a' + rand() % 26;  // <--- // <--- this to be assigned for the 2nd //loop to make random letters from A TO Z 
        
            for (int strLen2 = 0;strLen2 < ; ++strLen2)
            {

                Str2 += genRandom(); // AS YOU CAN SEE HERE WE RANDOMIZE THE 1ST // LOOP
            }


        for (int numStrings = 0; numStrings < numOfStrings; numStrings++)
        {
            for (int strLen = 0; strLen < lengthOfString; ++strLen)
            {
                Str += genRandom2(); // HERE IS THE SECOND ONE 
                }




            std::cout  << Str <<"/"<<Str2 <<"/"<<PREFIX ;







Last edited on
Please note I have made modifications to the code , even the 1st 2 strings are not random any more


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
        cout << "\nHow Many loops Would You Like Today?: ";
        cin >> numOfStrings;  //

        cout << "How Long The 1st String Should Be? (normal is: 2) ";
        cin >> lengthOfString;
        cout << "How Long the 2nd String Should Be ?";
        cin >> lengthOfStringZ;
        cout << "Add The Target File e.a up.php ";
        cin >> PREFIX;


  for (int numStrings = 0; numStrings < numOfStrings; numStrings++)
        {
            for (int strLen = 0; strLen < lengthOfString; ++strLen)
            {
                Str += genRandom();
                }
                for (int strLen2 = 0; strLen2 < lengthOfStringZ; ++strLen2)
                {
                    Str2 += genRandom2();
                    }


            std::cout << Str <<"/"<<Str2 <<"/"<<PREFIX ;


How Many Loops: 20
How Long The 1st String Should Be? (normal is: 2) 2
How Long the 2nd String Should Be? 3
Add Prefix  k


/sn/vvv/k
/eo/vvvvvv/k
/eo/vvvvvvvvv/k
/eo/vvvvvvvvvvvv/k
/eo/vvvvvvvvvvvvvvv/k
/eo/vvvvvvvvvvvvvvvvvv/k
/eo/vvvvvvvvvvvvvvvvvvvvv/k
/eo/vvvvvvvvvvvvvvvvvvvvvvvv/k
/eo/vvvvvvvvvvvvvvvvvvvvvvvvvvv/k
/eo/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv/k
/eo/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv/k
/eo/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv/k
/eo/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv/k
/eo/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv/k
/eo/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv/k
/eo/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv/k
/eo/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv/k
/eo/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv/k
/eo/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv/k
/eo/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv/k
Last edited on
I also tried

1
2
3
4
5
6
7
8
 for (int numStrings = 0; numStrings < numOfStrings; numStrings++)
        {
            for (int strLen = 0; strLen < lengthOfString; ++strLen)
            {
                Str += genRandom();
                Str2 += genRandom2();

                }


I get


inurl:/nj/rr/k
inurl:/jj/rr/k
inurl:/jj/rr/k
inurl:/jj/rr/k
inurl:/jj/rr/k
inurl:/jj/rr/k
inurl:/jj/rr/k
inurl:/jj/rr/k
inurl:/jj/rr/k
inurl:/jj/rr/k


both 1st and second strings are not randomized

I also used at the end of the loop:

1
2
3

            Str.clear();  // <--- Clears the the 1st string  
            Str2.clear();Clears the2nd string  
Consider:

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

string genrand(int nochars)
{
	static set<string> allStrings;

	string s;

	do {
		for (int i = 0; i < nochars; ++i)
			s += 'a' + rand() % 26;

	} while (!allStrings.insert(s).second);

	return s;
}

int main()
{
	srand(time(0));

	int numOfStrings, lengthOfString, lengthOfStringZ;
	char suffix;

	cout << "\nHow Many loops Would You Like Today?: ";
	cin >> numOfStrings;  //

	cout << "How Long The 1st String Should Be? (normal is: 2) ";
	cin >> lengthOfString;

	cout << "How Long the 2nd String Should Be ?";
	cin >> lengthOfStringZ;

	cout << "Add a suffix: ";
	cin >> suffix;

	for (int n = 0; n < numOfStrings; ++n) {
		string res = genrand(lengthOfString) + '/' + genrand(lengthOfStringZ) + '/' + suffix;
		cout << res << endl;
	}
}


This just displays the generate string for test purposes.
Thanks!

I also solved it using

1
2
3
4
5
6
7
8
9
10
11
12
13
for (int numStrings = 0; numStrings < numOfStrings; numStrings++)
        {
            for (int strLen = 0; strLen < lengthOfString; ++strLen)
            {
                Str += genRandom();
                }


            for (int strLen2 = 0; strLen2 < lengthOfStringZ; ++strLen2)
            {
                Str2 += genRandom2();
            }
Topic archived. No new replies allowed.