Problem adding info to Array of Structs

I am trying to add data to an array of structs in the following function. It complies fine and i get validation with each "addSong" that the song successfully added with a return value of 0, yet the showSongList shows that only the first song added correctly and then the very last artist and size added, but in the second slot. Please help me figure out what i am doing wrong...

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
173
174
175
176
177
178
179
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <string>

#include "tsuPod.h"

struct Song
{
   string title;
   string artist;
   int size;
};

const int MAX_SONGS = 8;
const int MAX_MEMORY = 25;

Song songs[MAX_SONGS];

using namespace std;

/* FUNCTION - int calcRemainingMem

 Calculates the total memory available

 input parms - none

 output parms - the remaining memory
*/


int calcRemainingMem ()
{
    int total = 0;
    int memLeft;

    for (int i = 0; i < MAX_SONGS; i++)
        total = total + songs[i].size;

    memLeft = MAX_MEMORY - total;
    return memLeft;
}


/* FUNCTION - void initTsuPod

 Initialize all the slots to blank and 0 size memory

 input parms - none

 output parms - none
*/

void initTsuPod ()
{
    for (int i = 0; i < MAX_SONGS; i++)
    {
        songs[i].title = "";
        songs[i].artist = "";
        songs[i].size = 0;
    }
}


/* FUNCTION - int addSong
 * attempts to add a new song to the tsuPod
          o returns a 0 if successful
          o returns -1 if not enough memory to add the song
          o returns -2 for any other error (such as a blank title or artist)

 input parms - the title, artist, and size of the song

 output parms - the corresponding int for whether the song add was successful
*/

int addSong (string newTitle, string newArtist, int size)
{
    int memLeft = calcRemainingMem ();
    //declaring counting int i for while loop below
    int i = 0;

    if (size > memLeft)
        return -1;

    else if (newTitle == "")
        return -2;

    else if (newArtist == "")
        return -2;

    while (i < MAX_SONGS)
    {
        if (songs[i].size > 0)
            i++;
        else
            songs[i].title = newTitle;
            songs[i].artist = newArtist;
            songs[i].size = size;
            return 0;
    }

    if (i >= MAX_SONGS)
        return -2;
}


/* FUNCTION - int removeSong
    * attempts to remove a song from the tsuPod
          o returns 0 if successful
          o returns -1 if nothing is removed


input parms - the title of the song to be removed

output parms - the corresponding int for whether the removal was successful
*/

int removeSong (string title)
{
    for (int i = 0; i < MAX_SONGS; i++)
    {
        if (songs[i].title == title)
        {
            songs[i].title = "";
            songs[i].artist = "";
            songs[i].size = 0;
            return 0;
        }
        else
            return -1;
    }
}


/* FUNCTION - void clearMemory
* clears all the songs from memory

input parms - none

output parms - none
*/
void clearMemory()
{

    for (int i = 0; i < MAX_SONGS; i++)
    {
        songs[i].title = "";
        songs[i].artist = "";
        songs[i].size = 0;
    }
}


/* FUNCTION - void showSongList
    * prints the current list of songs in order from first to last to standard output
    * format - slot #, Title, Artist, size in MB (one song per line)
    * print "Empty" for any slots that do not contain a song

input parms - none

output parms - none
*/

void showSongList ()
{
    cout << "\t\t tsuPod v1.0 Song List" << endl;
    cout << "\t\t _____________________" << endl;

    for (int i = 0; i < MAX_SONGS; i++)
    {
        if (songs[i].size > 0)
        {
            cout << "Slot #" << i + 1 << ": " << songs[i].title << " by " << songs[i].artist << ", " << songs[i].size << "mb" << endl;
        }
        else
            cout << "Slot # " << i + 1 << ": Empty" << endl;
    }
}

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

#include "tsuPod.h"

using namespace std;

int main()
{

    initTsuPod();

    int retCode;

    retCode = addSong("Runaway", "Bon Jovi", 1);
    cout << retCode << endl;

    retCode = addSong("Time", "Pink Floyd", 2);
    cout << retCode << endl;

    retCode = addSong("Bad Moon", "Creedance Clearwater Revival", 3);
    cout << retCode << endl;

    retCode = addSong("I'll Wait", "Van Halen", 4);
    cout << retCode << endl;

    retCode = addSong("Tush", "ZZ Top", 5);
    cout << retCode << endl;

    retCode = addSong("Nature Boy", "Nat King Cole", 6);
    cout << retCode << endl;

    retCode = addSong("DOA", "Foo Fighters", 2);
    cout << retCode << endl;

    retCode = addSong("Houndog", "Elvis", 1);
    cout << retCode << endl;

    showSongList();
    cout << endl;

    retCode = removeSong("Time");
    cout << retCode << endl;

    showSongList();
    cout << endl;


}



this is the output i get:

Slot #1: Runaway by Bon Jovi, 1mb
Slot #2: by Elvis, 1mb
Slot # 3: Empty
Slot # 4: Empty
Slot # 5: Empty
Slot # 6: Empty
Slot # 7: Empty
Slot # 8: Empty

Last edited on
In addSong() the else in the while loop is wrong. You need to change this:
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
int addSong (string newTitle, string newArtist, int size)
{
    int memLeft = calcRemainingMem ();
    //declaring counting int i for while loop below
    int i = 0;

    if (size > memLeft)
        return -1;

    else if (newTitle == "")
        return -2;

    else if (newArtist == "")
        return -2;

    while (i < MAX_SONGS)
    {
        if (songs[i].size > 0)
            i++;
        else
        { // <---- Note
            songs[i].title = newTitle;
            songs[i].artist = newArtist;
            songs[i].size = size;
            return 0;
        } // <---- Note
    }

    if (i >= MAX_SONGS)
        return -2;
}
Sometimes it isn't necessary to use braces, when your block is a single statement for example,
1
2
if (x) 
    func();
but I recommend that you always use them by default,
1
2
3
4
if (x)
{
    func();
}
the kind of error shown above by coder777 happens a lot when someone hasn't used braces, and then later decides to expand the statement by adding more lines.
1
2
if (x)
    func();
could become
1
2
3
if (x)
    func();
    func2(); // oops, not part of if clause, func2() always called, regardless of value in x 


Whereas,
1
2
3
4
if (x)
{
    func();
}

would become
1
2
3
4
5
if (x)
{
    func();
    func2(); // mistake avoided, func2() only called when x == true
}
t
Yep. It was something as simple as braces. I woke up this morning and saw it before i even saw this thread. That's what happens after 6 hours of school, 7 hours of work, and 3 hours of coding without a decent break. -__-

Thanks for the help!
Topic archived. No new replies allowed.