File handing code. Please assist.

So for some reason, my getline for entering the albumTitle in the deleteAlbum function is giving me this "no matching function call". Can someone please help me out?

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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
 #include <iostream>
#include <fstream>
#include <string>

using namespace std;

//Function Prototypes:
void addAlbum();
void displayAlbums();
bool deleteAlbum(string deletedAlbum, bool success);


int main()
{
    ifstream fin;
    ofstream fout;
    bool success = false;
    string deletedAlbum;
    string albumTitle;
    int choice = 0;

    //Main Menu of Program:
    cout << "Welcome to my music library. What would you like to do?" << endl;
    do
    {
        cout << "1: Display Favorite Albums" << endl;
        cout << "2: Add an Album" << endl;
        cout << "3: Remove an Album" << endl;
        cout << "4: Exit" << endl << "#";
        cin >> choice;
        if(choice == 1)
            displayAlbums();
            //Displays albums on screen, to the user.
        if(choice == 2)
            addAlbum();
            //Adds an album that the user may enter into the program.
        if(choice == 3)
        {
            cout << "Enter an album to remove: "<< endl;;
            cin >> deletedAlbum;
            success = deleteAlbum(deletedAlbum, success);
            if(success == true)
                cout << "Album deleted." << endl;
            else
                cout << "Could not delete. I guess you can't get that one song out of your head." << endl;
        }
    } while(choice != 4);
    return 0;
}
//Name: displayAlbums
//Description: Displays on screen the contents of "myFavoriteAlbums.txt".
//Precondition: Text file must have been created ("myFavoriteAlbums.txt"), complete with variables such as year of release, album artist and title.
void displayAlbums()
{
    string title;
    string artist;
    int yearReleased;
    ifstream fin;
    fin.open("myFavoriteAlbums.txt");
    if(fin.is_open())
    {
        while(isalnum(fin.peek()))
        //Display of albums to user
        {
            getline(fin, title);
            getline(fin, artist);
            fin >> yearReleased;
            fin.ignore(5, '\n');
            cout << title << " by " << artist << " released in " << yearReleased << "." << endl;
        }
        fin.close();
    }
    else //if the file open is unsuccessful
    {
        cout << "File cannot be opened." << endl;
    }
}

//Name: deleteAlbum
//Description: Deletes album data from program.
//Input:  string deletedAlbum
//        bool success - determines if deleted successfully
//Output: bool success - determines if deleted successfully
bool deleteAlbum (string deletedAlbum, bool success)
{
    ifstream fin;
    ofstream fout;
    const int ARRAY_SIZE = 100;
    string albumTitle [ARRAY_SIZE] = {" "};
    string albumArtist [ARRAY_SIZE] = {" "};
    int yearOfRelease [ARRAY_SIZE] = {0};
    int counter = 0;
    string album, title;
    int yearReleased;

    fin.open("myFavoriteAlbums.txt");
    if(fin.is_open())
    {
        while(isalnum(fin.peek()) && counter < ARRAY_SIZE)
        {
            getline(fin, title);
            if(title != deletedAlbum)
            {
                albumTitle [counter] = title;
                getline(fin, albumArtist);
                albumArtist[counter] = artist;
                fin >> yearReleased;
                fin.ignore(5, '\n');
                yearOfRelease[counter] = yearReleased;
                counter++;
            }
            else
            {
                getline(fin, artist);
                fin >> yearReleased;
                fin.ignore(5, '\n');
                success = true;
            }
        }
        fin.close();
    }
    else
        cout << "File cannot be read." << endl;

    fout.open("myFavoriteAlbums.txt"); //Overwrites data in file and program.
    if(fout.is_open())
    {
        for(int i = 0; i < counter; i++)
        {
            fout << albumTitle[i] << "\n";
            fout << albumArtist[i] << endl;
            fout << yearOfRelease[i] << "\n";
        }
        fout.close();
    }

    else
        cout << "Cannot read file!" << endl;
    return success;
}

//Name: addAlbum
//Description: Adds user-inputted album to "myFavoriteAlbums.txt".
void addAlbum ()
{
    ofstream fout;
    ifstream fin;
    string albumTitle;
    string albumArtist;
    int yearOfRelease;

    fout.open("myFavoriteAlbums.txt", ios::app);
    if(fout.is_open())
    {
        cin.ignore(5, '\n');
        cout << "Enter the title of the album: ";
        getline(cin, albumTitle);
        fout << albumTitle << endl;
        cout << "Enter the artist who crafted the gem of an album: ";
        getline(cin, albumArtist);
        fout << albumArtist << endl;
        cout << "Enter the year the album was released: ";
        cin >> yearOfRelease;
        cin.ignore(100, '\n');
        fout << yearOfRelease << endl;
        fout.close();
        cout << endl;
    }
    else
        cout << "File cannot be read." << endl;
}
Please post a sample of your input file.


Sorry about that. The contents of the file are:

"myFavoriteAlbums.txt":

O
Damien Rice
2002
On Avery Island
Neutral Milk Hotel
1996
Milk & Kisses
Cocteau Twins
1996
Nightlife
Phantogram
2011
The World Is Not A Cold Dead Place
Explosions in the Sky
2003
Let's look at the following snippet:
1
2
3
4
5
6
7
    string albumArtist [ARRAY_SIZE] = {" "};
    int yearOfRelease [ARRAY_SIZE] = {0};
...
                albumTitle [counter] = title;
                getline(fin, albumArtist);
                albumArtist[counter] = artist;
...


It looks like albumArtist is defined as an array but in the getline() you're trying to use this variable as a non-array.

Also where have you defined a variable with the name of artist?

In future if you get compile errors please post the complete error messages all of them exactly as they appear in your development environment. These messages have important information embedded within them to help locate and fix the problems.

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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <iostream>
#include <fstream>
#include <string>


using namespace std;


//Function Prototypes:
void addAlbum();
void displayAlbums();
bool deleteAlbum(string deletedAlbum, bool success);
int main()
{
    ifstream fin;
    ofstream fout;
    bool success = false;
    string deletedAlbum;
    string albumTitle;
    int choice = 0;
    //Main Menu of Program:
    cout << "Welcome to the music storage program. Here you can keep track of your favorite albums, artists and release years. What would you like to do?" << endl;
    do
    {
        cout << "1: Display Favorite Albums" << endl;
        cout << "2: Add an Album" << endl;
        cout << "3: Remove an Album" << endl;
        cout << "4: Exit" << endl;
        cin >> choice;
        if(choice == 1)
            displayAlbums();
            //Displays albums on screen, to the user.
        if(choice == 2)
            addAlbum();
            //Adds an album that the user may enter into the program.
        if(choice == 3)
            //Asks user to enter the title of the album they wish to delete.
        {
            cout << "Enter an album to remove: "<< endl;;
            cin >> deletedAlbum;
            success = deleteAlbum(deletedAlbum, success);
            if(success == true)
                cout << "Album deleted." << endl;
            else
                cout << "Could not delete. I guess you can't get that one song out of your head." << endl;
        }
    } while(choice != 4);
    return 0;
}
//Name: displayAlbums
//Description: Displays on screen the contents of "myFavoriteAlbums.txt".
//Precondition: Text file must have been created ("myFavoriteAlbums.txt"), complete with variables such as year of release, album artist and title.
void displayAlbums()
{
    string title;
    string artist;
    int yearReleased;
    ifstream fin;
    fin.open("myFavoriteAlbums.txt");
    if(fin.is_open())
    {
        while(isalnum(fin.peek()))
        //Display of albums to user
        {
            getline(fin, title);
            getline(fin, artist);
            fin >> yearReleased;
            fin.ignore(5, '\n');
            cout << title << " by " << artist << " released in " << yearReleased << "." << endl;
        }
        fin.close();
    }
    else //if the file open is unsuccessful
    {
        cout << "File cannot be opened." << endl;
    }
}
//Name: deleteAlbum
//Description: Deletes album data from program.
//Input:  string deletedAlbum
//        bool success - determines if deleted successfully
//Output: bool success - determines if deleted successfully
bool deleteAlbum (string deletedAlbum, bool success)
{
    ifstream fin;
    ofstream fout;
    const int ARRAY_SIZE = 100;
    string albumTitle [ARRAY_SIZE] = {" "};
    string albumArtist [ARRAY_SIZE] = {" "};
    int yearOfRelease [ARRAY_SIZE] = {0};
    int counter = 0;
    string title, artist;
    int yearReleased;
    fin.open("myFavoriteAlbums.txt");
    if(fin.is_open())
    {
        while(isalnum(fin.peek()) && counter < ARRAY_SIZE)
        {
            getline(fin, title);
            if(title != deletedAlbum)
            {
                albumTitle [counter] = title;
                getline(fin,artist);
                albumArtist[counter] = artist;
                fin >> yearReleased;
                fin.ignore(5, '\n');
                yearOfRelease[counter] = yearReleased;
                counter++;
            }
            else
            {
                getline(fin, artist);
                fin >> yearReleased;
                fin.ignore(5, '\n');
                success = true;
            }
        }
        fin.close();
    }
    else
        cout << "File cannot be read." << endl;
    fout.open("myFavoriteAlbums.txt"); //Overwrites data in file and program.
    if(fout.is_open())
    {
        for(int i = 0; i < counter; i++)
        {
            fout << albumTitle[i] << "\n";
            fout << albumArtist[i] << endl;
            fout << yearOfRelease[i] << "\n";
        }
        fout.close();
    }
    else
        cout << "Cannot read file!" << endl;
    return success;
}
//Name: addAlbum
//Description: Adds user-inputted album to "myFavoriteAlbums.txt".
void addAlbum ()
{
    ofstream fout;
    ifstream fin;
    string albumTitle;
    string albumArtist;
    int yearOfRelease;
    fout.open("myFavoriteAlbums.txt", ios::app);
    if(fout.is_open())
    {
        cin.ignore(5, '\n');
        cout << "Enter the title of the album: ";
        getline(cin, albumTitle);
        fout << albumTitle << endl;
        cout << "Enter the artist who crafted the gem of an album: ";
        getline(cin, albumArtist);
        fout << albumArtist << endl;
        cout << "Enter the year the album was released: ";
        cin >> yearOfRelease;
        cin.ignore(100, '\n');
        fout << yearOfRelease << endl;
        fout.close();
        cout << endl;
    }
    else
        {
            cout << "We can't input that album. You either entered in Justin Bieber, or there is an error." << endl;
        }

    (fin.close());

}


Fixed that. Now I've noticed that,
1. I cannot enter an album and successfully save it.
2. When I enter a title to delete (namely, The World is a Cold Dead Place) it endlessly loops.
Your endless loop seems to be caused by the extraction operator in main() getting the title. You probably should be using getline() here instead of the extraction operator.

I'd also suggest that you make the "year" a string so you don't need to keep switching between getline() and the extraction operator then dealing with the dangling new line character.

Got it. And for some reason my entire "deleteAlbum" function is malfunctioning. It displays the error message before it even asks for the name of an album to delete.
But that's not even in your delete() function, looks like there's a problem in main(), probably the same kind of error (hanging new line character), or perhaps you should be using getline() instead of the extraction operator?


This is what I've got for that function:

(In Main)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
        if(choice == 3)
            //Asks user to enter the title of the album they wish to delete.
        {
            cout << "Enter an album to remove: " << endl;
            getline (cin, deletedAlbum);
            success = deleteAlbum(deletedAlbum, success);
            if(success == true)
                cout << "Album deleted." << endl;
            else
                cout << endl;
                cout << "Could not delete. I guess you can't get that one song out of your head..." << endl;
        }
    } while(choice != 4);
    return 0; 



(In actual deleteAlbum function:)
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
bool deleteAlbum (string deletedAlbum, bool success)
{
    ifstream fin;
    ofstream fout;
    const int ARRAY_SIZE = 100;
    string albumTitle [ARRAY_SIZE] = {" "};
    string albumArtist [ARRAY_SIZE] = {" "};
    int yearOfRelease [ARRAY_SIZE] = {0};
    int counter = 0;
    string title, artist;
    int yearReleased;
    fin.open("myFavoriteAlbums.txt");
    if(fin.is_open())
    {
        while(isalnum(fin.peek()) && counter < ARRAY_SIZE)
        {
            getline(fin, title);
            if(title != deletedAlbum)
            {
                albumTitle [counter] = title;
                getline(fin,artist);
                albumArtist[counter] = artist;
                fin >> yearReleased;
                fin.ignore(5, '\n');
                yearOfRelease[counter] = yearReleased;
                counter++;
            }
            else
            {
                getline(fin, artist);
                fin >> yearReleased;
                fin.ignore(5, '\n');
                success = true;
            }
        }
        fin.close();
    }
    else
        cout << "File cannot be read." << endl;
    fout.open("myFavoriteAlbums.txt"); //Overwrites data in file and program.
    if(fout.is_open())
    {
        for(int i = 0; i < counter; i++)
        {
            fout << albumTitle[i] << "\n";
            fout << albumArtist[i] << endl;
            fout << yearOfRelease[i] << "\n";
        }
        fout.close();
    }
    else
        cout << "Cannot read file!" << endl;
    return success;
}
Last edited on
I'd simplify your function to something like:
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
bool deleteAlbum (string deletedAlbum)
{
    bool success = false; // NOTICE the change.
    const int ARRAY_SIZE = 100;   // This should probably be a global so all your functions can use it.
    string albumTitle [ARRAY_SIZE] = {};
    string albumArtist [ARRAY_SIZE] = {};
    string yearOfRelease [ARRAY_SIZE] = {}; // Notice the change.
    int counter = 0;
    string title, artist;
    string yearReleased;         // Notice the change.
    ifstream fin("myFavoriteAlbums.txt");
    if(fin.is_open())
    {
        while(getline(fin, title) && counter < ARRAY_SIZE)
        {
            getline(fin, artist);
            getline(fin, yearReleased);

            if(title != deletedAlbum)
            {
                albumTitle [counter] = title;
                albumArtist[counter] = artist;
                yearOfRelease[counter] = yearReleased;
                counter++;
            }
            else
            {
                success = true;
            }
        }
        fin.close();
    }
    else
        cout << "File cannot be read." << endl;


    ofstream fout("myFavoriteAlbums.txt"); //Overwrites data in file and program.  NOTICE THE CHANGE.
    if(fout.is_open())
    {
        for(int i = 0; i < counter; i++)
        {
            fout << albumTitle[i] << "\n";
            fout << albumArtist[i] << endl;
            fout << yearOfRelease[i] << "\n";
        }
        fout.close();
    }
    else
        cout << "Cannot read file!" << endl;
    return success;
}


And for main() :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
...
        cin >> choice;
        cin.ignore(1000, '\n');  // NOTICE THE CHANGE.
...
        if(choice == 3)
            //Asks user to enter the title of the album they wish to delete.
        {
            cout << "Enter an album to remove: " << endl;
            getline (cin, deletedAlbum);
            success = deleteAlbum(deletedAlbum, success);
            if(success == true)
                cout << "Album deleted." << endl;
            else
                cout << endl;
                cout << "Could not delete. I guess you can't get that one song out of your head..." << endl;
        }
    } while(choice != 4);


Try the suggested changes and see if your problems are solved. I believe the problem is being caused by the cin >> choice; leaving the end of line character in the buffer.


I'm so sorry, it didn't do anything but give the compiler a bunch of errors (including that ofstream in line 37 wasn't going to work and that declaring bool success in the deleteAlbum function was "shadowing a parameter". I ran the code and it gave me a termination error.
Last edited on
I'm so sorry, it didn't do anything but give the compiler a bunch of errors (including that ofstream in line 37 wasn't going to work

Please show your code, there is nothing wrong with line 37 in the above code. Did you notice that I deleted the "ofstream fout;" line (line 141 in your code)?

and that declaring bool success in the deleteAlbum function was "shadowing a parameter"

Didn't you notice that I deleted the parameter? This variable should be local to the function, there is no reason to be passing it as a parameter.
Topic archived. No new replies allowed.