0

What I'm trying to accomplish is for the program to ask the user for their username and password and display it to the screen like this "John, abc123" after the function passwordCheck has confirmed that the password is not less then 5 characters. If the password is less then 5 characters long it keeps asking until its satisfied. The entire program keeps looping until the user enters 0 for username. Here is what I have.

Attachments
//Compiler used:	Visual Studio C++ Express Edition

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

string passwordCheck();

int main( )
{
	vector<string> v;
	string username;
	string password;
	string login;
	string temp = "0";

	cout << " \n Enter username(0 to exit).\n";
	getline(cin, username);

	while(username != temp)
    {

		login = username + ", " + password;
		cout << " Enter password.\n";
		getline(cin, password);
		password = passwordCheck();

        v.push_back(login);

        getline(cin, username);
	}	

	
	cout << login << endl;
	
	for (unsigned int i = 0; i < v.size(); i++)
		cout << v[i] << ", ";
	    cout << endl;

    return 0;
}

string passwordCheck()
{
	
	string password;
	int length = password.length();

	do
	{
		getline(cin, password);
	}

	while(length < 5);

	return password;
}
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by close_encounter
0

Wow..... I would suggest you start by deciding where you want your inputs to occur. You have the password input occurring in 2 conflicting locations. You also have multiple occurrences of input for username. However, this is okay, the locations you have them in are complementary rather than conflicting.

Depending on what the assignment's instructions are, you may want to consider changing the return of passwordCheck to bool instead. You would then check the length of the string and return true or false depending on the results. You would then use this bool return value to control a validation loop in main().

0

Teacher wants me to change to a string function and use only one return with no bool. how to?

// function to check whether or not a password is valid
// has no spaces, doesn't use all digits
// is greater then 4 characters in length

bool CheckPassword(string password)
{
     bool valid=false;
     if(password.length()<5)
     return false;
     else
     for(int i = 0; i < password.length(); i++)
     if ((password[i]  >='A') && (password[i] <= 'Z') || (password[i]  >='a') && (password[i] <= 'z'))
     valid=true;
     for(int i = 0; i < password.length(); i++)
     if(password[i]==' ')
     return false;
     
     if(valid)
     return true;
     else
     return false;
 }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.