Question: I have c++ code where it generates a random password and asks the user to guess the password. However, when the user guesses the letters,
I have c++ code where it generates a random password and asks the user to guess the password. However, when the user guesses the letters, my code only is in order. I want, for instance, is; if the random password is HELLO, and if I insert O as a guess, I should get the correct guess.
#include
#include
#include
using namespace std;
int main()
{
// DON'T CHANGE THESE FIRST 2 LINES, which are used to get the random numbers loaded
int time = clock(); // Stores the computer's time in the variable time
srand(time); // Primes the pump for the random number generator
// const string UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWZ"
char alphabet[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int length;
string password = "",guess;
cout << "How long is the password you want to create: ";
cin >> length;
if (length != 0)
{
cout << "Your desired length is:" << length << endl;
}
for(int i = 0; i < length; i++)
{
password += alphabet[rand() % 26];
}
cout< //Printed because it is hard to guess as the code doesn't pick a meaningful password //It picks random letters cout<<" Guesses Screen "; while(true) { int c=0; cin>>guess; for(int i = 0; i < length; i++) { if(guess[i]==password[i]) { c=c+1; cout< } else cout<<"_ "; } cout<<" "< if(c==length)//if all the letters are correct you break { cout<<" YOU WIN!!"; break; } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
