creating a password in C++

need help with a password checker I'm making
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "stdafx.h"
#include <iomanip>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;


int main()
{
	// variables
	string password = "";
	string guess = "";
	string attempt = "";

	cout << "Enter a password using any characters from [a-z, A-Z, !, @, #, $, %, &]";
	cout << endl << "Password: ";
	getline(cin, password);

	system("cls");

	while (guess != password)
	{
		cout << "What is the password you entered?: ";
		getline(cin, guess);

		cout << endl;

		cout << "Password: ";
		for (int i = 0; i < password.length(); i++)
		{
			guess.substr(0,i) = password;
			attempt = password;
			cout << attempt;
		}
		cout << endl << "You entered: " << guess << endl;

		if (guess.length() != password.length())
		{
			cout << "Password length does not match!" << endl;
		}

		cout << endl;
	}
	cout << "You guessed the right password!" << endl;
	
    return 0;
}

only trouble I'm having is with the substr function. I am trying to compare each letter of the guess to the original password and have no idea what I'm doing with that for loop
Last edited on
just index the string.

if(password.length() == guess.length())
for(x = 0; x < password.length(); x++)
{
if(password[x] == guess[x])
something
else
somethingelse
}
else//guess length is wrong, comparison is pointless

if this is a true password function, you can just try
if(password == guess) //does it for you!!

if this is a game where you tell the user they got X out of Y correct, you need the above loop to figure out what they got right and wrong.


substr is sub-string, it is unsuitable for extracting single letters. It can do it, but it returns a string which is unfriendly for the task at hand.

Last edited on
Topic archived. No new replies allowed.