C++ Structure Issue

I'm having some issue with my program. When I select the first option (to add a song), the program crashes after the last input. I believe it's not incrementing properly.
If anyone could take a look at the code to provide any assistance, I would be very grateful.
Thank you for your time.

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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

// Song menu structure
struct SongData
{
   string SongTitle;    // Song Title
   string SongArtist;   // Song Artist
   string SongCategory; // Song Category
   string SongLength;   // Song Length
};

// Function prototypes
void SongMenu(SongData[], int, int&);
void InputFromFile(SongData[], int&);
void DisplayInfo(SongData[], int&);
void TitleSortArray(SongData[], int&);
void ArtistSortArray(SongData[], int&);
string AddSong(SongData[], int, int&);
string DeleteSong(SongData[], int);

// Main Function
int main()
{  
   int size = 6;
   int ListSize = 50;
   SongData SongList[ListSize];
   InputFromFile(SongList, size);
   SongMenu(SongList,ListSize, size);
   cout << "\nNormal job termination\n" << endl;
   system("pause");
   return 0;
}

// Displays the song menu
void SongMenu(SongData SongList[], int ListSize, int& size)
{
   int choice;
   
   do
   {
      Display();
      cout << "Song List Menu" << endl;
      cout << "--------------" << endl;
      cout << "1 - Add a song";
      cout << endl;
      cout << "2 - Delete a song";
      cout << endl;
      cout << "3 - Displays the song list in ascending title order";
      cout << endl;
      cout << "4 - Displays the song list in ascending artist order";
      cout << endl;
      cout << "5 - Exit";
      cout << endl << endl << "Choice: ";
      cin >> choice;
      cout << endl;
   
      switch (choice)
      {
         case 1:
	        AddSong(SongList, ListSize, size);
	        //TitleSortArray(SongList, size);
	        //ArtistSortArray(SongList, size);
		    DisplayInfo(SongList, size);
		    break;
	     case 2:
            DeleteSong(SongList, ListSize);
		    break;
	     case 3:
            TitleSortArray(SongList, size);
		    DisplayInfo(SongList, size);
		    break;
	     case 4:
            ArtistSortArray(SongList, size);
		    DisplayInfo(SongList, size);
		    break;
	     case 5:
            cout << "\nNormal job termination\n" << endl;
            system("pause");
            exit(5);                 
	     default:
            cout << "You entered an incorrect choice.";
            break;
      }
   }while(choice != 5);
}

// Functon that inputs the periodic table data from a file into the program
void InputFromFile(SongData SongList[], int& size)
{   
   int i = 0;
   fstream inputFile;
   inputFile.open("c:\\songlist.txt");
   
   for (i = 0; i < size; i++)
   {
      
      inputFile >> SongList[i].SongTitle;
      inputFile >> SongList[i].SongArtist;
      inputFile >> SongList[i].SongCategory;
      inputFile >> SongList[i].SongLength;
   }
}

// Displays the search functions return's properties 
void DisplayInfo(SongData SongList[], int& size)
{
   for (int i = 0; i < size; i++)
   {
      cout << "Title: " << SongList[i].SongTitle << endl;
      cout << "Artist: " << SongList[i].SongArtist << endl;
      cout << "Category: " << SongList[i].SongCategory << endl;
      cout << "Length: " << SongList[i].SongLength << endl;
      cout << endl;
   }
}

// This function sorts the song list by the title
void TitleSortArray(SongData SongList[], int& size)
{   
   string temp, temp2, temp3, temp4;
      
   for (int i = size - 1; i >= 0; i--)
   {
       for (int count = 0; count < i; count++)
       {
          if (SongList[count].SongTitle > SongList[count + 1].SongTitle)
          {
             temp = SongList[count].SongTitle;
             temp2 = SongList[count].SongArtist;
             temp3 = SongList[count].SongCategory;
             temp4 = SongList[count].SongLength;
             SongList[count].SongTitle = SongList[count + 1].SongTitle;
             SongList[count].SongArtist = SongList[count + 1].SongArtist;
             SongList[count].SongCategory = SongList[count + 1].SongCategory;
             SongList[count].SongLength = SongList[count + 1].SongLength;             
             SongList[count + 1].SongTitle = temp;
             SongList[count + 1].SongArtist = temp2;
             SongList[count + 1].SongCategory = temp3;
             SongList[count + 1].SongLength = temp4;
          }
       }
   }
   cout << "Ascending Title Order:" << endl << endl;
}

// This function sorts the song list by the artist
void ArtistSortArray(SongData SongList[], int& size)
{   
   string temp, temp2, temp3, temp4;
   
   for (int i = size - 1; i >= 0; i--)
   {
       for (int count = 0; count < i; count++)
       {
          if (SongList[count].SongArtist > SongList[count + 1].SongArtist)
          {
             temp = SongList[count].SongTitle;
             temp2 = SongList[count].SongArtist;
             temp3 = SongList[count].SongCategory;
             temp4 = SongList[count].SongLength;
             SongList[count].SongTitle = SongList[count + 1].SongTitle;
             SongList[count].SongArtist = SongList[count + 1].SongArtist;
             SongList[count].SongCategory = SongList[count + 1].SongCategory;
             SongList[count].SongLength = SongList[count + 1].SongLength;             
             SongList[count + 1].SongTitle = temp;
             SongList[count + 1].SongArtist = temp2;
             SongList[count + 1].SongCategory = temp3;
             SongList[count + 1].SongLength = temp4;
          }
       }
   }   
   cout << "Ascending Artist Order:" << endl << endl;
}

// This function adds a new song in the song list 
string AddSong(SongData SongList[], int ListSize, int& size)
{
   ListSize = size;
   cout << endl;
   cout << "Enter song title: ";          // Song Title
   cin >> SongList[ListSize].SongTitle;   
   cout << "Enter song artist name: ";    // Song Artist
   cin >> SongList[ListSize].SongArtist;   
   cout << "Enter song category: ";       // Song Cateogry
   cin >> SongList[ListSize].SongCategory;   
   cout << "Enter song length: ";         // Song Length
   cin >> SongList[ListSize].SongLength;   
   cout << endl;
   ListSize++;
}

// This functions deletes a song from the song list
string DeleteSong(SongData SongList[], int ListSize)
{
   int i = 0;  
   cout << "Enter the song to delete: ";
   string DeleteSong = SongList[ListSize].SongTitle;
   cin >> DeleteSong;   
   
   while (DeleteSong != SongList[i].SongTitle && i < ListSize)
   {
      i++;
   }
               
   if (i < ListSize)                       
   {
      SongList[i] = SongList[ListSize - 1];
      ListSize--;                        
   }              
}
You're passing ListSize by value.

The ListSize in main isn't being incremented. A copy of it is (which then immediately goes out of scope).

You need to pass it by reference.

For that matter, the same should be done when you pass it to SongMenu.
1
2
   int ListSize = 50;
   SongData SongList[ListSize];
illegal, array size must be constant.

Compile with warnings (and gcc extension)
In function ‘std::string AddSong(SongData*, int, int&)’:
   warning: no return statement in function returning non-void [-Wreturn-type]
std::string DeleteSong(SongData*, int)’:
   warning: no return statement in function returning non-void [-Wreturn-type]
That's causing the crash, the string destructor tries to delete an invalid pointer.

However, you've got logic errors in your function
Make a desk test
My desk at work has a Pacman stress ball that chomps on my keyboard.

I guess it's Pacman debugging in my case, then.
Last edited on
Topic archived. No new replies allowed.