program crashing

hi guys my program seems to be crashing when I choose option 2,the program for some reason decides to crash,I'm not sure why it does this and how I would go about fixing it

thanks

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
  map<string,string>* readFromDiary(){

	map<string, string> entries;
	ifstream in;
	string date;
	string description;
	string fileName = "diary.txt";

	in.open(fileName);

	if (!in.is_open()) {

		cout << "file could not be opened" << endl;
		return NULL;
	}

	while (in) {

		getline(in, date, ':');
		getline(in, description);
		if (!in) {
			break;
		}
		cout << date;
		cout << description;
		cout << endl;

		entries[date] = description;
	}
	return &entries;
}


void openDiary(){




}

void options(){

	cout << "MY DIARY" << endl;
	cout << "Press 1 - to add entry" << endl;
	cout << "press 2 - to view entries" << endl;
	cout << "press 3 - to quit" << endl;
}

int main() {

	bool quit = false;
	map<string,string>* entries = NULL;

	while(!quit){

		entries = readFromDiary();
		options();
		int choice;
		cin >> choice;

		switch(choice){

		case 1: inputToDiary();
		break;
		case 2:
		for (map<string, string>::iterator it = entries->begin();
					it != entries->end(); it++) {

			cout << it->first;
			cout << it->second << endl;
			}
		break;
		case 3: quit = false;
		break;
		default: cout << "invalid choice" << endl;
	}

	}

}
It crashes because a pointer to local object is returned that does not longer exists after the end of the function.
thanks Coder that makes sense =)

I tried fixing it up but it tells me that entries was not declared in this scope,the entries inside my readFromDiary() function not main

thanks

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


map<string,string>* readFromDiary(){

	map<string, string> entries* = new map<string,string>;
	ifstream in;
	string date;
	string description;
	string fileName = "diary.txt";

	in.open(fileName);

	if (!in.is_open()) {

		cout << "file could not be opened" << endl;
		return NULL;
	}

	while (in) {

		getline(in, date, ':');
		getline(in, description);
		if (!in) {
			break;
		}
		cout << date;
		cout << description;
		cout << endl;

		&entries[date] = description;
	}
	return entries;
}

Sorry I put the * at the wrong position

but I am still getting an error saying no match for operator []

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


map<string,string>* readFromDiary(){

	map<string,string> *entries = new map<string,string>;
	ifstream in;
	string date;
	string description;
	string fileName = "diary.txt";

	in.open(fileName);

	if (!in.is_open()) {

		cout << "file could not be opened" << endl;
		return NULL;
	}

	while (in) {

		getline(in, date, ':');
		getline(in, description);
		if (!in) {
			break;
		}
		cout << date;
		cout << description;
		cout << endl;

		&entries[date] = description;
	}
	return entries;
}

stupid me I didn't dereference it right,

but even when I allocated my map on the heap,it still crashes when I select the option 2

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

map<string,string>* readFromDiary(){

	map<string,string> *entries = new map<string,string>;
	ifstream in;
	string date;
	string description;
	string fileName = "diary.txt";

	in.open(fileName);

	if (!in.is_open()) {

		cout << "file could not be opened" << endl;
		return NULL;
	}

	while (in) {

		getline(in, date, ':');
		getline(in, description);
		if (!in) {
			break;
		}
		cout << date;
		cout << description;
		cout << endl;

		(*entries)[date] = description;
	}
	return entries;
}


void openDiary(){




}

void options(){

	cout << "MY DIARY" << endl;
	cout << "Press 1 - to add entry" << endl;
	cout << "press 2 - to view entries" << endl;
	cout << "press 3 - to quit" << endl;
}

int main() {

	bool quit = false;
	map<string,string>* entries = NULL;

	while(!quit){

		entries = readFromDiary();
		options();
		int choice;
		cin >> choice;

		switch(choice){

		case 1: inputToDiary();
		break;
		case 2:
		for (map<string, string>::iterator it = entries->begin();
					it != entries->end(); it++) {

			cout << it->first;
			cout << it->second << endl;
			}
		break;
		case 3: quit = true;
		break;
		default: cout << "invalid choice" << endl;
	}

	}

}
Hints:

diary.txt:
11/04/2017: Lorem ipsum dolor sit amet, ei usu exerci vocibus invidunt, ea tantas civibus sed, ne vitae alterum probatus eam. Assum ornatus ius ea, autem commodo tamquam eam ex. Est habeo signiferumque an, ut his novum definitionem. Quod hendrerit omittantur ad duo, ei usu diceret delicata. Te cum populo scripta gubergren.

09/03/2017: Duo vide consul et, ne est abhorreant moderatius. Option partiendo eos eu, per ea timeam mandamus mediocritatem. Nec at minim sonet consequuntur. Sea eu semper aliquid volumus, et solum copiosae pertinax eum. Possit dictas facilisis mei ex.

05/06/2016: Mundi persius platonem no vim. Sea no aperiam omittantur. Ad vel sanctus utroque assentior, mei ei aeque suscipit patrioque. Alia mundi postulant nam eu, his at amet feugait hendrerit. Ex quis dicam aeterno sea, rebum recusabo et mel.


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
#include <fstream>
#include <iostream>
#include <map>

using diarymap = std::map<std::string, std::string>;

void options();
void inputToDiary();
diarymap& readFromDiary(diarymap& entries);

int main()
{
    bool quit = false;
    diarymap entries;
    while(!quit) {
        readFromDiary(entries);
        options();
        int choice;
        std::cin >> choice;
        std::cin.ignore();

        switch(choice) {
        case 1:
            inputToDiary();
            break;
        case 2:
            for (diarymap::iterator it = entries.begin();
                    it != entries.end(); it++) {
                std::cout << it->first << ' ' << it->second << '\n';
            }
            std::cout << '\n';
            break;
        case 3:
            quit = true;
            break;
        default:
            std::cout << "invalid choice\n";
        }
    }
}

void options()
{
    std::cout << "MY DIARY\n"
                 "Press 1 - to add entry\n"
                 "Press 2 - to view entries\n"
                 "Press 3 - to quit\n";
}

void inputToDiary()
{
    std::cout << "Sorry: no code yet.\n\n";
}

diarymap& readFromDiary(diarymap& entries)
{
    std::ifstream in("diary.txt");
    if (!in) {
        std::cout << "file could not be opened\n";
        return entries;
    }

    for(std::string line; std::getline(in, line); /**/) {
        if(line.empty()) { continue; }
        std::string::size_type mypos = line.find(':');
        std::string date = line.substr(0, mypos);
        line.erase(0, mypos);
        entries.emplace(date, line);
    }
    in.close();
    return entries;
}


Output:
MY DIARY
Press 1 - to add entry
Press 2 - to view entries
Press 3 - to quit
1
Sorry: no code yet.

MY DIARY
Press 1 - to add entry
Press 2 - to view entries
Press 3 - to quit
2
05/06/2016 : Mundi persius platonem no vim. Sea no aperiam omittantur. Ad vel 
sanctus utroque assentior, mei ei aeque suscipit patrioque. Alia mundi 
postulant nam eu, his at amet feugait hendrerit. Ex quis dicam aeterno sea, 
rebum recusaboet mel.
09/03/2017 : Duo vide consul et, ne est abhorreant moderatius. Option 
partiendo eos eu, per ea timeam mandamus mediocritatem. Nec at minim sonet 
consequuntur. Sea eu semper aliquid volumus, et solum copiosae pertinax eum. 
Possit dictas facilisis mei ex.
11/04/2017 : Lorem ipsum dolor sit amet, ei usu exerci vocibus invidunt, ea 
tantas civibus sed, ne vitae alterum probatus eam. Assum ornatus ius ea, autem 
commodo tamquam eam ex. Est habeo signiferumque an, ut his novum definitionem. 
Quod hendrerit omittantur ad duo, ei usu diceret delicata. Te cum populo 
scripta gubergren.

MY DIARY
Press 1 - to add entry
Press 2 - to view entries
Press 3 - to quit
3
Topic archived. No new replies allowed.