I have this Code written but I am having problems with the While loop I need it to recognize if the password has less then 5 characters and if there are spaces. I just cannot get it working for the spaces. Can anyone help??

#include <iostream>
#include <vector>
#include <string>
using namespace std;
string CheckPassword(string pass);

int main( )
{

	string login;
	string userName, password;
	vector<string> v;
	
	cout << "Enter your Username or 0 to stop:\n";
	getline(cin,userName);

	while ( userName != "0" )
	{

		password = CheckPassword(password);
		
		
		login = userName + ", " + password;

        v.push_back(login);

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


     }

	if (userName == "0")
	{

    cout << "The logins are:\n";
    for (unsigned int i = 0; i < v.size( ); i++)
        cout << v[i] << " " << "\n";
    cout << endl;
	}

	system ("pause");
    return 0;
}

string CheckPassword(string pass)
{
	bool first = true;
	

	do
	{
		if(!first) 
		{
			cout << "Password must be greater than 5 characters and contain no spaces:\n";
	    }
		
		cout << "Enter your Password:\n";
	    getline(cin,pass);
		first = false;
	}
	while (pass.length() <= 5 || pass.find(' '));
	
		
		return pass;
	
}

Recommended Answers

All 6 Replies

find() returns string::npos if not found, which is not the same thing as 0 or NULL.

Is there anything that works with strings to search for a space the way I have it set up now?

None other than the find() method. The while loop just needs a little adjusting to make it work correctly. use and && operator instead of or || because you want to check for less than 5 AND no spaces.

I tried and it still is not working can you show me what I might be doing wrong???

There is no reason for the parameter to the function. Also, the || operator is correct.

string CheckPassword()
{
    string pass;
	bool first = true;

	do
	{
		if(!first) 
		{
			cout << "Password must be greater than 5 characters and contain no spaces:\n";
	    }
		
		cout << "Enter your Password:\n";
	    getline(cin,pass);
		first = false;
	}
	while (pass.length() <= 5 || pass.find(' ') != string::npos);
	
		
		return pass;
	
}

Thanks for the help!!

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.