Create function from char string input.

I have written code which allows the user to input a password and user name and then the user will input it again and compare the 2. I want to make this into a function and call it from main() but i am unsure how to do this. Any help would be appreciated. Everything works in the code, but i am unable to figure out how to make it a function that can be called. thanks.

#include <iostream>
#include < string.h>
#include <string>

void main()
{
char username[256];
char password[256];

std::cout << "Username: ";
std::cin >> username;

std::cout << "Password: ";
std::cin >> password;

bool length = false;

while ( length == false)
{
if (strlen (password) < 8)
{

std::cout << "Please input correct amount of numbers" << std::endl;
std::cout << "Password: ";
std::cin >> password;
}

else
{
length = true;
}
}


bool upper = false;



while(upper = false)
{
for(int i = 0; i < strlen (password); i++)
{
if(isupper(password[i]))
{
upper = true;
}

}

if (upper == false)
{
std::cout << "Please input at least 1 upper character." << std::endl;
std::cout << "Password: ";
std::cin >> password;
}

}


bool lower = false;

while( lower == false)
{
for(int i = 0; i < strlen (password); i++)
{
if(islower(password[i]))
{
lower = true;
}

}

if (lower == false)
{
std::cout << "Please input at least 1 lower character." << std::endl;
std::cout << "Password: ";
std::cin >> password;
}

}

system("cls");
std::cout << "your Username and Password is: " << std::endl << std::endl;
std::cout << "Username: " << username << std::endl;
std::cout << "Password: " << password << std::endl << std::endl;
system("pause");
system("cls");

char LoginUsername[256];
char LoginPassword[256];


std::cout << "please enter your username and Password to log in: " << std::endl << std::endl;

std::cout << "Username: ";
std::cin >> LoginUsername;

std::cout << "Password: ";
std::cin >> LoginPassword;


bool correct = true;

while( correct == true)
{
if ((strcmp(LoginUsername, username) ==0) && (strcmp(LoginPassword, password) ==0))
{
correct = false;
}

else
{

std::cout << "Please Enter again." << std::endl;;
std::cout << "Username: ";
std::cin >> LoginUsername;

std::cout << "Password: ";
std::cin >> LoginPassword;
}
}

}



That should probably be broken up into a series of function calls.
Here are some function declarations to give you an idea of how to break them up.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/Verifies that the passed in char string conforms to User Name requirements
//return true if the char string conforms, false if it doesn't
bool verifyUserNameFormat(const string& userName);
//Verifies that the passed in char string conforms to Password requirements
//return true if the char string conforms, false if it doesn't
bool verifyPasswordFormat(const string& password);

//Prompts for input to create a new user
//returns true if user account is successfully created
//userName and password will contain the new username and password on successful creation
//returns false if user account creation fails
//userName and password will be empty if account creation fails
bool newUserPrompt(string& userName, string& password);

//Verify userName and password are correct
//returns true if correct
//returns false if incorrect
bool loginVerify(const string& userName, const string& password);

//Prompt for user login
bool loginPrompt();

I recommend using STL strings instead of char arrays.
newUserPrompt() will call verifyUserNameFormat() and verifyPasswordFormt().
loginPrompt will call loginVerify().
Topic archived. No new replies allowed.