我正在尝试做一个 C++ 程序,它做这样的事情: 在一个姓氏和名字的列表(文件)中,读取相同的姓氏并在另一个文件中盖章 'people with surname surname are: name,name、姓名 等.. 这就是我所做的,但出了点问题
#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;
}
还有我忘记在文件上盖章了,但我认为这不是什么大问题
那么,有人可以帮助我吗?
2 'for' 谁检查是否有任何相同的姓氏 btw
最佳答案
你不是要从文件中读取吗?
ifstream fin("people.txt");
while(cin >> name >> surname) // cin reads from the console, waits for your input
替换cin
与 fin
.
你也没有operator==
对于 persona
重载:
bool operator==(persona const& other) const
{
return nome == other.nome && cognome == other.cognome;
}
还有operator<<
,这里是必需的:
cout << "People with surname (" << persone[i] << ...
打印 persone[i].nome
或 persone[i].cognome
相反。
关于C++ 和文件输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34250417/