Vova Leksakov
Vova Leksakov

Reputation: 19

Why is my vector not being written to code?

My main task is to sort the vector with the class of letters in the future, but for a reason that is not clear to me, the vector is not recorded, only the first line of the user’s mail. This is my header

#ifndef CLASS_H
#define CLASS_H

#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct date {
    int day{ 1 };
    int month{ 1 };
    int year{ 1999 };

    date(int day, int month, int year) {
        this->day = day;
        this->month = month;
        this->year = year;
    }

    date() = default;

    friend istream& operator>>(istream& is, date& date) {
        is >> date.day >> date.month >> date.year;
        return is;
    }

    friend ostream& operator<<(ostream& os, const date& date) {
        os << date.day << "." << date.month << "." << date.year;
        return os;
    }
};

class letter {
public:
    string sender{ "None" };
    string message;
    date when;

    string Get_sender() {
        return sender;
    }

    string Get_message() {
        return message;
    }

    date Get_date() {
        return when;
    }

    letter() = default;

    letter(string sender, string message, date when)
        : sender(sender), message(message), when(when) {}

    friend istream& operator>>(istream& is, letter& mes) {
        is >> mes.sender;
        char quote;
        is >> ws >> quote;
        getline(is, mes.message, '"');
        is >> mes.when;
        return is;
    }

    friend ostream& operator<<(ostream& os, const letter& letterObj) {
        os << "Sender: " << letterObj.sender << endl;
        os << "Message: " << letterObj.message << endl;
        os << "Date: " << letterObj.when << endl;
        return os;
    }
};

class Email {
public:
    vector<letter> letters;
    string userName{ "None" };

    friend istream& operator>>(istream& is, Email& mail) {
        is >> mail.userName;
        return is;
    }

    friend ostream& operator<<(ostream& os, const Email& mail) {
        os << "User name: " << mail.userName << endl;
        os << "Vector letters:" << endl;
        for (const letter& letterObj : mail.letters) {
            os << letterObj << endl;
        }
        return os;
    }
};

#endif // !CLASS_H

main cpp

#include <iostream>
#include "class.h"
#include <string>
#include <windows.h>
#include <fstream>
#include <vector>
using namespace std;

int main() {
    char FileName[100];
    cout << "Which file should I enter data from?\n";
    cin.getline(FileName, sizeof(FileName));
    ifstream fin(FileName);
    Email email;
    if (fin.is_open()) {
        fin >> email;
        letter temp;
        while (fin >> temp) {
            email.letters.push_back(temp);
        }
        fin.close();
        cout << email << endl;
        cout << "Which file should I output the data to?\n";
        cin.getline(FileName, sizeof(FileName));
        ofstream fout(FileName);
        if (fout.is_open())
        {
            fout << email;
            fout.close();
            cout << "Data output to file " << FileName << endl;
        }
        else
        {
            cout << "Error writing to file " << FileName << endl;
        }
    }
    else
    {
        cout << "File " << FileName << " not found\n";
    }

    Sleep(7654);
    return 0;
}

and my file data.txt:

[email protected] 
[email protected] "I love my mom" 17.08.2023 
[email protected] "HABIBATI" 13.09.2024 
[email protected] "This is my 4720 year!" 31.12.4719 

I think there is either a problem in ostream or in reading a file from the stream. My vector is not written when reading.

Upvotes: 0

Views: 96

Answers (0)

Related Questions