[prog] Re: C++
wolf
wolf at wolfrising.net
Mon Mar 15 19:46:43 EST 2004
To everyone who helped me out, thank you! here's a version that works
for anyone
curious who followed the adventure along : ) mind you, it still needs a
few improvements
but it works : )
Thanks!!
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class cd {
public:
cd();
cd(const cd&);
cd(string artist, string title, string genre, int year);
void showCD();
void setCD(string cartist, string ctitle, string cgenre, int
iyear);
string getartist();
string gettitle();
string getgenre();
int getyear();
private:
string artist;
string title;
int year;
string genre;
};
//*********
cd::cd()
{
artist = "";
title = "";
genre = "";
year = 0;
}
cd::cd (string sartist, string stitle, string sgenre, int iyear) {
// create a new cd
artist = sartist;
title = stitle;
genre = sgenre;
year = iyear;
}
//*********
void cd::showCD() {
// show a cd
cout << artist << " " << title << " " << genre << " " << year <<
endl;
}
//*********
string cd::getartist()
{
return artist;
}
string cd::gettitle()
{
return title;
}
string cd::getgenre()
{
return genre;
}
int cd::getyear()
{
return year;
}
//*********
void cd::setCD(string cartist, string ctitle, string cgenre, int iyear)
{
artist = cartist;
title = ctitle;
genre = cgenre;
year = iyear;
}
void saveCDLibrary(cd cdLibrary[], int length) {
// need to write to a file?
ofstream fout("cdinfo.dat", ios::out); // Create a file
//called "cdinfo.dat" for output (writing)
if (!fout) { // unable to create file
cerr << "Failed to open output file";
exit(1);
}
for (int i=0; i < length; i++) {
fout << cdLibrary[i].getartist() << "," <<
cdLibrary[i].gettitle() << "," << cdLibrary[i].getgenre() << "," <<
cdLibrary[i].getyear() << "," << endl;
}
}
//*********
void printCDLibrary(cd cdLibrary[], int length) {
// This maybe needs sorted, so it would be
alphabetical??
// no idea how to do that
for (int i = 0; i < length; i++) {
cdLibrary[i].showCD();
//cout << cdLibrary[i].showCD();
}
}
//*********
void readCDLibrary(cd cdLibrary[], int length) {
string input, artist, title, genre;
int year;
// Open the file
ifstream fin("cdinfo.dat", ios::in);
if (!fin) { // unable to open data file
cerr << "File could not be opened";
exit(2);
}
int i=0;
while ( fin >> artist >> title >> genre >> year ) {
cdLibrary[i++].setCD(artist, title, genre, year);
}
}
//*********
int main()
{
int cdCount = 0;
const int LIBRARY_SIZE = 10;
string artist, title, genre;
int year;
cd cdLibrary[LIBRARY_SIZE];
bool ans;
// Ask if they want to read the CDs from a file or
enter new
// data:
cout << "Read from file (1) or Enter new data (0)? ";
cin >> ans;
if (ans) {
// read from the file?
readCDLibrary(cdLibrary, LIBRARY_SIZE);
} else {
// read in information about CDs
while (cdCount < LIBRARY_SIZE) {
cout << "Enter CD Information:" << endl;
cout << "Enter artist: ";
cin.getline(artist, 256);
cout << "Enter title: ";
cin.getline(title, 256);
cout << "Enter genre: ";
cin.getline(genre, 256);
// cout <<"\n";
cout << "Enter year: (0 to quit)";
cin >> year;
if (year == 0) { break; }
cout << "\n";
cdLibrary[cdCount].setCD(artist, title,
genre, year);
cdCount++;
}
}
saveCDLibrary(cdLibrary, LIBRARY_SIZE);
printCDLibrary(cdLibrary, LIBRARY_SIZE);
return 0;
}
//*********
More information about the Programming
mailing list