[prog] using strings in C++

Marize Pommot-Maia marize at pommot.net
Mon Feb 21 01:46:03 EST 2005


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Wolf Rising wrote:
| This is a project I've been working on for quite a while, the idea was
| to create a program that allows the user
| to catalog cds. At the moment, I'm only trying to get it to allow the
| user to enter 10 cds, and I would like
| the option of writing it to a text file (I'm not sure if that part
| works yet or not...)
|
| The program compiles and runs but there are at least two things I know
| of that aren't working correctly.
| First, if you have a entry with more than one word it fouls things up,
| for example if you enter just
| Beatles, it will prompt you correctly for the next line, enter The
| Beatles and it thinks you've
| entered two different fields. Also, at the end when it should display
| back what the user has entered,
| it displays a series of numbers. Any ideas, help, suggestions would be
| appreciated. Usual disclaimer,
| I'm not a student just trying to learn.
|
| Thanks!
|
| #include <iostream>
| #include <cstring>
| #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 artist, string title, string genre, int year);
| 	string get_artist() { return artist; }
| 	string get_title() { return title; }
| 	string get_genre() { return genre; }
| 	int get_year() { return year; }
| 	
| private:
| 	string artist;
| 	string title;
| 	int year;
| 	string genre;
| 	
| };
|
| //*********
| cd::cd(const cd&){
| 	
| 	char *artist = "";
| 	char *title = "";
| 	char *genre = "";
| 	int 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 << " " << genre << " " << title << " " << year << endl;
| }
|
| //*********
|
| 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; i < length; i++) {
| 		fout << cdLibrary[i].get_artist() << cdLibrary[i].get_title() <<
| cdLibrary[i].get_genre() << cdLibrary[i].get_year();
| 		
| 	}
| }
|
| //*********
|
| void printCDLibrary(cd cdLibrary[], int length) {
| 	
| 	
| 	for (int i; i < length; i++) {
| 		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);
|     }
| }
|
| //*********	
|
| void cd::setCD (string a, string t, string g, int y)
| {
| 	artist = a;
| 	title = t;
| 	genre = g;
| 	year = y;
| }
|
|
| //*********
|
|
|
| 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: (-1 to quit)" << endl;
| 			cout << "Enter year: ";
| 			cin >> year;
| 			if (year == -1) { break; }
| 			cout << "Enter artist: ";
| 			cin >> artist;
| 			cout << "Enter title: ";
| 			cin >> title;
| 			cout << "Enter genre: ";
| 			cin >> genre;
| 			
| 			cdLibrary[cdCount].setCD(artist, title, genre, year);
| 			
| 			cdCount++;
| 		}
| 	}
| 	
| 	saveCDLibrary(cdLibrary, LIBRARY_SIZE);
| 	printCDLibrary(cdLibrary, LIBRARY_SIZE);
| 	
| 	
| 	return 0;
| }
| _______________________________________________
| Programming mailing list
| Programming at linuxchix.org
| http://mailman.linuxchix.org/mailman/listinfo/programming
|
|

I took a quick look at your program.  You're using "cin" to input the
string.  However, "cin" reads characters until a space, newline, tab or
EOF indicator is found.  When you want to get an entire line of text you
could use the function "cin.getline".  Since you mentioned you wanted to
learn, I leave to you finding out more about it.  Another way to deal
with this would be by overloading the operator "cin" so it can read the
entire string.

- --
- ------------------------------------------------------------------------
				Marize Pommot-Maia
				marize at pommot.net

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCGKKr4y2uvMn0AkcRApw6AKCzmJXJN8LWYM1qH13Y0eoQxYsOKACghw0w
D5BwNN4ZXCJg/x9RxrJmu18=
=WN61
-----END PGP SIGNATURE-----


More information about the Programming mailing list