Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your task is to write a C++ program that will open the file: customers.txt , loo

ID: 3816399 • Letter: Y

Question

Your task is to write a C++ program that will open the file: customers.txt, loop through the file and create user names for each customer and write these user names to a new file called: usernames.txt.

You will notice that the customers.txt file has the following structure

<first_name> <last_name> <date_account_created>

An example of this is: Mickey Mouse 3/1/2016

Given the example data from above the corresponding user name will be: MIMO2016

This username was created by

Taking the first two characters of the first name and converting them to uppercase

Taking the first two characters of the last name and converting them to uppercase

Taking the year from the date

Concatenating all of these together

MI + MO + 2016

Program structure:

Create a function called create_username() that accepts a single string that will contain a line of text from the file (Example: “Mickey Mouse 3/1/2016”) and returns a stringrepresenting the new username. This should be accomplished with a value parameter.

Helpful Hints:

Use an EOF (end of file) loop to process the the lines of the file. Page 284 in text

Read in the entire line with the getline. Page 156 in text

Use the string method find to locate the spaces in the lines

Use the string method substr to extract the relevant parts of the line

Use the string method length to determine how long a string is. This will be helpful when extracting the year from the end of the line

Use the toupper function from the <cctype> library to accomplish the uppercase conversion. Page 348 in text.

Use the string method append to build your new string from the parts of the line

In all of your programs, you must remember the following:

Include documentation at the beginning of your program.

Include your first and last name as the first line of all of your program outputs.

Use appropriate, meaningful identifier names for all variables in your program.

Declare constant variables for any “numeric constants” in your code.

Explanation / Answer

/*
a C++ program that will open the file: customers.txt, loop through the file and create user names for each customer and write these user names to a new file called: usernames.txt.
*/

#include <fstream>
#include <string>
#include <cctype>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
int create_username(std::string username) {
   ofstream outfile("D://username.txt",std::ios_base::app);     
outfile << username<<endl;
outfile.close();
   return 0;
}

int main()
{
ifstream infile("D://customers.txt");
string strLine;
   string username;
   while (!infile.eof())   
{
       getline(infile, strLine);
   std::istringstream iss(strLine);
   string f_name, l_name,year;//taking first name , second name ,date in variable
   if (!(iss >>f_name >>l_name >>year)) { break; } // error
   else {
       username=f_name.substr(0,2) ;
       username+=l_name.substr(0,2) ;
   username+=year.substr(year.size() -4) ;//getting year from date
   std::transform(username.begin(), username.end(),username.begin(), ::toupper);
   create_username(username);
  
   }
}
   return 0;
}