Need help checking for repetition in array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 3
Reputation: bpage is an unknown quantity at this point 
Solved Threads: 0
bpage bpage is offline Offline
Newbie Poster

Need help checking for repetition in array

 
0
  #1
Dec 11th, 2007
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
  1. void enter_key()
  2. {
  3. char key[25];
  4. int k,j;
  5.  
  6. cout << "Please input a key to be used. Make sure to enter every letter in the alphabet only once." << endl;
  7.  
  8. for (k=0;k<=25;k++)
  9. {
  10. cin >> key[k];
  11. for (j=0;j<=25;j++)
  12. {
  13. if (key[k] == key[j])
  14. {
  15. cout << "Try a different letter: ";
  16. cin >> key[k];
  17. }
  18. }
  19. }
  20.  
  21. for (k=0;k<=25;k++)
  22. {
  23. cout << key[k] << " ";
  24. }
  25. return;
  26. }
Last edited by Ancient Dragon; Dec 11th, 2007 at 11:00 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Need help checking for repetition in array

 
0
  #2
Dec 11th, 2007
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.
  1. if( strchr(keys,'a') )
  2. {
  3. // letter 'a' is already in the array
  4. }
  5. else
  6. {
  7. keys[index++] = 'a';
  8. }
Last edited by Ancient Dragon; Dec 11th, 2007 at 11:09 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3
Reputation: bpage is an unknown quantity at this point 
Solved Threads: 0
bpage bpage is offline Offline
Newbie Poster

Re: Need help checking for repetition in array

 
0
  #3
Dec 12th, 2007
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


  1. for (k=0;k<=5;k++)
  2. {
  3. cin >> test_repeat;
  4. for (j=0;j<=5;j++)
  5. {
  6. if (test_repeat != key[j])
  7. {
  8. key[k] = test_repeat;
  9. }
  10. if (test_repeat == key[j])
  11. {
  12. "Please enter another letter. ";
  13. }
  14. }
  15. }
Last edited by bpage; Dec 12th, 2007 at 12:40 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Need help checking for repetition in array

 
0
  #4
Dec 12th, 2007
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:
  1. bool found;
  2. for (k=0;k<=5;k++)
  3. {
  4. cin >> test_repeat;
  5. j = 0;
  6. found = false;
  7. while ( j <= k && !found )
  8. {
  9. if (test_repeat == key[j])
  10. {
  11. cout << "Enter another number";
  12. cin >> test_repeat;
  13. j = 0;
  14. }
  15. else if( j == k )
  16. {
  17. found = true;
  18. key[k] = test_repeat;
  19. }
  20. j++;
  21. }
  22. }
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
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3
Reputation: bpage is an unknown quantity at this point 
Solved Threads: 0
bpage bpage is offline Offline
Newbie Poster

Re: Need help checking for repetition in array

 
0
  #5
Dec 12th, 2007
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC