I verified the input successful and correct into the database array. However, I am not sure why the first two members of the structure are incorrect while the last one, the track, is correct;
Code:
struct music database[BUFSIZ];
void populate(char *title, char *artist, int track, int index)
{
database[index].title = title;
database[index].artist = artist;
database[index].track = track;
}
void dbDump(int index)
{
using namespace std;
int i;
for (i = 0; i < index; i++)
{
cout << database[i].title << endl;
cout << database[i].artist << endl;
cout << database[i].track << endl;
}
}
void musicDb(int index)
{
using namespace std;
clearBuffer();
cout << "Title: ";
char title[BUFSIZ];
cin.getline(title, sizeof(title));
cout << "Artist: ";
char artist[BUFSIZ];
cin.getline(artist, sizeof(artist));
cout << "Track: ";
int track;
cin >> track;
populate(title, artist, track, index);
}
int music()
{
using namespace std;
bool run = true;
int index = 0;
while (run)
{
musicDb(index);
decide: cout << "Another entry (y or n)? ";
char decision;
cin >> decision;
if (decision - '0' >= 0 && decision - '0' <= 9)
{
cout << "Invalid entry!" << endl;
goto decide;
}
if (decision == 'n')
{
run = false;
}
else if (decision != 'y')
{
cout << "Invalid entry!" << endl;
goto decide;
}
index++;
}
dbDump(index);
return 0;
}
This program is for kicks and giggles as I learn C++. It is interesting to note that if only one entry is made, I can dump it fine. If more than one entry is made, it does not dump correctly;