dcsimg
Results 1 to 7 of 7

Thread: Make a random letter display from string array[Resolved]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Posts
    281

    Resolved Make a random letter display from string array[Resolved]

    I am trying to better my understanding of using rand() and srand() while getting better at arrays and strings. This program is to take a string that the user put in and randomly show one letter. Then ask the user if he/she wants to play again to see what letter would pop up the next time. So far this is what I have. How would I go about randomizing an arrays element?

    Code:
    #include <iostream.h>
    #include <time.h>
    #include <stdlib.h>
    
    int randomLetter( void );
    
    int main()
    {
    	int answer;
    
    	randomLetter();
    
    	cout << "Try again? 1 for Yes, 0 for No. \n";
    	cin >> answer;
    
    	if (answer == 1)
    		randomLetter();
    	
    	return 0;
    }
    
    int randomLetter( void )
    {
    	const int arraySize = 10;
    	char string1[arraySize];
    	int selected;
    
    	srand( time(0));
    
    	cout << "Enter a string: ";
    	cin >> string1;
    	cout << "You entered " << string1 << "." << endl;
    
    	for ( int i = 0; string1[ i ]; i++ )
    		selected = string1 [ 1+ rand() % 9];
    
    	cout << "\n\nRandom character selected is: " << string1 [ i ] << endl;
    
    	return string1[i];
    }
    Last edited by KodeShark; Sep 22nd, 2004 at 05:44 PM.

  2. #2
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    There is probably a couple of different ways to do it. One way, though not perfect but easy to do, would be to loop a fixed number of times, say 100. On each loop pick 2 random numbers with in the array range. Make sure those numbers arent the same. Then swap the values in the array using the random number as the elements.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Posts
    281
    How would I go about coding that? Here is what I have so far. I've been working on it all night >.<

    Code:
    int randomLetter( void )
    {
    	const int arraySize = 10;
    	char string1[arraySize];
    
    	srand( time(0));
    
    	cout << "Enter a string: ";
    	cin >> string1;
    	cout << "You entered " << string1 << "." << endl;
    
    	for ( int i = 0; string1[ i ]; i++ )
    		string1 [ 1 + rand() % 9]; // I tried randomizing here but it doesnt seem to work.
    
    	cout << "\n\nRandom character selected is: " << string1 [ i ] << endl;
    
    	return string1[i];
    }

  4. #4
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Here is your code but fixed up.
    Code:
    #include <iostream>      //<--- No .h
    #include <time.h>
    #include <stdlib.h>
    
    using namespace std;   //Added Namespace
    
    int randomLetter( void );
    
    int main()
    {
    	int answer;
    
    	randomLetter();
    
    	cout << "Try again? 1 for Yes, 0 for No. \n";
    	cin >> answer;
    
    	if (answer == 1)
    		randomLetter();
    	
    	return 0;
    }
    
    int randomLetter( void )
    {
    	const int arraySize = 10;
    	char string1[arraySize];
    	int selected;
    
    	srand(time(NULL));		//NULL not 0
    
    	cout << "Enter a string: ";
    	cin >> string1;
    	cout << "You entered " << string1 << "." << endl;
    
    	selected = (rand() % 9) ;		//Get A Random #er
    
    	cout << "\n\nRandom character selected is: " << string1 [ selected] << endl;
    
    	return string1 [ selected];
    }

    Here is a better way if you are not forced into using chars.
    Code:
    #include <iostream>      //<--- No .h
    #include <time.h>
    #include <stdlib.h>
    #include <string>		//Added String
    
    using namespace std;   //Added Namespace
    
    int randomLetter( void );
    
    int main()
    {
    	int answer = 1;
    
    	while (answer != 0)		//Added
    	{
    		randomLetter();
    
    		cout << "Try again? 1 for Yes, 0 for No. \n";
    		cin >> answer;
    	}
    	
    	return 0;
    }
    
    int randomLetter( void )
    {
    	const int arraySize = 10;
    	string string1;
    	int selected;
    
    	srand(time(NULL));		//NULL not 0
    
    	cout << "Enter a string: ";
    	cin >> string1;
    	cout << "You entered " << string1 << "." << endl;
    
    	selected = (rand() % string1.size());		//Get A Random #er
    
    	cout << "\n\nRandom character selected is: " << string1 [ selected] << endl;
    
    	return string1 [ selected];
    }
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Posts
    281
    Ah. So I was on the right track at least~ ^^

    Thank you for your help. I'll try it tomorrow morning. The weather has caused me to be sick -_-

    edit:
    It works fine but I get something similar to this quite often.

    IF

    Note: They are more close together and is obviously an error. It looks like a korean symbol..
    Last edited by KodeShark; Sep 15th, 2004 at 09:15 AM.

  6. #6
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    What you were getting was probably junk in the buffer. You were either going out of scope of the array or you were not filling the array correctly.

    Did you understand everything ok?
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Posts
    281
    yes thank you very much. i will look at the scopes more carefully =) Thank you again. I have a little difficulty stepping out of the book and applying what I have learned.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width