I am trying to write a helper program for Wordle which will display all of the letters which have not been eliminated and the letters which have been confirmed. I've broken the program into functions, and I'm having an issue with the confirmed_letters()
function.
I have this array declared in the global scope:
char* word[NUM_LETTERS];
and it's initialized like this:
for (int i = 0; i < NUM_LETTERS; i++) word[i] = new char(char(42));
Here's the function:
void confirmed_letters() {
int num_let = 0; // the # of confirmed letters
int temp_num; // holds the position of the confirmed letter
char temp_letter;
std::cout << "\n\nHow many letters have you confirmed?\n" <<
"Enter only the letters whose positions are known. ";
std::cin >> num_let;
std::cin.ignore(1000, '\n');
if (num_let == 0) return;
std::cout << "Enter the integer position of the confirmed letter, " <<
"followed by the letter. Press the enter (return) key between each entry.\n";
for (int i = 0; i < num_let; i++) {
//temp_num = -1; // I don't think this is needed
std::cin >> temp_num;
std::cin.ignore(1000, '\n');
if (temp_num > 5 || temp_num < 1) {
std::cout << "Invalid letter position. Enter an integer between 1 and 5.\n";
i--;
goto end;
}
std::cin >> temp_letter;
std::cin.ignore(1000, '\n');
word[temp_num - 1] = &temp_letter;
end:
display_word();
}
return;
}
display_word()
is just a function to display the word.
Here's the output I got:
How many letters have you confirmed?
Enter only the letters whose positions are known. 3
Enter the integer position of the confirmed letter, followed by the letter. Press the enter (return) key between each entry.
1
o
o * * * *
2
i
i i * * *
3
e
e e e * *
So, for some reason, each additional letter is modifying the previous letters. I don't understand what is going on here. Each time the value of one of the elements of word[]
is modified, it should only modify one element, but it's modifying all of them. Thoughts?