Guess the password Game

I'm currently writing a guess the password game but I'm stuck in the output part. For example if the password is 12345, which it will shown as "*****", then if I guess 13465, it will show 1***5. I have 5 chance to choose it. Here's my codes:

#include <iostream>
#include <string>
#include <conio.h>
#include <sstream>

using namespace std;
int main(){
int guess,pass1,pass2,pass3,pass4,pass5,guess1,guess2,guess3,guess4,guess5;
string x="";
char ch;
cout << "Please enter 5 numbered password: "<<endl;
ch = _getch();
while(ch != 13){//character 13 is enter
x.push_back(ch);
cout << '*';
ch = _getch();
}
stringstream geek(x);
int pass = 0;
geek >> pass;

for (int counter = 4; counter >=0; counter=counter - 1)
{
cout<<"\nPlease guess the password: ";
cin>>guess;
guess5=guess%10;
guess4=(guess%100)/10;
guess3=(guess%1000)/100;
guess2=(guess%10000)/1000;
guess1=(guess%100000)/10000;

pass5=pass%10;
pass4=(pass%100)/10;
pass3=(pass%1000)/100;
pass2=(pass%10000)/1000;
pass1=(pass%100000)/10000;

if (pass1 == guess1 && pass2 == guess2 && pass3 == guess3 && pass4 == guess4 && pass5 == guess5)
{
cout << "\nCongratulations. You have break the code.\n";
return (0);
}
else if (pass1 == guess1 && pass2 == guess2){
cout<< "\nWrong Password. You have "<<counter<<" tries left. Your match is: "<<guess1<<guess2<<"***";
}
else {
cout<< "\nWrong Password. You have "<<counter<<" tries left.";
}
}
}
<conio.h> has been deprecated long time ago and you really don’t need it.

One possible basic logic can be:
Create your password as a std::string (ex. by reading the user input by means of std::cin or std::getline()). Let’s call it ‘x’, if you want (I think it’s a confusing name).

Create another std::string of the same length of the previous, but filled with asterisks. Let’s call it ‘to_out’.

--> In a loop:
Ask the user a guess. Record the answer in a third std::string, say ‘attempt’. Verify if the user has input exactly as many characters as there’re in ‘x’.

Compare ‘x’ and ‘attempt’ character by character. For every char which matches, change the corresponding char in ‘to_out’ from asterisk to the correct one.

Display ‘to_out’.

Check for winning condition.
<-- end loop
Topic archived. No new replies allowed.