Here I made some changes they are commented
Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
struct CompactDisc
{
string title;
string artist;
string year;
};
void welcome();
void menu();
void add(CompactDisc CDs[], int& count);
int listDetails(CompactDisc CDs[], int count);
int listTitles(CompactDisc CDs[], int count);
int saveToFile(CompactDisc CDs[], int count);
void welcome()
{
cout << left << setfill('*') << setw(15) << "*" << endl;
cout << "CD Librarian" << endl;
cout << left << setfill('*') << setw(15) << "*" << endl;
cout << endl;
}
int main()
{
welcome();
menu();
return 0;
}
void menu()
{
int count = 0;
bool exit = false;
CompactDisc CDs[20];
char num;
while(!exit)//Added a loop instead of recursive function calls
{
cout << "Here are your options:" << endl;
cout << "\t 1.) Add a CD to your collection." << endl;
cout << "\t 2.) List detailed information about your CDs." << endl;
cout << "\t 3.) List just the titles of the CDs in your collection."
<< endl;
cout << "\t 4.) Save detailed information about your CDs to a file."
<< endl;
cout << "\t 5.) Quit." << endl;
cout << "Enter a number: ";
cin >> num;
cin.ignore(80,'\n');//your cin>> is leaving a newline in the buffer that
//the getline is picking up
if (num == '1')
{
add(CDs, count);
}
else if (num == '2')
{
listDetails(CDs, count);
}
else if (num == '3')
{
listTitles(CDs, count);
}
else if (num == '4')
{
saveToFile(CDs, count);
}
else if (num == '5')
{
exit = true;
}
else
{
//add error checking
}
}
}
void add(CompactDisc CDs[], int &count)
{
cout << "Enter the album title: "<< endl;
getline(cin,CDs[count].title);
cout << "Enter the artist: "<< endl;
getline(cin,CDs[count].artist);
cout << "Enter the year released: "<< endl;
getline(cin,CDs[count].year);
count++;
cout << "Your CDs have been added to the database." << endl;
}
int listDetails(CompactDisc CDs[], int count)
{
int i;
cout << "The count is at: " << count << "." << endl;
cout << "Here is your list of CDs: " << endl;
for(i = 0; i < count; i++)
{
cout << CDs[i].title << " " << CDs[i].artist << " " << CDs[i].year << endl;
}
return 0;
}
int listTitles(CompactDisc CDs[], int count)
{
int i;
for(i = 0; i < count; i++)//only print out the stuff we have
{
cout << CDs[i].title << endl;
}
return 0;
}
int saveToFile(CompactDisc CDs[], int count)
{
int i;
ofstream fout;
fout.open("CDcollection.txt",ios::out);
for(i = 0; i < count; i++)//write all you cd info to the file
{
fout << CDs[i].title << " " << CDs[i].artist << " " << CDs[i].year << endl;
}
cout << "Your CD collection has been saved to your C drive." << endl;
fout.close();
return 0;
}