This Programming project explores how the unnamed namespace

This Programming project explores how the unnamed namespace works. Listed are snipppests from a program to perform input validation for a username and password. The code to input and validate the username is in a sperate file than the code to input and validate the password.

//This needs to be in c++ for codeblocks, thanks for help.

File header user.cpp:

namespace Authenticate

{
void inputUsername( )

{

do

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

cin >> username;

} while(!isValid( ));

}

stromg 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 contains exactly eight letters. Generate an appropriate header file for this code.

//Repeat the same steps for the file password.cpp, placing the password variable and the isValid( ) in the unnamed namespace. In this case, the isValid ( ) function should return true if the input password has at least eight character including at least one non-letter:


File header password.cpp:

namespace Authenticate

{

void inputPassword( )

{

do

{

cout << \"Enter your passsword (at least 8 characters \" << \" and at least one non-letter)\" << endl;

cin >>> password;

}while(!isValid ( ));

}

string getPassword( )

{

return password;

}

}

//At this point, you should have two functions named isValid( ), each in different unnamed namespaces. Place the following main function in an appropriate place. The program should compile and run.

int main()

{

inputUserName( );

intputPassword( );

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

\" and your password is: \" <<

getPassword( ) << endl;

return 0;

}

//Test the program with several invalid usernames and passwords.

Solution

Please follow the code and comments for description :

CODE :

a) userNamePassword.h :


#ifndef USERNAME_PASSWORD_H // required header files for the data to run the code
#define USERNAME_PASSWORD_H

#include <iostream>
#include <string>

using std::cout; // required namespaces
using std::cin;
using std::endl;
using std::string;

namespace Authenticate // defining the namespace for the code
{
   void inputUserName( ); // variable and function declarations
   string getUserName( );
   void inputPassword( );
   string getPassword( );
}

#endif

b) username.cpp :

#include \"userNamePassword.h\" // getting the header file

namespace { // declaring local variable and function in unnamed namespace
   string username;

   bool isValid( ) { // boolean function to check if the data is valid or not
       if (username.length() != 8) {
           return false; // check for the username must be exactly 8 characters
       }      
       for (size_t i = 0; i < username.length( ); i++) { // iterate over hte length of the characters
           if (!isalpha( username.at(i) ) ) {
               return false; // only letters allowed
           }
       }
       return true;
   }
}

namespace Authenticate // using the namespace defined
{
   void inputUserName( ) // function to get the data from the user
   {
       do
       {
           cout << \"Please enter your username (8 letters only)\" << endl; // iterating till the valid data is entered
           cin >> username;
       } while (!isValid( ));
   }
   string getUserName( ) // get the user name data
   {
       return username;
   }
}


c) password.cpp :

#include \"userNamePassword.h\" // get the header files

namespace { // declaring local variable and function in unnamed namespace
   string password;

   bool isValid( ) {
       if (password.length( ) < 8)
       return false; // password requires at least 8 characters
       for (size_t i = 0; i < password.length( ); i++) {
           if (!isalpha( password.at(i) ) ) {
               return true; // found at least one non-letter
           }
       }
       return false; // didn\'t find any non-letters
   }
}

namespace Authenticate
{
   void inputPassword( )
   {
       do
       {
           cout << \"Please enter your password (at least 8 characters \" <<
           \"and at least one non-letter)\" << endl;
           cin >> password;
       } while (!isValid( ));
   }
   string getPassword( )
   {
       return password;
   }
}


d) main.cpp :

#include \"userNamePassword.h\" // get the file

using namespace Authenticate; // using the namespace defined

int main( ) // driver method
{
   inputUserName( ); // call the function
   inputPassword( ); // call the function
   cout << \"Your Username is \" << getUserName( ) << \" and your Password is : \" << getPassword( ) << endl; // print the result to console
   return 0;
}


OUTPUT :

Please enter your username (8 letters only) :
john
Please enter your username (8 letters only) :
carter
Please enter your username (8 letters only) :
johncarter
Please enter your username (8 letters only) :
johnny77
Please enter your username (8 letters only) :
johnnyKR
Please enter your password (at least 8 characters and at least one non-letter) :
high1234
Your Username is johnnyKR and your Password is: high1234


Hope this is helpful.

This Programming project explores how the unnamed namespace works. Listed are snipppests from a program to perform input validation for a username and password.
This Programming project explores how the unnamed namespace works. Listed are snipppests from a program to perform input validation for a username and password.
This Programming project explores how the unnamed namespace works. Listed are snipppests from a program to perform input validation for a username and password.
This Programming project explores how the unnamed namespace works. Listed are snipppests from a program to perform input validation for a username and password.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site