[prog] Re: C++

wolf wolf at wolfrising.net
Mon Mar 15 03:35:11 EST 2004


Hi, one last attempt before the end of the weekend and I have to make 
sure to sleep : )
Thank you to everyone who responded and offered assistance/suggestions, 
I very
much appreciate it : )

This version (of which I've had a massive amount of help with) does 
compile but it doesn't
run correctly, something is wrong with the I/O stuff.  anyone happen to 
see the error?
when you attempt to run the program
this prints to the screen:

Read from file (1) or Enter new data (0)? 0
Enter CD Information:
Enter year: (0 to quit)
In saveCDLibrary
in showCD
in showCD
in showCD
in showCD
in showCD
in showCD
in showCD
in showCD
in showCD
in showCD
In printCDLibrary

FINAL has exited with status 0.


here's the current version:
#include <iostream>
#include <fstream>
//#include <cstring>
#include <string>

using namespace std;

class cd {
private:
	string artist;
	string title;
	int year;
	string genre;
	
public:
	cd() {}
     cd(string artist, string title, string genre, int year);
	cd(const cd& cp);
	cd& operator=(const cd& newCD){
		artist = newCD.artist;
		title = newCD.title;
		genre = newCD.genre;
		year = newCD.year;
		return *this;
	}
     void showCD(ostream &outStream, char sepChar);
};


//*********

cd::cd(string sartist, string stitle, string sgenre, int iyear) {
	// create a new cd object
	artist = sartist;
	title = stitle;
	genre = sgenre;
	year = iyear;
}

cd::cd(const cd& copy) {
	artist = copy.artist;
	title = copy.title;
	genre = copy.genre;
	year = copy.year;
}

//*********
void cd::showCD(ostream &outStream, char sepChar) {
	cerr << "in showCD\n";
	// print a cd object to an output stream
	outStream << artist << sepChar << genre << sepChar << title << sepChar 
<< year << endl;
}

//*********
void saveCDLibrary(cd cdLibrary[], int length) {
	cerr << "In saveCDLibrary\n";
	ofstream outputFS;
	outputFS.open("cdLibraryFile.txt");
	for (int i; i < length; i++) {
		cdLibrary[i].showCD(outputFS, '|');
	}
	outputFS.close();
}

//*********
void printCDLibrary(cd cdLibrary[], int length) {
	cerr << "In printCDLibrary\n";
		
	for (int i; i < length; i++) {
		cdLibrary[i].showCD(cout, ' ');
	}
}

//*********

void readCDLibrary(cd cdLibrary[], int length) {
	cerr << "In readCDLibrary\n";
	string line;
	int pipePos[3];
	ifstream inputFS;
	
	inputFS.open("cdLibrary.txt");
	for (int i = 0; i < length; i++) {
		getline(inputFS, line);
		cerr << line << endl;
		
				int lastPipe = 0;
		for (int j = 0; j < 3; j++) {
			pipePos[i] = line.find("|", lastPipe);
			lastPipe = pipePos[i];
		}
		
		string artist = line.substr(0, pipePos[0] - 1);
		string title  = line.substr(pipePos[0] + 1, pipePos[1] - 1);
		string genre  = line.substr(pipePos[1] + 1, pipePos[2] - 1);
		string year   = line.substr(pipePos[2] + 1);
		int yint = atoi(year.c_str());
		
		// artist, title, genre, year
		cd tmpCD(artist, title, genre, yint);
		cdLibrary[i] = tmpCD;
	}
	inputFS.close();
}
//*********

//*********begin main
int main()
{
	int cdCount = 0;
	const int LIBRARY_SIZE = 10;
	string artist, title, genre, year;
	int ans;
	cd cdLibrary[LIBRARY_SIZE];
	
	// 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 year: (0 to quit)" << endl;
			getline(cin, year);
			int yint = atoi(year.c_str());
			if (yint == 0) { break; }
			cout << "Enter artist: " << endl;
			getline(cin, artist);
			cout << "Enter title: " << endl;
			getline(cin, title);
			cout << "Enter genre: " << endl;
			getline(cin, genre);
			
			cd tmpCD(artist, title, genre, yint);
			cdLibrary[cdCount] =  tmpCD;
			cdCount++;
		}
	}
	
	saveCDLibrary(cdLibrary, LIBRARY_SIZE);
	printCDLibrary(cdLibrary, LIBRARY_SIZE);
	
	return 0;
}

//*********end



More information about the Programming mailing list