I am trying to complete a task, but I have to do it without using string. My goal is to read a text file, find lines containing the same CD name, but different actions (A or B) and then write the output in a different file. For example:
Input file:
10 B 01010112345 ABBA
15 A 02020213456 U2
16 A 03030314567 4you
17 B 02020213456 ABBA
20 B 04040415678 4you
127 B 04040415678 HipHop2014
1234 A 05050516789 ABBA
Output file:
20 03030314567 04040415678
1234 05050516789 01010112345
This is my code:
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
const int SIZE = 250;
struct CD {
int time;
char action;
// char person_code[12];
// char cd[50];
string person_code;
string cd;
bool removed = false;
};
int main(){
fstream f("exchange.in");
ofstream fout("exchange.out");
CD m[SIZE];
CD x;
int it = 0, counter = 0;
while(f>>x.time>>x.action>>x.person_code>>x.cd && it<SIZE){
cout<<"Time: "<<x.time<<" Action: "<<x.action<<" Person code: "<<x.person_code<<" CD: "<<x.cd<<endl;
m[it].time = x.time;
m[it].action = x.action;
// memcpy(&m[it].person_code, &x.person_code, sizeof(CD));
// memcpy(&m[it].cd, &x.cd, sizeof(CD));
m[it].person_code = x.person_code;
m[it].cd = x.cd;
it++;
}
for(int i=0; i<it; i++){
for(int j=0; j<it; j++){
if(!m[i].removed && !m[j].removed && m[i].cd == m[j].cd && m[i].action == 'A' && m[j].action == 'B'){
int temp;
if(m[i].time > m[j].time) {
temp = m[i].time;
}
else{
temp = m[j].time;
}
cout<<"Time: "<<temp<<" PC1: "<<m[i].person_code<<" PC2: "<<m[j].person_code<<endl;
fout<<temp<<' '<<m[i].person_code<<' '<<m[j].person_code<<endl;
counter++;
m[i].removed = true;
m[j].removed = true;
}
}
}
if(counter == 0) fout<<0<<"\n";
f.close();
fout.close();
}
As you can see, I've commented out the lines with char (person_code and cd). Right now the code is working with string (it outputs the same information as given above in my examples), but when I use char, the output file is blank.