inputted letters to asterisk

Ok so my friend bet me that I couldn't write a user login program so I am trying to but I am having trouble with getting a certain line to change if the user wants to change his/her password here is my code:

#include <iostream>
#include <string>
using namespace std;
int main() {
string a;
string b;
string a1 = "Jacob";
string b1 = "Password";
string pass;
unsigned short t = 0;
char p;

while (t < 3) {
cout << "Username\n";
cout << "/";
cin >> a;
cout << "Password\n";
cin >> b;

if (a == a1 && b == b1) {
t = 0;
cout << "Welcome, would you like to change your password? [y/n]\n";
cin.ignore(1000, '\n');
cin >> p;
break;
} else {
cout << "YOU ARE A FAIL ~c==3\n" << "TRY AGAIN\n";
++t;
cin.ignore(1000, '\n');
}
}
if (t == 3){
cout << "EXITING PRESS ENTER";
cin.get();
exit(0);
}
if (p == 'y') {
cout << "Cannot change password\n"; //This is what it says for now want
//it to change b1 to what user wants
} else if (p == 'n') {
cout << "Thank you, goodbye\n";
}

system("PAUSE");
return 0;
}
Last edited on
already tested and runs fine
closed account (zb0S216C)
When you test for 'y', you said the user cannot change their password. Why? Why give them the option if you don't want them to change their password?

Here's your code in the working condition:

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <string>

using namespace std;

int main() 
{ 
	string a = ""; 
	string b = "";
	string a1 = "Jacob";
	string b1 = "Password";
	string pass = "";
	unsigned short t = 0;
	char p = '\0';

	while (t < 3) 
	{
		cout << "Username\n";
		cout << "/";
		cin >> a; 
		cout << "Password\n";
		cin >> b; 

		if (a == a1 && b == b1) 
		{
			t = 0; 
			cout << "Welcome, would you like to change your password? [y/n]\n";
			cin.sync( );
			p = cin.get( );
			break; 
		} 

		else 
		{
			cout << "YOU ARE A FAIL ~c==3\n" << "TRY AGAIN\n";
			++t;
			cin.ignore(1000, '\n'); 
		}
	}

	if (t == 3)
	{
		cout << "EXITING PRESS ENTER";
		cin.get();
		exit(0);
	} 

	if (p == 'y') 
	{
		cout << "New password: ";
		cin >> b1;
		cout << "Current Username: " << a1.c_str( ) << endl;
		cout << "Current Password: " << b1.c_str( ) << endl;
	}

	else if (p == 'n') 
	{
		cout << "Thank you, goodbye\n";
	}

	cin.sync( );
	cin.get( );
	return 0;
}


Also, don't call system( ).

Wazzak
Last edited on
Although it is possible to do what you want, it is very difficult. It involves modifying the executable. The simpler solution would be to store the password in a separate file.
kev82 could you show me how to do that including checking if the inputed password matches the predefined password in the separate file
thanks ill check it out
hmm that doesn't tell me how to check if text in file matches with inputted password do you know how to do that?
Read the password in the file and the user input into strings. Then you can compare them with the comparison operator ==

Reading strings from a file is covered in the referenced tutorial, I believe.
Last edited on
Ok I see. Now there is one last question do you guys know how to make the input asterisks (*) instead of the actual letters?
Here is current working code:


#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {

string a;
string b;
string a1 = "Jacob";
string pass;
unsigned short t = 0;
char p;

while (t < 3) {

cout << "Username\n" << "/";
cin >> a;
cout << "Password\n" << "/";
cin >> b;

ifstream password ("password.txt");
getline (password,pass);
password.close();

if (a == a1 && b == pass) {
t = 0;
cout << "Welcome, would you like to change your password? [y/n]\n";
cin.ignore(1000, '\n');
cin >> p;
break;
} else {
cout << "YOU ARE A FAIL ~c==3\n" << "TRY AGAIN\n";
++t;
cin.ignore(1000, '\n');
}
}
if (t == 3){
cout << "EXITING PRESS ENTER";
cin.get();
exit(0);
}
if (p == 'y') {
fstream password ("password.txt");
cout << "New password: ";
cin >> pass;
password << pass;
cout << "\nPassword changed";
password.close();
} else if (p == 'n') {
cout << "Thank you, goodbye\n";
}
cin.ignore(1000, '\n');
cin.get();
return 0;
}

I already created the file "password.txt" and made it a hidden file so there was no need to create it in the program
Last edited on
I think that this is a function you can perform on a simple while loop. Or you can call a separate function when the user types in a letter into the console.

This is really helpful if you are designing a system that would require passwords. Though, I had some problem with it when the program was taking the backspace as a letter and not used to erase the already typed in letter. I guess a regular expression would do the trick.
Ok I see. Now there is one last question do you guys know how to make the input asterisks (*) instead of the actual letters?


You can't, not with standard console I/O. You'll have to go into something OS specific.
Well I am using Windows 7 do you know how to do it for that OS?
Found this, I think it's what you want:

http://www.cplusplus.com/forum/general/3570/#msg15410
Great! Thanks you guys so much you really helped me out! : )
Topic archived. No new replies allowed.