Reputation: 275
What I want to do:
Generate a random sequence of letters with only the letters a,b,c,d in it. Let the user enter a sequence as a guess, output the position at which he has a correct letter, print the number of correct letters at wrong positions, repeat until the user guesses the sequence correctly OR after 10 wrong tries.
My attempt:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main ()
{
char lettres[4] = {'a','b','c','d'};
char rString[5];
int i=0;
srand(time(0));
while(i<5)
{
int temp = rand() % 4;
rString[i] = lettres[temp]; //while loop to generate the random sequence and put it in rString
i++;
}
int u=0;
char t[5];
while(u<10)
{
cout << "Enter your guess ";
cin >> t[5]; // take guess attempt as an array, each letter at a different index
int k;
int compteur=0;
int t2[5];
int compteur2=0;
for(k=0; k<5; k++)
{
if(rString[k]==t[k])
{
k = t2[compteur2]; //if letter is at correct position put its index in a new array
compteur2++;
}
else
{
for(int j=0; j<5; j++)
{
if(t[k]==rString[j])
{
compteur++; //if not check if there is correct letters and count them
}
}
}
}
cout << compteur2 << " " << compteur;
if(compteur2==5)
{
cout<<"bravo";
i = 10; // if guessed sequence is correct set i=10 to exit
}
else
{
cout<< "Positions with correct letter: " << t2[5];
cout <<"You have a total of"<< compteur << "correct letters";
}
u++;
}
return 1;
}
Current output:
Enter your guess: abcd //obviously I entered abcd
0 0Positions with correct letter: 32766You have a total of0 correct letters
//execution ends
Update:
I did 2 changes, one of which was suggested.
First, I changed the way I put the guessed sequence into an array and it's now the following : cin >> t;
instead of cin >> t[5];
Second I realized I am putting the integers into t2[5]
by writing k=t2[compteur2]
which is wrong as far as I know, I changed it to t2[compteur2]=k
And I starter printing my array using a loop.
Now the program isn't terminating instantly but I am getting the following after I enter a sequence:
1 2Positions with correct letter: 1,0,0,0,-420947304,You have a total of5 correct letters
Even though I have a mediocre programming experience, I just started learning c++ so I am not very proficient with the syntax and such.
Upvotes: 0
Views: 71
Reputation: 310
Use a std::string
to collect the guess from the user, because using a fixed size char array can lead to undefined behavior.
One solution could be to not proceed unless you get the correct sized guess string (my assumption is that you're looking for a guess of a fixed size).
while(u<10) {
string guess;
cout << "Enter your guess ";
cin >> guess; // take guess attempt as an array, each letter at a different index
if (guess.size() != 5) {
continue;
}
/* Process the guess */
u++;
}
Upvotes: 0
Reputation: 409482
The problem most likely is the off-by-one out-of-bounds writing that happens with
cin >> t[5];
This will read one single character from cin
and write it to the sixth element of the five-element array t
. This leads to undefined behavior.
What probably happens in reality is that the next variable in memory, which is most likely u
(being defined next to t
), will be overwritten by that value you read.
If you want to read a string you need to pass a pointer to the first element of the array, &t[0]
. You can simplify this by learning that arrays naturally decay to pointers to their first element, which means that t
will be equal to &t[0]
. So you can simply write
cin >> t;
Note that there's no bounds checking for C-style strings. If the user inputs a string longer than four characters, you will still have the same problem as the extra character and the null-terminator will be written out of bounds of the array. The solution to this is the std::string
class:
string t; // A dynamic string which will be extended as needed
...
cin >> t;
Upvotes: 1