开发者

How would i manipulate an imported .text file to show only the last four numbers os an employee's SNN on the screen?

This program retrieves a file from a loction on the computer and prints it to the screen only showing the last four digits of the employee's SSN.

#include <iostream>
#include <fstream>
#include <cstdlib>   // needed for exit()
#include <string>
using namespace std;

int main()
{
    double Thanks_for_your_time;
    string filename = "C:\\Emp\\employee_info.txt";
    string line;

  ifstream inFile;

  inFile.open("C:\\Emp\\employee_info.txt");  // open the file with the  
                              // external name 
  if (inFile.fail())  // check for a successful open
  {
    cout << "\nThe file was not successfully opened"
        << "\n Please check that the file currently exists." 
         << endl;
    exit(1);
  }

  cout << "\nThe file has been successfully opened for reading\n"
       << endl;

  while (getline(inFile,line))
      cout << line << endl;



  // statements to read data from the file would be placed here
  do
{
   cout << "\nThanks for your time(0 to quit):";
   cin >> Thanks_for_your_time;

   }
   /*

The file has been successfully opened for reading

Employee Name: Harry Heck Employee SSN: 987-98-7987 (Everything but the last four need to be "x"'s or blank) Employee Hourly Pay: $20.15 Hours Worked T开发者_如何学编程his Week: 40.25 Gross Pay: $811.04

Employee Name: Sally Smothers Employee SSN: 654-65-4654 (Everything but the last four need to be "x"'s or blank) Employee Hourly Pay: $50.25 Hours Worked This Week: 40.35 Gross Pay: $2027.59

Thanks for your time(0 to quit): */


Use regex from the standard library.

#include <regex>
using namespace std::tr1;

I haven't played with C++ in many years, but it would be something like this (assuming you stored your string in the variable 'str'):

std::tr1::regex rx("[0-9]..-..-");
std::string replacement = "***-**-";
std::string str2 = std::tr1::regex_search(str, rx, replacement);

The above code was referenced from this site, and you can test your regular expressions with this fantastic tool. I am fairly certain that you want regex_search as opposed to regex_replace due to the slightly different way that C++ handles matches, but again, I haven't used C++ in some time, so I can't say for sure.

Note that "[0-9]..-..-" is a regex that will match any numeric character followed by two characters of any type (. is a wildcard), then a -, then two more characters of any type, then another -. Thus, in your text, it will only match the first two segments of the two SSNs. You are then replacing the numbers in that matched pattern with asterisks.

Also, being as this is homework, I'd like to give you some additional resources, with the first being language-specific:

http://softwareramblings.com/2008/07/regular-expressions-in-c.html

http://www.regular-expressions.info/reference.html

http://www.zytrax.com/tech/web/regex.htm

Also, in the future, you will probably get more useful answers if you follow the community guidelines for asking homework questions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜