To Write data in a spectic format

Hello guys am working on a c++ project that search for Student id in the source file,if a match is found
it writes to the output file
after the search those were the found
1
2
John    Bill   M  1895865 20013647  2001 78 66 80 66 79 75 
Frank   Libmen M  6546727 20016367  2001 50 78 56 43 33 57 


My code
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;
int main()
{
 struct person
{
  string firstname;
  string surname;
  string gender;
  int    id;
};

system("cls");
  ifstream input;
  ifstream source;
  ofstream output;
  ofstream notfound;
  input.open("input.txt");
  source.open("source.txt");
  output.open("Output Processed file.txt");
  notfound.open("Not Found.txt");
  if(!input)
      {
      cout << "Error!" << endl;
      cout << "Unable to access the input.txt file required to run this process..." << endl;
      cout << "********************************************************************" << endl;
      cout << "The Process Was not Sucessful!! enter 0 Return to Previous Menu" << endl;
      }
if(source.fail())
    {
     cout << "Error!" << endl;
     cout << "Unable to access the source.txt file required to run this process" << endl;
     cout << "Its either it was Deleted or placed in a location inaccessable for this Program" << endl;
     cout << "Please check and re-run the process" << endl;
     cout << "" << endl;
     cout << "********************************************************************" << endl;
     cout << "The Process Was not Sucessful!! enter 0 Return to Previous Menu" << endl;

    }

  // foreach line of input....
  string input_line;
  while( getline(input,input_line) )
    {
    stringstream i_line(input_line);
    person input_person;
    i_line >> input_person.firstname
           >> input_person.surname
           >> input_person.gender
           >> input_person.id;
    // search the source for a matching name
    source.clear();
    source.seekg(0);
    bool found = false;
    string source_line;
    while ( !found && getline(source,source_line) )
      {
      stringstream s_line(source_line);
      person source_person;
      s_line >> source_person.firstname
             >> source_person.surname
             >> source_person.gender
             >> source_person.id;
      // found them!
      if ( source_person.id == input_person.id)
            {
            found = true;
            }
      }
      // and output...
      if ( !found )
            {
            notfound << "No NRC match for:" << source_line << endl;
            }
      else
            {
            cout << "NRC Match Found for:" << source_line<<  endl;
            output << source_line<<  endl;
            }
    }
            input.close();
            source.close();
            output.close();
            notfound.close();
            cout << "" << endl;
            cout << "********************************************************************" << endl;
            cout << "The Process Was Sucessful!! enter 0 Return to Previous Menu" << endl;


}


output file expected format
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
First Name: John
Last  Name: Bill
Gender: M
Student ID: 1895865
Course Code: 20013647
Year: 2001

Mathematics   78
English       66 
Science       80
Geography     66
Music         79
Art           75   

First Name: Frank
Last  Name: Libmen
Gender: M
Student ID: 6546727 
Course Code:20016367
Year: 2001

Mathematics   50 
English       78 
Science       56 
Geography     43 
Music         33
Art           57  

I need help editing the code...
Last edited on
there are 2 simple ways to proceed:

1) extend person struct to hold the additional fields, parse them out (line 54ish) and then add something to print them to the file, more or less intact. Its crude, but it should work.

2) a bit of a do-over but here, if you parsed the records into your struct *as you read them* and put them all into a nice vector<person> everyone; it would be a fair amount of work but a lot cleaner than reading the file each time and such. If you have not yet covered vectors/arrays, this would not be helpful advice.

output to text files looks exactly like cout. So you can get it working with cout then replace cout with file and it may be easier to debug etc.
Last edited on
Hello Chewe Kennedy,

I take it that we have to guess at what the 2 input files look like?

Not having the correct information I am not sure if what I did is working the program correctly.

Andy
Hello Chewe Kennedy,

In your code:
1
2
3
4
5
6
7
8
9
if (!found)
{
    notfound << "No NRC match for:" << source_line << endl;
}
else
{
    cout << "NRC Match Found for:" << source_line << endl;
    output << source_line << endl;
}

In line 3 are you sure that "source_line" is the correct variable? It did not work for me, but I could have the files "input.txt" and "source.txt" wrong. Although based on those names I could be misunderstanding what each file is for.

As mentioned there are several ways that you could do the output in the else part. I tried this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
else
{
    cout << "NRC Match Found for: " << source_line << endl;
    //output << source_line << endl;

    output
        << std::setw(14) << "First Name : " << input_person.firstname << '\n'
        << std::setw(14) << "Last Name : " << input_person.surname << '\n'
        << std::setw(14) << "Gender : " << input_person.gender << '\n'
        << std::setw(14) << "Student ID : " << input_person.id << '\n'
        << std::setw(14) << "Course Code : " << "20013647  This line never read from input" << '\n'
        << std::setw(14) << "Year : " << "2001  This line never read from input" << '\n'
        << "\n The rest of what you need was never read from the file\n\n";

    found = false;
}


This produced:

 First Name : John
  Last Name : Bill
     Gender : M
 Student ID : 1895865
Course Code : 20013647  This line never read from input
       Year : 2001  This line never read from input

 The rest of what you need was never read from the file

 First Name : Frank
  Last Name : Libmen
     Gender : M
 Student ID : 6546727
Course Code : 20013647  This line never read from input
       Year : 2001  This line never read from input

 The rest of what you need was never read from the file


If you are not familiar with "std::setw()" you can leave that out and just add spaces to the beginning of each line if you want the (:)s to line up.

Andy
Thanks Andy...........i will research more on std::setw() it might work..........
Topic archived. No new replies allowed.