In: Computer Science
Language: c++
works in visual basic
Info:
theresa,allen,5-5-1989 marie,freeman,1-10-1985 benjamin,snyder,22-2-1999 peter,morrison,10-09-2001 karen,ruiz,04-11-1975 nancy,ryan,1-6-1941 lori,patterson,1-5-2005 louise,greene,10-12-2007 diane,day,6-4-1998 lillian,stewart,1-12-2002
Write a program that reads data from the csv file, prog4.txt and stores the data in an array. The program should read the data and verify that the first letter of the first and last name are capitalized and the date is in the correct format. The CSV file contains values separated by commas, the first element is the first name, the second element is the last name, and the last element is the date of birth in a European format (dd-mm-yyyy) Each row of the csv file contains the following elements • First name (all lower case) • Last name (all lower case) • Date of birth (dd-mm-yyyy) Requirements: 1. String Arrays a. Define a userData array that you can store each rows’ data into b. Use the ifstream object to read file data into the array c. Define a rowData array to store the separate elements of each row into (this will allow you to easily apply string manipulations to each element of the array) d. Use any additional variables as you see fit to accomplish the tasks outlined below. 2. Use a menu‐driven program with the following selections: 1. Read and show file data as is 2. Show file data cleansed 3. Exit the program 3. Define the following 3 functions a. int displayMenu(); Outputs the menu selections Inputs the users selection Validates that the user has entered a valid selection b. void showFileDate (string); Outputs the data in a tabular form directly from the file as is. This method takes an argument of file name. c. void showFileDateCleansed (string); Outputs the data in a tabular form with the first letter capitalized for the first and last name columns, also in this option the programmer must create an additional column labeled USA-Date (see the output file below), this column must use the EU-Date column, and convert the date from dd-mm-yyyy to the USA standard of mm-dd-yyyy. Ensure that if a day and or a month is less than 10, a leading zero is accounted for and added. For example if a data is 1-12-2016, the conversion should be 01-12-2016. If a date is 22-9- 2011, the conversion should be 22-09-2011 etc. This method takes an argument of file name.
Please note thtat for all the array declarations, we have used Dynamic Memory allocation using new operator.
Example:
string * arr = new string [1000];
We can also use static allocation like this:
string arr[1000];
Both methods works fine.
The problem is essentially based on these 2 things:
1. File handling
2. String manipulations
Please find all the explanation of code within the code itself as comments.
Here is the full code:
# include <iostream>
# include <iomanip>
# include <sstream>
# include <fstream>
# include <string>
using namespace std;
bool isValid (int option) {
if (option == 0 || option == 1 || option == 2) {
return true;
}
return false;
}
int displayMenu () {
int option = -1;
// Outputing the Menu Selections
cout << "\n\n";
cout << "Please select the action of your choice: \n";;
cout << "1. Read and Show file data as is present in the file\n";
cout << "2. Show file data in cleased format\n";
cout << "0. Exit\n";
// Taking the user Selection
cin >> option;
// checking if valid
if (isValid(option)) {
return option;
}
// if not valid, then
cout << "Selection chosen was INVALID.\n";
return displayMenu ();
}
void printRow (string first, string second, string date) {
char separate = '\t';
int width = 20;
cout << separate << left << setw(width) << first
<< separate << left << setw(width) << second
<< separate << left << setw(width) << date << "\n\n";
}
void printBiggerRow (string first, string second, string date, string usaDate) {
char separate = '\t';
int width = 20;
cout << separate << left << setw(width) << first
<< separate << left << setw(width) << second
<< separate << left << setw(width) << date
<< separate << left << setw(width) << usaDate << "\n\n";
}
int getUserData (string filename, string * userData) {
// ifstream is a class that is used to for reading a file in C++
// Create an ifstream object to read file data into the array
ifstream infile;
// a file needs to be opened, before we can read from it. We will use the open() function for this.
infile.open(filename);
// We will read line by line from the file
// and store each line separately in our array - userData
// this variable will be used as an index for our userData array
int lineNumber = 0;
// As a security measure, we can first check, if our file is open
if (infile.is_open ()) {
// each of our line will be store in this variable
string line;
// using getLine () function to read a line from our file
while (getline (infile, line)) {
// store the line into the userData array
userData [lineNumber] = line;
// increment the line number
lineNumber += 1;
}
}
return lineNumber;
}
void getRowData (string row, string * rowData) {
// this will store the set of characters after every split
string word;
// we need to split the string present in the argument string row, to get first name, last name and date
// for this, we will make use of stringstream
// put the contents of the argument string row into the stringstream object
stringstream s(row);
// index for rowData array
int j = 0;
// we will use the getline () function which will split our the stringstream with respect to its third argument
while (getline (s, word, ',')) {
rowData [j] = word;
j += 1;
}
}
// this function will return the USA-Date
string getUSADate (string EU_date) {
string usaDate;
// we will create a date array which will hold the values of : day, month and year in 0, 1 and 2 indices respectively
string * dateArray = new string [3];
// we will find them by splitting the EU_date which contains the EU-date
// we will use the stringstream object like before
stringstream t(EU_date);
// we will use the getline () function which will split characters in stringstream with respect to its third argument
// in our case, we want to split on the basis of ','
// this will store the set of characters (the string) after every split
string word;
// index for dateArray
int k = 0;
while (getline (t, word, '-')) {
dateArray [k] = word;
k += 1;
}
// checking month
// if size is 1, then, we need to add 0 to its front
if (dateArray[1].size() == 1) {
usaDate += '0';
}
usaDate += dateArray[1];
usaDate += "-";
// checking day
// if size is 1, then, we need to add 0 to its front
if (dateArray[0].size() == 1) {
usaDate += '0';
}
usaDate += dateArray[0];
usaDate += "-";
// append year
usaDate += dateArray [2];
return usaDate;
}
void showFileData (string filename) {
// Define a userData array that you can store each rows’ data into
// we are creating a fixed size array as the number of rows are not clear
// you may change the size according to your need
string * userData = new string [1000];
// get the records into the userData array
int records = getUserData (filename, userData);
// Now that our userData array has all the records from the CSV file
// we will now print the contents in a Tabular format
// leaving some lines for clean rendering
cout << "\n\n";
// printing the headings of each column
// As it wasn't stated in the problem statement, we are using a simple format with the names only
printRow ("FIRST NAME", "LAST NAME", "EU-DATE");
for (int i = 0; i < records; ++i) {
// Define a rowData array to store the separate elements of each row into
// as each row contains three type of data : first name, last name, date
// therefore, we will create the array with three rows
string * rowData = new string [3];
// fill the rowData array from the userData [i]
getRowData (userData[i], rowData);
// print the data as is in the tabular format
printRow (rowData[0], rowData[1], rowData[2]);
}
return;
}
void showFileDateCleansed (string filename) {
// Define a userData array that you can store each rows’ data into
// we are creating a fixed size array as the number of rows are not clear
// you may change the size according to your need
string * userData = new string [1000];
// get the records into the userData array
int records = getUserData (filename, userData);
// Now that our userData array has all the records from the CSV file
// we will now print the contents in a Tabular format
// leaving some lines for clean rendering
cout << "\n\n";
// printing the headings of each column
// As it wasn't stated in the problem statement, we are using a simple format with the names only
printBiggerRow ("FIRST NAME", "LAST NAME", "EU-DATE", "USA-DATE");
for (int i = 0; i < records; ++i) {
// Define a rowData array to store the separate elements of each row into
// as each row contains three type of data : first name, last name, date
// therefore, we will create the array with three rows
string * rowData = new string [3];
// fill the rowData array from the userData [i]
getRowData (userData[i], rowData);
// change the first letter of first name and last name to capitals
// We can change the letter to capital by
// 1. first subtracting 'a' from it, which make it '\0'
// 2. then adding 'A' to it, which makes it 'A'
rowData[0][0] += ('A' - 'a');
rowData[1][0] += ('A' - 'a');
// get the USA-Date
string usaDate = getUSADate (rowData[2]);
// print the data as is in the tabular format
printBiggerRow (rowData[0], rowData[1], rowData[2], usaDate);
}
return;
}
int main (void) {
// MENU DRIVE PROGRAM
// store the name of the file in a variable
string filename = "prog4.txt";
// this variable will be used to store the user's selection
int userSelection;
//
do {
userSelection = displayMenu();
switch (userSelection) {
case 1:
showFileData (filename);
break;
case 2:
showFileDateCleansed (filename);
break;
}
} while (userSelection != 0);
return 0;
}
Please refer to the screenshots of the code for better understanding of the indentation.
these functions are used to print the rows in a nice and clean format by making use of the C++ manipulators.
setw () function specifies the width that the string should take.
left makes sure that the string is printed to the right.
we have used separate and width to set the string. You may adjust these as you wish.
this function will read the data from the file and fill the userData array
This function is used to fill the rowData array.
This function is used to get the date in USA format.
For the input given in the problem statement, we will get following output:
For the user input = 1;
for user input = 2