簡體   English   中英

C ++和文件輸出

[英]C++ and output on files

我試圖做一個C ++程序誰做這樣的事情:在姓和名(文件)的列表,請參閱在另一個文件中“人姓是相同的姓氏和印章: 名稱姓名名稱等。那是我所做的,但是出了點問題

#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'誰檢查是否有相同的姓氏

您不是要讀取文件嗎?

ifstream fin("people.txt");
while(cin >> name >> surname) // cin reads from the console, waits for your input

fin代替cin

您也沒有persona重載的operator==

bool operator==(persona const& other) const
{
    return nome == other.nome && cognome == other.cognome;
}

還有operator<< ,這里需要:

cout << "People with surname (" << persone[i] << ...

打印persone[i].nomepersone[i].cognome

if (persone[k] == persone[j])是問題。 錯誤消息會告訴您什么地方錯了,什么是行號。 您正在比較兩個Persona結構,但是不能在它們上使用==運算符。 也許您應該比較它們的nome或cognome值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM