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
Consider a file named transaction.txt which contains unknown number of lines. Each line contains data of one transaction as follows: username (string), amount (float), and membership number (int). Write a C++ program that reads the data from the file, then displays on screen the number of users, the total amount, the average of amounts, the lowest amount and the highest amount. Reading from the file should stop when one or more of the conditions given below becomes true:(a) The end of file has been reached. (b) The “user999” is reached.
Consider a file named transaction.txt which contains unknown number of lines. Each line contains data of one transaction as follows: username (string), amount (float), and membership number (int). Write a C++ program that reads the data from the file, then displays on screen the number of users, the total amount, the average of amounts, the lowest amount and the highest amount. Reading from the file should stop when one or more of the conditions given below becomes true:(a) The end of file has been reached. (b) The “user999” is reached.transaction.txtHassan 12.5 12345 Mariam 5.2 87542 Zahra 33.1 93568 :::: ::Sample Screen OutputNumber of users: 54 Transactions total amount: 2658.7 The average of amounts: 120 The highest amount: 280 The lowest amount: 5.2
This is Python Program 5: Improve the file copy program from Lab 9 so that it will make sure the user enters a file that exists. If a file doesn’t exist, the open function will raise the FileNotFoundError. You do not need to check if the destination exists since a file doesn’t need to exist to write it. Instead of copying the content of the source as exact, it only copies non-empty lines and it also prepend each line with a line number. A line is empty if the length of the line stripped of white space from both end is 0. The code segment below shows how to strip white space and getting the length of the string. If the length of the line is zero, the line is a blank line. line1 = " " line2 = " aa " print(len(line1.strip())) # 0 print(len(line2.strip())) # 2 In the sample run, “add.py” is the source file and “add-copy.py” is the destination file. Note that “add-copy.py” contains only non-blank lines from “add.py” and each line in “add-copy.py” is labelled with a line number. Sample run:…
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
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
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