SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Separate Compilation And Namespaces
Exercise:
Programming Projects
Question:5 | ISBN:9780132846813 | Edition: 5

Question

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

File header 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 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( ) function in the unnamed namespace. In this case, the isValid( ) function should return true if the input password has at least eight characters including at least one non-letter:

File header password.cpp:

namespace Authenticate

{

void inputPassword( )

{

do

{

cout << "Enter your password (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( );

inputPassword( );

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

" and your password is: " <<

getPassword( ) << endl;

return 0;

}

Test the program with several invalid usernames and passwords.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Sorry the answer is not available at the moment…

If you are able to find the answer, please make sure to post it here. So that your Juniors have smile on their lips and feel happy.

Spread the 'tradition of sharing'.