C++ Random string

Discussion in 'Software' started by someguy85, Feb 26, 2011.

  1. someguy85

    someguy85 Private E-2

    Hi everyone. So, my c++ is a little rusty (as you will be able to tell from my coding, but I started playing with it again. I am trying to create a program that will fill an array with a specified number of randomly generated strings. Each string should have at least 4 words (because everything is random, the words or strings don't need to make any sense). I gave it a whirl and I just can't seem to get it to work (I can't even get it to compile without errors because my coding is so rusty). Can someone please look at my code and tell me if I'm going about this the right way. I am also open to suggestions to how other people would go about coding this.

    In my code I'm basically running nested for loops for a "randomly" generated amount of time to create the individual letters, words, and strings. Thanks for any help.




    Code:
    
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    int main()
    {
    	unsigned int seed = 0;
    	unsigned int letterSeed = 0;
    	int numStrings,
    		numWords,
    		numLetters;
    	char letter = 'K';
    	char stop = 'N';
    	cout << "How many strings would you like to randomly generate? ";
    	cin >> numStrings;
    	int length = (numStrings * 120) + 1;
    	char allStrings[length];
    	int iteration = -1;
    	int pos = 0;
    	
    	for(int i = 0; i < numStrings; i++)
    	{
    		unsigned secondSeed = 0;
    		seed = time(0);
    		numWords = srand(seed);
    		char aString[122]
    
    		if(numWords < 4)
    			numwords = 4;
    
    		if(numWords > 8)
    			numWords = 8;
    
    		for(int k = 0; k < numWords; k++)
    		{
    			int wordLength = 0;
    			secondSeed = time(0);
    			numLetters = srand(secondSeed);
    
    			if(numLetters > 15)
    				numLetters = 15;
    
    			wordLength = numLetters + 2;			
    			char word[wordLength];
    
    			for(int x = 0; x < numLetters; x++)
    			{
    
    				srand( (unsigned int) time(0) ) + x;
    				word[x] = (rand() % 26) + 'A';
    
    			}
    			word[wordLength + 1] = " ";
    			strcpy(aString, word);
    		}
    		iteration += 1;
    		pos = iteration * 122;
    		aString[121] = " ";
    		strcpy(allStrings[pos], aString);
    	}
    		
    
    
    
    	return 0;
    }
    
    
     
  2. PC-XT

    PC-XT Master Sergeant

    The first thing I noticed was that you were trying to get results from srand(seed), which is void. You can call it just once at the beginning of the program to set the sequence of pseudo-random numbers to return each time you call rand().

    This is a relatively common error when you haven't coded in a while. In case you want examples, here are some:
    http://www.cplusplus.com/forum/beginner/22286/
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds