Hi,

I am trying to do a pretty basic project for my computer science class. The part that is giving me trouble is where the user needs to input every letter of the alphabet in a random order but only each letter can be inputted once. I was wondering if someone could take a loop at my two loops that I thought would solve this problem. Any help is appreciated...

Thanks

void enter_key()
{
char key[25];
int k,j;

cout << "Please input a key to be used.  Make sure to enter every letter in the alphabet only once." << endl;

	for (k=0;k<=25;k++)
	{
		cin >> key[k];
		for (j=0;j<=25;j++)
		{
			if (key[k] == key[j])
			{
				cout << "Try a different letter: ";
				cin >> key[k];
			}
		}
	}

	for (k=0;k<=25;k++)
	{
		cout << key[k] << " ";
	}
return;
}

First you need to initialize the array to some known value -- normally 0 -- to prevent it containing random values. Here is the easiest and most common way to do that char key[25] = {0}; Is your program going to be case-insensitive? That is, 'A' is treated the same as 'a'? If so then you will want to convert whatever you type to either upper or lower case letters before inserting the letter into the array and before attempting to search the array for the letter.

You do not want to add the key into the array right away -- use a different variable for the data entry. Then search the array for that value, if not found then insert it at the next available slot then increment that counter. If found then you can display the error message. The loop you have coded will fail when it comes across the kth element.

There are other ways to search for the letter -- strrchr() will return the index value if the letter is found. Example: assume index is an integer that counts the number of letters inserted into the array.

if( strchr(keys,'a') )
{
   // letter 'a' is already in the array
}
else
{
   keys[index++] = 'a';
}

Thanks for the reply.. I think i understand what you are saying.. That was a good point about the lower or uppercase. I will worry about that when I can get the main part working. I tried fixing the loop like you said but now it never says the letter is being repeated.. i think the problem im having is i need it to check through all the values in the array for that one letter.. If you can take another look that'd be great.. thanks again

for (k=0;k<=5;k++)
	{
		cin >> test_repeat;
		for (j=0;j<=5;j++)
		{
			if (test_repeat != key[j])
			{
				key[k] = test_repeat;
			}
			if (test_repeat == key[j])
			{
				"Please enter another letter. ";
			}
		}
	}

The j loop need only go as far as the current value of k - no need to test past the point of where you've successfully stored data.

The j loop doesn't work well as a for loop - you will continue testing and assigning even once you've found that test_repeat is a new value.

Try this, I think it will correctly do what your problem asks:

bool found;
for (k=0;k<=5;k++)
{
    cin >> test_repeat;
    j = 0;
    found = false;
    while ( j <= k && !found )
    {
        if (test_repeat == key[j])
        {
           cout << "Enter another number";
           cin >> test_repeat;
           j = 0;
        }
        else if( j == k )
        {
           found = true;
           key[k] = test_repeat;
        }
        j++;
    }
}

It's a bit more complex than your for loop approach, but the problem is actually not so simple. When you find a match to existing data, you have to get a new input and restart the search at the beginning. Only if you get all the way through the existing data without finding a match, do you place the new data item in the array.
Val

Thanks for the help. I was able to use what you did.. It makes sense actually.. We never really worked with boolean much so I hadnt thought of that route. I did need to change the && to || because I need it to do it while one or the other. Just letting you know incase you come across something like that again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.