Reputation: 1
I was trying to do a C++ program who do a thing like this: in a list of surname and name (file), read the same surname and stamp in another file 'people with surname surname are: name,name,name etc.. That's is what i did, but something went wrong
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct persona
{
string nome;
string cognome;
};
int carica_vettore (persona a [])
{
string name, surname;
int i=0, np=0;
ifstream fin ("people.txt");
while (cin >> name >> surname)
{
if (name == "0" && surname == "0")
break;
a[i].nome = name;
a[i].cognome = surname;
i++;
np++;
}
return np;
}
int main ()
{
string nomepers, cognomepers;
int cont = 0;
persona persone [50];
cont = carica_vettore(persone);
for (int k = 0; k < cont; k++)
{
for (int j = k+1; j < cont-1; j++)
if (persone[k] == persone[j])
cout << "People with surname (" << persone[i] << "), are: " << persone[i].nome;
}
return 0;
}
And also i forget to stamp on file, but i think that's is not such a big problem
So, anyone could help me?
2 'for' who check if there is any same surnames btw
Upvotes: 0
Views: 38
Reputation: 1439
if (persone[k] == persone[j])
is the problem. The error message tells you what is wrong and what the line number is. you are comparing two Persona structs, but you cannot use the ==
operator on them. maybe you should compare their nome or cognome values instead.
Upvotes: 1
Reputation: 19617
Didn't you mean to read from a file?
ifstream fin("people.txt");
while(cin >> name >> surname) // cin reads from the console, waits for your input
Replace cin
with fin
.
You also don't have operator==
for persona
overloaded:
bool operator==(persona const& other) const
{
return nome == other.nome && cognome == other.cognome;
}
And also operator<<
, which is required here:
cout << "People with surname (" << persone[i] << ...
Print persone[i].nome
or persone[i].cognome
instead.
Upvotes: 3