Problem Solving with C++ (10th Edition)
Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
bartleby

Videos

Textbook Question
Book Icon
Chapter 12, Problem 5P

This Practice Program explores how the unnamed namespace works. Listed below 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 file separate from the code to input and validate the password.

File 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 nonletter:

File password.cpp:

namespace Authenticate

{

  void inputPassword()

  {

  do    {

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

  <<

  “and at least one nonletter)” << 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.

Blurred answer
Students have asked these similar questions
The function fopen return NULL when: An error occurred while opening the file. An error occurred while reading the file. An error occurred while closing the file. An error occurred while compiling the file.
Line Numbers Write a program that asks the user for the name of a file. The program should display the contents of the file with each line preceded with a line number followed by a colon. The line numbering should start at 1. Python
in C# Code in VS studio: Create a program that has 2 options for a user to select. The first option accepts information from the user and then save that data to a file on disk. The second option loads all data stored on that file.

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Files & File Systems: Crash Course Computer Science #20; Author: CrashCourse;https://www.youtube.com/watch?v=KN8YgJnShPM;License: Standard YouTube License, CC-BY
UNIX Programming (Part - 10) The File System (Directories and Files Names); Author: ITUTEES;https://www.youtube.com/watch?v=K35faWBhzrw;License: Standard Youtube License