Vector error

For a school lab I have to take a text file with songs and out put the lines in a certain format with the total time below. I am done with my code but when I run it I get fatal vector errors. I suspect I may have gone "out of bounds" with a vector but cannot find it. Any help is appreciated :)

My text file:

Playlist.txt (name of my file)

Mingus "Ah um"
"Better Git It in Your Soul" 7:23 Charles Mingus
"Goodbye Pork Pie Hat" 5:44 Charles Mingus
"Boogie Stop Shuffle" 5:02 Charles Mingus
"Self-Portrait in Three Colors" 3:10 Charles Mingus
"Open Letter to Duke" 5:51 Charles Mingus
"Bird Calls" 6:17 Charles Mingus
"Fables of Faubus" 8:13 Charles Mingus
"Pussy Cat Dues" 9:14 Charles Mingus
"Jelly Roll" 6:17 Charles Mingus
"Bashin" 9:53 Charles Mingus

The output that is supposed to happen:

Mingus "Ah um"
-----------------------------------------------------
1. 7:23 Better Git It in Your Soul by Charles Mingus
2. 5:44 Goodbye Pork Pie Hat by Charles Mingus
3. 5:02 Boogie Stop Shuffle by Charles Mingus
4. 3:10 Self-Portrait in Three Colors by Charles Mingus
5. 5:51 Open Letter to Duke by Charles Mingus
6. 6:17 Bird Calls by Charles Mingus
7. 8:13 Fables of Faubus by Charles Mingus
8. 9:14 Pussy Cat Dues by Charles Mingus
9. 6:17 Jelly Roll by Charles Mingus
10. 9:53 Bashin by Charles Mingus
-----------------------------------------------------
Total Playing Time = 1:07:04

My code:

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
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include <stdlib.h>

using namespace std;

void readLine(vector<string> &playlist);
void timeAuthorLine(const vector<string> playlist, vector<string> &author, vector<string> &name, int &totaltime, vector<int> &min, vector<int> &sec1, vector<int> &sec2);
void displayData(const vector<string> playlist, const vector<string> author, const vector<string> name, int totaltime, const vector<int> min, const vector<int> sec1, const vector<int> sec2);

int main()
{
	vector<string> playlist;


	readLine(playlist);

	system("pause");

	return 0;
}
void readLine(vector<string> &playlist)
{
	vector<string> author, name;
	vector<int> min, sec1, sec2;
	int totaltime = 0, i = 0;
	ifstream inFile("Playlist.txt");
	if (inFile.fail())
	{
		cout << "File not found.";
			exit(1);
	}
	else
	{
		while (getline(inFile, playlist[i]))
		{
			getline(inFile, playlist[i]);
			i = i + 1;
		}
		inFile.close();
		timeAuthorLine(playlist, author, name, totaltime, min, sec1, sec2);
		displayData(playlist, author, name, totaltime, min, sec1, sec2);
	}
}
void timeAuthorLine(const vector<string> playlist, vector<string> &author, vector<string> &name, int &totaltime, vector<int> &min, vector<int> &sec1, vector<int> &sec2)
{
	for (int i = 0; i < playlist.size(); i++)
	{
		string line = playlist[i];
		int index = line.find(":");
		min[i] = line[index - 1] - '0';
		sec1[i] = line[index + 1] - '0';
		sec2[i] = line[index + 2] - '0';
		totaltime = totaltime + 60 * min[i] + 10 * sec1[i] + sec2[i];
		for (int j = index + 4; j < line.size(); j++)
		{
			author[i] = line[j];
		}
		for (int k = 1; k < index - 4; k++)
		{
			name[i] = line[k];
		}
	}
}
void displayData(const vector<string> playlist, const vector<string> author, const vector<string> name, int totaltime, const vector<int> min, const vector<int> sec1, const vector<int> sec2)
{
	cout << playlist[0] << endl;
	cout << "-----------------------------------------------------" << endl;
	for (int i = 0; i < playlist.size(); i++)
	{
		if ((i + 1) < 10)
			cout << " " << i + 1 << ".  ";
		else
			cout << i + 1 << ".  ";
		cout << min[i] << ":" << sec1[i] << sec2[i] << " " << name[i] << " by " << author[i] << endl;
	}
	cout << "-----------------------------------------------------" << endl;
	cout << "Total Playing Time = " << totaltime / 3600 << ":";
	if ((totaltime % 3600) / 60 < 10)
		cout << "0" << (totaltime % 3600) / 60 << ":";
	else
		cout << (totaltime % 3600) / 60 << ":";
	if ((totaltime % 3600) % 60 < 10)
		cout << "0" << (totaltime % 3600) % 60;
	else
		cout << (totaltime % 3600) % 60;
}
Last edited on
This shouldn't compile. Did it actually, really compile? You can see the errors here http://cpp.sh/9gfd by attempting to build and run the code

line[index - 1] is a char.

line[index - 1].c_str() is thus an attempt to call the class function c_str() in that char, but a char is not a class and does not have that class function.
Last edited on
Oops I have fixed that before, but I pasted the wrong version of the code. I have pasted my latest version (still has vector error I can't find). :(
I see you've edited your original code.

The next error is on line 38, where you attempt to write into the first element of the vector<string> named playlist. That vector is of size zero; there is no first element, so you cannot write into it.
I think you're mistaken (or maybe i'm just being confused at the way you say "first" element), int i starts at 0, and the loop increases int i by one and reads in each line of the text file into vector<string> playlist. Each new input of from a repeat in the loop increases the vector's size, since its size is dynamic.
while (getline(inFile, playlist[i]))

Here, the first time through the loop (which is as far as it gets), i is zero.

So this attempts to write into playlist[0], which would be the first element in playlist.

playlist[0] does not exist. The capacity of playlist at this point is zero. It has no elements. If it had any elements, the first element would be playlist[0], but it doesn't have any.

Each new input of from a repeat in the loop increases the vector's size, since its size is dynamic.


You're not inputting anything into the vector. You're trying to use strings already in the vector, which don't exist because you never put any strings into the vector. You can put strings into the vector with push_back or emplace_back, or you can have a whole bunch made in the vector when you create the vector, like this: vector<string> playlist(100); // start with 100 blank strings in the vector

The vector is dynamic in that when you add so many strings that it doesn't have enough memory to store them all, it gets more memory. It does not make the strings for you. You still have to make the strings, and put them in.


Anyway, the point is this; when you make a vector<string> with nothing in it, you still have to make every string that's going to go into that vector, and put it in. You could make a hundred blank strings and put them in, and then happily use those blank strings and overwrite them. That would fix your problem.
Last edited on
Favour value semantics for the vector. Let each function do just one thing, and do it well.

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
std::vector<std::string> read_lines( std::istream& stm )
{
    std::vector<std::string> result ;

    std::string line ;
    while( std::getline( stm, line ) ) result.push_back(line) ;

    return result ; 
}

int main()
{
    const std::string path = "Playlist.txt" ;

    std::ifstream file(path) ;

    if( file.is_open() )
    {
        const auto playlist = read_lines(file) ;

        // parse the lines, compute stuff, print results etc.
        // (call the other functions to do these)
    }

    else
    {
        std::cerr << "file could not be opened\n" ;
        return 1 ;
    }
}
Topic archived. No new replies allowed.