[prog] using strings in C++
Wolf Rising
wolfrising at gmail.com
Mon Feb 21 01:05:05 EST 2005
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;
}
More information about the Programming
mailing list