Listed below are snippets from a prgram to perform input validation for
a username and password. The code to input and validate the username is
in a seperate file than the code to input and validate the password.

Partial code from user.cpp:

namespace Authenticate
{
void inputUserName(){

do
{
cout << "Enter your username (8 letters only)" << endl;
cin >> username;
}
while(!isvalid());

}
string getUserName()
{
return username;
}
}

Define the username variable and the isValid() function in the
unnamed namespace so the code will compile. The isValid() function
should return true if username contians exactly eight letters.
Generate an appropriate header file for the code.
Repeat rhe same steps for the file password.cpp, placing the password
variable and the isValid() function in the unnamed namespace:

namespace Athenticate
{
void inputPassword(){

do
{
cout << "Enter your password (at least 8 characters " <<
"and at leat one non-letter)" << endl;
cin >> password;
}
while(!isValid());
}

string getPassword()
{
return password;
}
}
For passwords, isValid() should require the passwrod to have eight or
more letters and at leat one non-letter, Generate an appropriate header
file for this code as well.
At this pont, you should have two functions named isValid(), each in
different namesapcs. Place the following main function in an appopriate
place. The program should comile and run.

int main()
{
intputUserName();
inputPassword();
cout << "Your username is " << getUserName() <<
"and your password is: " <<
getPassword() << endl;
return 0;
}

Respuesta :

Answer:

#include <iostream>

#include <string>

#include "user.h"

#include "password.h"

using namespace Authenticate;

using namespace std;

int main()

{

inputUserName();

inputPassword();

cout << "Your username is " << getUserName() <<

" and your password is: " <<

getPassword() << endl;

return 0;

}

user.h:

#ifndef USER_H

#define USER_H

#include <string>

using namespace std;

namespace Authenticate

{

namespace

{

bool isvalid();

}

void inputUserName();

string getUserName();

}

#endif

user.cpp:

#include <iostream>

#include "user.h"

namespace Authenticate

{

string username="";

namespace

{

bool isvalid()

{

if(username.length() == 8)

return true;

else

return false;

}

}

void inputUserName(){

do

{

cout << "Enter your username (8 letters only)" << endl;

cin >> username;

}

while(!isvalid());

}

string getUserName()

{

return username;

}

}

password.h:

#ifndef PASSWORD_h

#define PASSWORD_h

#include <string>

using namespace std;

namespace Authenticate

{

namespace

{

bool isValid();

}

void inputPassword();

string getPassword();

}

#endif

password.cpp:

#include <iostream>

#include <string>

using namespace std;

namespace Authenticate

{

string password="";

namespace

{

bool isValid()

{

if(password.length() >= 8)

{

for(int i=0; i<password.length(); i++)

if(password[i] >= '0' && password[i] <= '9')

return true;

return false;

}

else

return false;

}

}

void inputPassword(){

do

{

cout << "Enter your password (at least 8 characters " <<

"and at leat one non-letter)" << endl;

cin >> password;

}

while(!isValid());

}

string getPassword()

{

return password;

}

}