PassWord program

Hey guys, I got a little help earlier with my CheckPassword Function.
Everything works exactly correct, just as it should.
However, when i get into the third vector, for some reason it takes part of the previous vector and prints it backwards, which is not correct.
If someone could help me fix this as im not very familiar with vectors.
Thanks for all help,

PS: I suggest copying and pasting the code.

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  #include <iostream>
#include <vector>
#include <string>
#include <conio.h>
#include <ctype.h>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;

string CheckPassword();

int main( ){
	string userName;
	string passWord;
	string login;
	vector<string> v;

	cout << " Enter your Username (0 to stop): ";
	getline(cin,userName);

	while (userName != "0"){
		passWord = CheckPassword();

		login = userName + ", " + passWord;

		v.push_back(login);
		cout << " Username and Password (" << login << ") added to Vector.\n";

		cout << " Vector size is now: " << v.size( ) << endl;
		cout << " Vector List: \n" << endl;
		for (int i = 0; i < v.size(); i++)
			cout << v[i] << endl;

		cout << endl << " Enter your Username (0 to stop): ";
		getline(cin,userName);
	}
	system("pause");
    return 0;
}

string CheckPassword(){
	string pass = ""; // initialize to null/empty string
	char ch = ' ';
	bool flag;
		   do {
			   int i = 0;
			   flag = true;
			   
			   pass.clear();				// reset the input stream

			   cout << " Enter your Password: "; 
			   ch = _getch();

			   while (ch != 13){
				   pass += ch;
				   cin.putback(ch);
				   cout << '*';
				   ch = _getch();
			   }

			   cout << endl;
			   i = 0; //resets the i integer to be used again.

			   while (i < pass.length() && flag){
				   if (pass.at(i) == ' '){
					   cerr << "Password cannot contain spaces." << endl;
					   flag = false;
				   }
				   i++;
			   }
			   if (pass.length() < 5){
				   cerr << "Password too short." << endl;
				   flag = false;
			   }
			  cout << endl;
		   } while (flag == false); 
            cin.ignore();			// remove any remaing characters from the stream
	return pass;
}
I run the code, everything seems correct except the part you need a cin.ignore() to wait for username input after the first time.
1
2
cin.putback(ch); // I commented this one out as well as this:
cin.ignore();

They seemed to be excesive measures - first one causes unwanted changes to login name, the other paused needlessly (hit enter twice issue)
http://stackoverflow.com/questions/13556890/why-do-i-have-to-press-enter-twice

Also, please consider user backspacing while entering password - it gets saved ok, but Asterixes are too many.
Last edited on
Jock, enter "black" as the password and the error will be recreated. It takes some characters from the password and puts them in the username.
Jock, never mind. you fixed it! thanks for the help. :)
Topic archived. No new replies allowed.