I'm having the hardest time figuring out why my password check loop won't exit after excepting valid input. Can someone help?

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main( )
{
    
	vector<string> credentials;
	string login;
        string userName;
	string password;
	string CheckPassword (string password);

		cout << "Enter user name or enter 0 to stop." << endl;
		getline(cin, userName);
		
		while  (userName != "0")
			{
				CheckPassword(password);
			}
	
		password = CheckPassword (password);
		login = userName + password;
		credentials.push_back(login);
		for (unsigned int i = 0; i < credentials.size( ); i++)
		cout << credentials[i] <<" ";
		cout << endl;
			
	return 0;	
}

string CheckPassword (string password)
{
	do
	{
        cout << "Enter a valid password." << endl;
		getline(cin, password);
    }
	while (password.length ( ) <= 4);
	cout << "Password accepted." << endl;
	return password;
}

If you don't re-check AND re-set the value of userName inside the while, it will be infinite.

while  (userName != "0")
{
   // <-Needs to ask for userName HERE
   CheckPassword(password);
}

Thanks for the quick response thines01! i know what you're saying but i dont understand why it's necessary or how to implement it. I revised the code some. I decided to make username request a function as well. however i still can't get it to terminate. i don't understand as the build replicates the password check which does terminate.

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main( )
{
    
	vector<string> credentials;
	string login;
    string userName;
	string password;
	string GetUsername (string userName);
	string CheckPassword (string password);

		
		
	
	    userName = GetUsername (userName);
		password = CheckPassword (password);
		login = userName + "," + " " + password;
		credentials.push_back(login);
		for (unsigned int i = 0; i < credentials.size( ); i++)
		cout << "Your username and password is: " << credentials[i] <<" ";
		cout << endl;
			
	return 0;	
}

string GetUsername (string userName)
{
	do
	{
		cout << "Enter user name or enter 0 to stop." << endl;
		getline(cin, userName);
	}
	while (userName != "0");
	cout << "User name " << userName << " accepted." << endl;
	return userName;
}

string CheckPassword (string password)
{
	do
	{
        cout << "Enter a valid password." << endl;
		getline(cin, password);
    }
	while (password.length ( ) < 5);
	cout << "Password accepted." << endl;
	return password;
}

If you don't re-check AND re-set the value of userName inside the while, it will be infinite.

while  (userName != "0")
{
   // <-Needs to ask for userName HERE
   CheckPassword(password);
}

furthermore, it's completely ignoring the "while username != 0" statement....:(

Thanks for the quick response thines01! i know what you're saying but i dont understand why it's necessary or how to implement it. I revised the code some. I decided to make username request a function as well. however i still can't get it to terminate. i don't understand as the build replicates the password check which does terminate.

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main( )
{
    
	vector<string> credentials;
	string login;
    string userName;
	string password;
	string GetUsername (string userName);
	string CheckPassword (string password);

		
		
	
	    userName = GetUsername (userName);
		password = CheckPassword (password);
		login = userName + "," + " " + password;
		credentials.push_back(login);
		for (unsigned int i = 0; i < credentials.size( ); i++)
		cout << "Your username and password is: " << credentials[i] <<" ";
		cout << endl;
			
	return 0;	
}

string GetUsername (string userName)
{
	do
	{
		cout << "Enter user name or enter 0 to stop." << endl;
		getline(cin, userName);
	}
	while (userName != "0");
	cout << "User name " << userName << " accepted." << endl;
	return userName;
}

string CheckPassword (string password)
{
	do
	{
        cout << "Enter a valid password." << endl;
		getline(cin, password);
    }
	while (password.length ( ) < 5);
	cout << "Password accepted." << endl;
	return password;
}
Member Avatar for r.stiltskin

Think again.

"Enter username or enter 0 to stop."

OK - you enter wmurrow.

Now check: does wmurrow equal "0"?
No. OK, enter the loop again:

"Enter username or enter 0 to stop."

and so on...

This will continue until you surrender and enter 0, exactly as your program says.

Thank you for your input. i realize now this a logic error and not a programing one. the thing is, how do i properly lay it out so that the loop terminates when i put in the sentinel value instead of terminating when i put in 0 and taken that input and putting it into the string? i just cant seem to get past this....

Think again.

"Enter username or enter 0 to stop."

OK - you enter wmurrow.

Now check: does wmurrow equal "0"?
No. OK, enter the loop again:

"Enter username or enter 0 to stop."

and so on...

This will continue until you surrender and enter 0, exactly as your program says.

Member Avatar for r.stiltskin

You'll have to be clear about what you are trying to accomplish. Are you doing any kind of editing or validity-checking on the username? It doesn't seem so. So why is getting the username in a loop? Apparently you're willing to accept any string as username, and then if the username is "0" you want to "stop", but stop what?

psuedocode is as follows:

prompt for username
input desired username
prompt for password (must be >= 5 characters)
input desired password
if password < 5 characters, prompt for password again
prompt for another username (if no more usernames are desired, type 0 to stop prompts)
take all usernames and passwords stored in vector and print them to the screen.

does that help?

You'll have to be clear about what you are trying to accomplish. Are you doing any kind of editing or validity-checking on the username? It doesn't seem so. So why is getting the username in a loop? Apparently you're willing to accept any string as username, and then if the username is "0" you want to "stop", but stop what?

oh and last but not least, I don't want 0 (the sentinel value) to be stored in the vector.

psuedocode is as follows:

prompt for username
input desired username
prompt for password (must be >= 5 characters)
input desired password
if password < 5 characters, prompt for password again
prompt for another username (if no more usernames are desired, type 0 to stop prompts)
take all usernames and passwords stored in vector and print them to the screen.

does that help?

Ok...so writing out the psuedo code helped and I was finally able to work out the logic piece. Thank you very much for help on that. I still have the sentinel value of 0 still being read into the vector problem though. I also discoered that the vector is only storing the last username and password entered. Below is an updated build. Any thoughts?

//willmurrow password.cpp
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main( )
{
    
	vector<string> credentials;
	string login;
    string userName;
	string password;
	string GetUsername (string userName);
	string CheckPassword (string password);

		
		
		cout << "Enter user name or enter 0 to stop." << endl;
		getline(cin, userName);
		while(userName != "0")
		{
		password = CheckPassword (password);
		userName = GetUsername (userName);
		login = userName + "," + " " + password;
		}
		credentials.push_back(login);
		for (unsigned int i = 0; i < credentials.size( ); i++)
		cout << "Your username and password is: " << credentials[i] <<" ";
		cout << endl;
		
	return 0;	
}

string GetUsername (string userName)
{
	cout << "Enter user name or enter 0 to stop." << endl;
	getline(cin, userName);
	cout << "User name " << userName << " accepted." << endl;
	return userName;
}

string CheckPassword (string password)
{
	do
	{
        cout << "Enter a valid password." << endl;
		getline(cin, password);
    }
	while (password.length ( ) < 5);
	cout << "Password accepted." << endl;
	return password;
}

oh and last but not least, I don't want 0 (the sentinel value) to be stored in the vector.

Member Avatar for r.stiltskin

So look at your code again, and find the line where you have a valid username and password. Immediately following that line is where you should be forming the login and pushing it into the vector.

It should be obvious to you that this should happen inside the loop, so it will be done for every login, not just the last one.

Finally figured it out! Thanks for all your help stiltskin!

// Author:			Will Murrow
// Source file:		willmurrowpassword.cpp
// Description:		Login Generator
// Compiler used:	Visual C++ 2008 Express Edition

#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;

int main( )
{
    
	vector<string> credentials;
	string login;
    string userName;
	string password;
	string CheckPassword (string password);
		
		cout << setw (20) << "Login Generator" << endl << endl;
		cout << setw (35) << "Enter user name (enter 0 to stop)." << endl << endl;
		getline(cin, userName);
		while(userName != "0")
		{
		password = CheckPassword (password);
		login = userName + "," + " " + password;
		credentials.push_back(login);
		cout << setw (35) << "Enter user name (enter 0 to stop)." << endl << endl;
		getline(cin, userName);
		}
		for (unsigned int i = 0; i < credentials.size( ); i++)
		cout << endl << setw (33) << "Your username and password are: " << credentials[i] << endl << endl;
		cout << endl;		
	return 0;	
}

string CheckPassword (string password)
{
	do
	{
        cout << endl;
		cout << setw (49) << "Enter a valid password (must have 5 characters)." << endl << endl;
		getline(cin, password);
    }
	while (password.length ( ) < 5);
	cout << endl;
	cout << setw (19) << "Password accepted." << endl << endl;
	return password;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.