Issue with adding a new record to an array

I'm having some issues with adding a record to my array.
If anyone could take a look at the AddSong function to provide assistance, that would be appreciated.
Thanks 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
#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);
void InputFromFile(SongData[], int);
void DisplayInfo(SongData[], int);
void TitleSortArray(SongData[], int);
void ArtistSortArray(SongData[], int);
void RemoveUnderscores(SongData[], int);
string AddSong(SongData[], int);

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

// Displays the song menu
void SongMenu(SongData SongList[], int size)
{
   
   int choice;
   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, size);
	 DisplayInfo(SongList, size);
	 break;
      case 2:
         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;
   } 
}

// 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;
      
   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;
             SongList[count].SongTitle = SongList[count + 1].SongTitle;
             SongList[count + 1].SongTitle = temp;
          }
       }
   }
   cout << "Ascending Title Order:" << endl;
}

// This function sorts the song list by the artist
void ArtistSortArray(SongData SongList[], int size)
{ 
   
   string temp;
   
   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].SongArtist;
             SongList[count].SongArtist = SongList[count + 1].SongArtist;
             SongList[count + 1].SongArtist = temp;
          }
       }
   }   
   cout << "Ascending Artist Order:" << endl;
}

// This function adds a new song in the list 
string AddSong(SongData SongList[], int size)
{
   /*
   SongData SongList;

   cout << endl;
   cout << "Enter song title: ";          // Song Title
   cin  >> SongList.SongTitle;
   cout << "Enter song artist name: ";    // Song Artist
   cin  >> SongList.SongArtist;
   cout << "Enter song category: ";       // Song Cateogry
   cin  >> SongList.SongCategory; 
   cout << "Enter song length: ";         // Song Length
   cin  >> SongList.SongLength;
   cout << endl;
   
   //return SongList;
   
   // Increases the size of the array  
   size++;
   */
}

// This function removes the underscores.
void RemoveUnderscores(SongData SongList[], int size)
{    
    for (int count = 0; count < size; count++)
    {
       if (SongList[count].SongTitle == "_")
       {
          SongList[count].SongTitle == " ";
       }
    }
}
SongList[] is an array - did you mean to do this :

1
2
3
4
5
6
7
8
9
10
11
string AddSong(SongData SongList[], int Position)
{
   
   SongData SongList; // not needed - it is already there as an argument

   cout << endl;
   cout << "Enter song title: ";          // Song Title
   cin  >> SongList[Position].SongTitle;

//similar code here


LIne 174 does not increase the size of the array - array size is constant and known at compile time, and the size variable is local to the function - goes out of scope when the function ends.

If you want an automatically increase-able container use the STL <vector> or <list> . Check out the reference section top left of this page.

Your function starting line 179, doesn't do what you think - it only works if the entire title is "_" - did you mean to check each character of the title? If so use one of the algorithms to do so.

Your sort functions don't sort - they only look at the next item in the array. Google sort algorithms for info on that.

With the SongMenu function, you could have it loop, so the user gets another go at input of the menu choice if they put in something invalid earlier:

1
2
3
4
5
6
7
8
9
10
bool Quit = false;

while (!Quit) {
//your code here
//your switch code

case 5:
     Quit = true;

} 


HTH
A description of the problem would be helpful.

I can see two problems:
- SongList is an array. That means SongList.SongTitle, SongList.SongArtist and the lot do not exist. You have to provide an index first. If I'm guessing correctly, you need to add to your code a count of the number of songs entered by the user.
- You retrieve user input using operator>>. This is a bad idea, because this operator only reads one word at a time. You should use getline instead.
Topic archived. No new replies allowed.