Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Ok I have to build a CD Catalog with C++ for school and I got this one but it does not work. I have literally search the internet for hours and have found no codes for a cd catalog,organizers or whatever name you want to call it. SO if can either help with this code or show me a code that works. I would very much be thankful .
#include <fstream.h>
#include <stdlib.h>
#include <iomanip.h>
#include <apstring.h>
//#include “apstring.h”struct CD
{
apstring name,
artist;
int numberOfTracks;
double value;
};CD temp;
const apstring EXIT = “exit”;
const int WIDTH = 28;//PROTOTYPES
void Print_Menu(int);
int numberOfCDs(int);
void Print_CD();
void Add_CD(int &);
void View_CDDIR();// Make this function later, when I know how to without doubt.
//void Delete_CD();int main()
{
int numCDs;
apstring choice;
do
{
system("CLS");
numCDs = numberOfCDs(numCDs);Print_Menu(numCDs);
getline(cin, choice);if(choice != EXIT)
{
switch(choice)
{
case 'p': case 'P': Print_CD(); break;
case 'a': case 'A': Add_CD(numCDs); break;
case 'v': case 'V': View_CDDIR(); break;
default: break;
}
}// end if
}
while(choice != EXIT);
return 0;
}void Print_Menu(int numCDs)
{
cout << “You currently have " << numCDs << " CD(s) in your archive.\n”;
cout << “Enter 'exit' to quit program.\n\n”;
cout.setf(ios::left);cout << setw(WIDTH) << "Print an old CD" << "P" << endl;
cout << setw(WIDTH) << "Add a new CD" << "A" << endl;
cout << setw(WIDTH) << "View the CD Archive" << "V" << endl;
// I also need to include a DELETE function
cout << "\nEnter your selection: ";
}void Print_CD()
{
apstring fileToRead;
ifstream ReadFile;
cout << "\nPlease enter the name of the CD to view: ";
getline(cin,fileToRead);
ReadFile.open(fileToRead,ios::in); // open file for user to view
//Initialize cd temp2 and print it's contentcout.setf(ios::left); // left justify output
CD temp2; // declare the new temporary cd spacifically for viewing
if(ReadFile)
{
//initialize
ReadFile.getline(temp2.name,41);
ReadFile.getline(temp2.artist,41);
ReadFile >> temp2.numberOfTracks;
ReadFile >> temp2.value;
cout<<setw(WIDTH)<<"CD Name:"<<temp2.name<<endl;
cout<<setw(WIDTH)<<"CD Artist:"<<temp2.artist<<endl;
cout<<setw(WIDTH)<<"Number of Tracks:"<<temp2.numberOfTracks<<endl;
cout<<setw(WIDTH)<<"Value of CD:"<<"$"<<temp2.value;cin.ignore(80,'\n');
cin.get();
cin.ignore(80,'\n');
}
else
cout << "The file by that name does not exist.";
cin.ignore(80,'\n');
cin.get();
cin.ignore(80,'\n');
}void Add_CD(int & numCDs)
{
apstring outputfilename;
ofstream outfile;
cout << "\nEnter the CD name: ";
getline(cin,temp.name);
//initialize the CD name to the name of the file
temp.name = outputfilename;
//open the new file for output
outfile.open(outputfilename, ios::out);
if(outfile)
{
outfile.unsetf(ios::skipws);cout << "Enter the artist's name: ";
getline(cin,temp.artist);
cout << "Enter the number of tracks: ";
cin >> temp.numberOfTracks;
cout << "Enter the value of the CD: $";
cin >> temp.value;
outfile << temp.name << endl
<< temp.artist << endl
<< temp.numberOfTracks << endl
<< temp.value << endl << endl;
outfile.close();
cout << “File was sucksuccessfully added to the drive.”;
cin.ignore(80,'\n');
cin.get();
cin.ignore(80,'\n');
}
else
{
cout << "There was an error creating the file.\n";
cin.ignore(80,'\n');
cin.get();
cin.ignore(80,'\n');
}
//////////////////////////////////////////////////////////////////////////
//INCREMENT THE TOTAL NUMBER OF CD'S IN ARCHIVE BY ADDING LATEST CD NAME
ofstream appfile;
appfile.open("cddir",ios::app);if (!appfile)
cout << "There was an error opening 'cddir' file.\n";
else
{
appfile << temp.name;
appfile.close();
}
}//end of function add_cd-----------------------------------------------void View_CDDIR()
{
ifstream infile;
apstring content3;infile.open("cddir",ios::in);
cout << endl;
do
{
getline(infile,content3);
cout << content3 <<endl;
}
while(!infile.eof());
infile.close();cin.ignore(80,'\n');
cin.get();
cin.ignore(80,'\n');
}
int numberOfCDs(int numCDs)
{
ifstream infile;
infile.open("cddir",ios::in);
apstring content;
numCDs = 0;if(infile)
{
do
{
getline(infile,content);
if(!infile.eof())
numCDs++;
}
while(!infile.eof());
infile.close();
}
else
{
cout << "The file 'cddir' is not present!\nOne will be created for you.\n";
ofstream createfile;
createfile.open("cddir",ios::out);
if(createfile)
{
cout << "\nThe file has been created and there is nothing in it.\n\n";
createfile.close();
}
else
{
cout << "\nUnable to create file 'cddir'\n\n";
}
}
return numCDs;
that the code.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |