-4

Hello i want to add for loop that will print my code for 20 times or 50 times .. but i really can't figure it out since i already add for function but it still give only 1 result :D

 #include <bits/stdc++.h> 
using namespace std; 

const int MAX = 36; 

// Returns a string of random alphabets of 
// length n. 
string printRandomString(int n) 
{ 
    char alphabet[MAX] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 
                        'H', 'I', 'J', 'K', 'L', 'M', 'N', 
                        'O', 'P', 'Q', 'R', 'S', 'T', 'U', 
                        'V', 'W', 'X', 'Y', 'Z', '0', '1', '2',
                        '3', '4', '5', '6', '7', '8', '9'}; 

    string res = ""; 
    for (int i = 0; i < n; i++) 
        res = res + alphabet[rand() % MAX]; 
    
    return res; 
} 

// Driver code 
int main() 
{ 
srand(time(NULL)); 
int n = 20; 
cout << printRandomString(n); 
return 0; 
} 

4 Answers4

2

Just run your function in a loop

int main() 
{ 
    srand(time(NULL)); 
    int n = 20; 
    for (int i = 0; i < 50; ++i)
        cout << printRandomString(n); 
    return 0; 
} 

Side note: #include <bits/stdc++.h> is bad, using namespace std; is bad and they are especially bad hen combined together.

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
2

You need to set a loop in main accordingly:

int len = 20 + ( rand() % ( 50 - 20 + 1 ) );
for(int i = 0; i< len; i++)
{
    int n = 20 + ( rand() % ( 50 - 20 + 1 ) );
    cout << printRandomString(n)<<endl;
}

Following is complete working code. See it working here:

#include <iostream>
using namespace std; 

const int MAX = 36;
const int PRINT_MIN = 20;
const int PRINT_MAX = 50;


// Returns a string of random alphabets of length n. 
string printRandomString(int n) 
{ 
    char alphabet[MAX] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 
                        'H', 'I', 'J', 'K', 'L', 'M', 'N', 
                        'O', 'P', 'Q', 'R', 'S', 'T', 'U', 
                        'V', 'W', 'X', 'Y', 'Z', '0', '1', '2',
                        '3', '4', '5', '6', '7', '8', '9'}; 

    string res = "";
    for (int i = 0; i < n; i++) 
        res = res + alphabet[rand() % MAX]; 
    
    return res; 
} 

// Driver code 
int main() 
{ 
    srand(time(NULL)); 
    int len = PRINT_MIN + ( rand() % ( PRINT_MAX - PRINT_MIN + 1 ) );
    for(int i = 0; i< len; i++)
    {
        int n = PRINT_MIN + ( rand() % ( PRINT_MAX - PRINT_MIN + 1 ) );
        cout << printRandomString(n)<<endl;
    } 
    return 0; 
} 
cse
  • 4,066
  • 2
  • 20
  • 37
1

Your Code as it is will only result in 1 string of 20 characters. if you want to print this string say m number of times, then you should change your main() function to the following:

int main() 
{ 

srand(time(NULL)); 
int n = 20,m=30; 
for(int i=0;i<m;i++)
{
   cout << printRandomString(n); 
}
return 0; 
} 
freeroamer90
  • 379
  • 3
  • 11
0

I think this is what you want to implement.

#include <iostream> 
using namespace std; 
const int MAX = 36; 
char printRandomString() 
{ 
    char alphabet[MAX] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 
                        'H', 'I', 'J', 'K', 'L', 'M', 'N', 
                        'O', 'P', 'Q', 'R', 'S', 'T', 'U', 
                        'V', 'W', 'X', 'Y', 'Z', '0', '1', '2',
                        '3', '4', '5', '6', '7', '8', '9'}; 
   return alphabet[rand() % MAX]; 
} 

// Driver code 
int main() 
{ 
int n;
scanf("%d",&n);
for(int i;i<n;i++){
printf("%c",printRandomString()); 
}
return 0; 
} 

and as @Yksisarvinen mentioned ,try not to use #include <bits/stdc++.h> .

Giriteja Bille
  • 168
  • 1
  • 13