| | |
Need help checking for repetition in array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Solved Threads: 0
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
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
c++ Syntax (Toggle Plain Text)
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; }
Last edited by Ancient Dragon; Dec 11th, 2007 at 11:00 pm. Reason: add code tags
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
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.
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.
C++ Syntax (Toggle Plain Text)
if( strchr(keys,'a') ) { // letter 'a' is already in the array } else { keys[index++] = 'a'; }
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.
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Solved Threads: 0
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
C++ Syntax (Toggle Plain Text)
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. "; } } }
Last edited by bpage; Dec 12th, 2007 at 12:40 am.
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:
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
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:
C++ Syntax (Toggle Plain Text)
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++; } }
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Solved Threads: 0
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
![]() |
Other Threads in the C++ Forum
- Previous Thread: from FORTRAN to C++
- Next Thread: help
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






