Yet another QWERTYman(tm) problem (with fun and stuff!)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <fstream>
#include <iostream>
#include <limits>
#include <string>
#include <vector>
using namespace std;

class ydm_notes
{
    int year;
    short day;
    short month;
    string notes;
    public:
    ydm_notes() {}
    ydm_notes(int m_year, short m_day, short m_month, string m_notes) : 
    day (m_day), month (m_month), year (m_year), notes(m_notes) {}
    ~ydm_notes() {}
    friend ostream & operator<<(ostream &, ydm_notes);
    friend istream & operator>>(istream &, ydm_notes);
    friend void sort(vector<ydm_notes> & vect);
    
    void input()
    {
        cout << "Year: ";
        cin >> year;
        cout << "Day: ";
        cin >> day;
        cout << "Month: ";
        cin >> month;
        cout << "Day's notes: ";
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        getline(cin, notes);
    }
};

ostream & operator<<(ostream & out, ydm_notes obj)
{
    out << obj.year << " " << obj.day << " " << obj.month << " notes: " << obj.notes << endl;
    return out;
}

istream & operator>>(istream & in, ydm_notes obj)
{
    in >> obj.year;
    in >> obj.day;
    in >> obj.month;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin.ignore(numeric_limits<streamsize>::max(), ':');
    getline(in, obj.notes);
    return in;
}

void sort(vector<ydm_notes> & vect)
{
    for(int i = vect.size()-1; i >= 0; i--)
    {
        for(int j = i; vect[j].year < vect[j-1].year; j--)
        {
        ydm_notes temp;
        temp = vect[i];
        vect[i] = vect[i-1];
        vect[i-1] = temp;
        }
        for(int j = i; vect[j].year < vect[j-1].year; j--)
        {
        ydm_notes temp;
        temp = vect[i];
        vect[i] = vect[i-1];
        vect[i-1] = temp;
        }
        for(int j = i; vect[j].month < vect[j-1].month; j--)
        {
        ydm_notes temp;
        temp = vect[i];
        vect[i] = vect[i-1];
        vect[i-1] = temp;
        }
    }
}

void editor(vector<ydm_notes> & vect, string file);

int main()
{
    vector<ydm_notes> notesh;
    while(true)
    {
    cout << "1: New File\n"
         << "2: Load File\n"
         << "3: Exit\n>";
    int choice;
    cin >> choice;
    if(!cin || (choice < 1 || choice > 4) )
    {
    cout << "Invalid input. Press ENTER to continue.\n";
    cin.clear();
    cin.sync();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    continue;
    }
    else if(choice == 3)
        return 0;
    else if(choice == 2)
    {
        cout << "File to load?\n>";
        string file;
        getline(cin, file);
        editor(notesh, file);
        continue;
    }
    else if(choice == 1)
    {
        editor(notesh, "\0");
    }
    }
}

void editor(vector<ydm_notes> & vect, string file)
{
    fstream FileStream;
    cout << "How many notes to log?\n";
    int num;
    cin >> num;
    for(int i = 0; i < num; i++)
    {
        ydm_notes temp;
        temp.input();
        vect.push_back(temp);
    }
    if(file != "\0")
    FileStream.open(file.c_str(), ios::in | ios::out);
    else
    {
        cout << "File to save to?\n>";
        getline(cin, file);
        FileStream.open(file.c_str(), ios::in | ios::out);
    }  
    
    for(int i = num; !FileStream.eof(); i++)
    {
    ydm_notes tmp;
    FileStream >> tmp;
    vect.push_back(tmp);
    }
    sort(vect);
    for(int i = 0; i < vect.size(); i++)
    FileStream << vect[i];
}

In the for loop on line 140, it's ungraciously giving its creator (moi) a segfault...I'm sure it's something to do with a member function, I've tried debugging, but I wasn't sure how to fix it.

Any ideas?
Why "\0"? "" is just as good.

Lines 48 and 49 should pass 'in' as the this parameter, not cin.

The problem is that you're allocating all your memory on line 144 because eof() never returns !0 if the file is empty. Try checking bad().
Still faulting in a seg manner. It's very possible that it's in the sort function.
sort() doesn't look like it should segfault, although it does look weird. I'm fairly certain line 144 is allocating all memory.
I'll try running it through the debuggah.
Your void editor(vector<ydm_notes> & vect, string file) function goes into an infinite loop here:
1
2
3
4
5
6
    for(int i = num; !FileStream.eof(); i++)
    {
    ydm_notes tmp;
    FileStream >> tmp;
    vect.push_back(tmp);
    }


Your design is incorrect.
I think Helios said that already though.
Tried bad(), too...

Grumble.
I tried your program, selected option 1, (New File), selected one Note - entered that, and selected the filename to save to (xxx.txt).

The file does not get created - I'm sure that ios::in |ios::out will not create a file if it does not exist - hence
the problem.
file.eof() or file.bad() just will not work if the file does not exist - hence the infinite loop.

Plus:
If I select option 1 (New File) - why are you trying to read from a just created file (which as we said does not get created anyway) here:
1
2
3
4
5
6
    for(int i = num; !FileStream.eof(); i++)
    {
    ydm_notes tmp;
    FileStream >> tmp; //read from the file
    vect.push_back(tmp);
    }


[EDIT]
remove blank spaces
Last edited on
Topic archived. No new replies allowed.