Thread: Any help on making random char code?

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    118

    Any help on making random char code?

    I need to create a random code that uses "abcde" and that does not repeat any letters, but I have no idea how to do that, I know how to make random numbers, but not chars, any help?
    This is what I need them for:
    Code:
    #include <iostream>
    #include <cstring>
    #include <ctime>
    using namespace std;
    
    int main()
    {
    
    	char code[6] = "abcde";  // here I want the ranom code
    	char icode[6];
    	char letter;
    	char opcion;
    
    	bool playagain = false;
    
    	int i = 0;
    	int correct = 0;
    
    	do
    	{
    		cout << "\t\t*RULES*\n\n";
    		cout << "1.- You do NOT speak about Fight Club.... lol.\n";
    		cout << "2.- You do NOT write in CAPS.\n";
    		cout << "3.- You do NOT write more than 5 lettrrs per guess.\n\n\n";
    		cout << "Try to guess a code I have thought of:\n";
    		do
    		{
    			icode[5] = '\0';
    
    			for (i = 0; i < 5; i++)
    			{
    				cin >> letter;
    
    				icode[i] = letter;
    
    				if (icode[i] == code[i])
    				{
    					correct++;
    				}
    			}
    			cout << "You had " << correct << " correct letters!\n";
    
    			if (!strcmp(code, icode))
    			{
    				cout << "You guessed the code!\n";
    			}
    			correct = 0;
    
    		} while (strcmp(code, icode));
    
    		cout << "Do you want to play again? (y/n)\n";
    		cin >> opcion;
    		playagain = (opcion == 'y' ? true : false);
    
    	} while (playagain);
    
    	return 0;
    }
    Why drink and drive when you can smoke and fly?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Random characters are the same as random numbers, you just have to specify a starting number:

    A - Z : 65 - 90
    a - z : 97 - 122

    Example:
    Random letter A, B or C

    ((rand() % 3) + 65)

    Random letter d, e, f, g or h

    ((rand() % 5) + 97 + 3)


    The problem will be not to repeat any letters. You could generate a letter, check if it is already picked (storing the picked one in an array) and if it already exists, pick a new one.

    Another solution would be to place all available letters in an array, then do random swaps several times to get them in a random order (generate two random indices, then swap the letters at those two positions in the array).
    Last edited by Magos; 02-02-2003 at 12:14 PM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Game Code Messed Up (Mainly struct{};)
    By CodeNinja in forum C++ Programming
    Replies: 11
    Last Post: 07-19-2004, 08:08 AM
  4. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM