Last 4 Digits of SSN

Hey Guys, I need MAJOR help. I'm a C++ noob and I need to find out the code to read the last 4 digits of an SSN from a file. Thank so much.
What it is "SSN" ????
Social Security Number (SSN)
Last edited on
So give an example of what format this SSN have, and what is the content of the file (it contains only one SSN or more).
I have no format, that's why I am asking. The file can contain one SSN just to make it easy. The SSN is 10 numbers without any dashes or whitespaces. For example: 1467439678.
Last edited on
So that is simple. Open the requested files. Read in everything. Store the data from (10 - 4)th character.

Give me a guideline and I'll help you taking the right way.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <fstream>
#include <string>

using namespace std;

int main () {

string fname;
cout << "Enter file name :" << endl;
cin >> fname;

ifstream ifs;
if (!ifs.open (fname.c_str(), ios::binary)) {
cout << "failed to open the file !" << endl;
return 1;
}
char buf [11] = {0};
ifs.read (buf, 10);
string result;
result += buf[6];
result += buf[7];
result += buf[8];
result += buf[9];
cout << result << endl;
return 0;
}


I have not tested this example.
Last edited on
I tweaked up modoran's example up a bit.
tweaked. thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
	string fname;
cout << "Enter file name :" << endl;
cin >> fname;

ifstream ifs;
int i;
string result;
char buf [11] = {0};
ifs.open(fname.c_str(), ios::binary);
if(ifs.read(buf,10))
{
	result += buf[6];
	result += buf[7];
	result += buf[8];
	result += buf[9];
	cout << "Last Four Are: ";
	cout << result << endl;
	cin >> i;
	return 1;
}
else
{
	cout << "Failed to read file";
	cin >> i;
	return 0;
}
Last edited on
Ok I found a solution to my problem that, I think, may help you....

File input will be named "Input.txt" and will contain the SSN "1252369874"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main()

{ // Entry Point

// File Input
     ifstream fin;
     fin.open("Input.txt");
     if (!fin.good()) throw "I/O ERROR";

     // File Input Info
     int SSN;
     fin >> SSN;
     SSN = SSN % 10000;  // Changes value of INT to the number of '0' you input
                         // For example you have 999 in your file and you input 100... The output will be 99
// File Output
     ofstream fout;
     fout.open("Report.txt");
     if (!fout.good()) throw "I/O ERROR";  // By doing this skips the monitor output and goes directly to File Output

     // File Output Info
     fout << setfill('*') << setw(10) << SSN << endl;  // The setfill and setw just makes it look nice and readable     

     return 0;

} // End Point 


The File Output is "******9874"

I have tried it and it worked. Thanks Modoran for the code, but it was to long.

If you have multiple data in a File Input like:

5412369874
6541239875

DO THIS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main()

{ // Entry Point
// File Input
     ifstream fin;
     fin.open("Input.txt");
     if (!fin.good()) throw "I/O ERROR";

     // File Input Info
     int SSN;

// File Output
     ofstream fout;
     fout.open("Repot.txt");
     if (!fout.good()) throw "I/O ERROR";

     while(!fin.eof())
     { // DO NOT put DECLARATION STATEMENTS in WHILE loop
          // File Input Info
          fin >> SSN

          // Show Last 4 Digits of SSN
          SSN = SSN % 10000;

          // File Output Info
          fout << setfill('*') << setw(10) << SSN <<endl;

     } // While

     return 0;

} // End Point 

NOTE: NEVER put Declaration Statements in a WHILE loop

Hope this will help you become fast and lazy programmers ^^
Topic archived. No new replies allowed.