'Is this a good way to read multiple data from a file using a struct?
I recently started up coding again and there is an exercise which is giving me nightmares.
It's about finding surnames with equal characters. They give me a file which has four values: The first one is an integer that rappresent the number of values that I have to check in the surnames, the second, third and fourth are the surnames, let me give you an example:
2 mazzei marfeo marchica 3 musacchia greca musmeci 4 nicosia nicolosi nicotra
I'm having problems to work with those data, I didn't work a lot with files and structures so I'm struggling a bit, how can I make understand the code that the first value is the number of character that I want to examinate? And more generally, how can I save those information?
This is the code that I made up just for having an idea of how I could do that, don't mind if it's a lot messy.
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <fstream>
using namespace std;
struct Task{
int caratteriDaConfrontare;
string cognomi[2];
};
int main(){
ifstream fin("prova.txt");
ofstream out;
out.open("output.txt");
Task *dati=new Task[255];
string line;
int value=dati[0].caratteriDaConfrontare;;
while(fin>>line){
for(int i=0; i<value; i++){
string firstSurname=dati[i].cognomi[0];
unsigned short surnameLengthI=dati[i].cognomi[0].length();
string secondSurname=dati[i].cognomi[1];
unsigned short surnameLengthII=dati[i].cognomi[1].length();
string thirdSurname=dati[i].cognomi[2];
unsigned short surnameLengthIII=dati[i].cognomi[2].length();
for(int x=0; x<surnameLengthI; x++){
for(int y=0; y<surnameLengthII; y++){
for(int z=0; z<surnameLengthIII; z++){
if(firstSurname[x]==secondSurname[y])out<<value<<firstSurname<<secondSurname;
else if(firstSurname[x]==thirdSurname[z])out<<value<<firstSurname<<thirdSurname;
else if(secondSurname[y]==thirdSurname[z])out<<value<<secondSurname<<thirdSurname;
}
}
}
}
}
fin.close();
out.close();
return 0;
}
Of course it's giving me errors, thanks to everyone that'll respond!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
