[prog] using strings in C++

Wolf Rising wolfrising at gmail.com
Mon Feb 21 04:47:12 EST 2005


sorry, should have copied a bit more, already made that change:


int main()
{
	int cdCount = 0;
	const int LIBRARY_SIZE = 10;
	//string artist, title, genre;
	const int SIZE = 100;
	char artist[SIZE];
	char title[SIZE];
	char genre[SIZE];
	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.getline(artist,SIZE);
			//cin >> artist;
			cout << "Enter title: ";
			cin.getline(title,SIZE);
			//cin >> title;
			cout << "Enter genre: ";
			cin.getline(genre,SIZE);
			//cin >> genre;
			
			cdLibrary[cdCount].setCD(artist, title, genre, year);
			
			cdCount++;
			
		}


however in the class should I change all the strings to type char?


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;
	
};


On Sun, 20 Feb 2005 12:23:18 -0500, Marize Pommot-Maia
<marize at pommot.net> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Wolf Rising wrote:
> | Changed this part to use cin.getline, any ideas on how to get the
> | print out to work correctly?
> | Thank you !!
> |
> | 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.getline(artist,SIZE);
> |                       //cin >> artist;
> |                       cout << "Enter title: ";
> |                       cin.getline(title,SIZE);
> |                       //cin >> title;
> |                       cout << "Enter genre: ";
> |                       cin.getline(genre,SIZE);
> |                       //cin >> genre;
> |
> 
> Hi again,
> 
> OK, if you want to use "cin.getline" then the data type has to be
> changed to "char".  Here's a simple example of how it works:
> 
> #include<iostream>
> using namespace std;
> 
> int main() {
> ~  int SIZE=80;
> ~  char sentence[SIZE];
> 
> ~  cout << "Tell me something: " << endl;
> ~  cin.getline(sentence,SIZE);
> 
> ~  cout << "\t What you said: \n" << sentence << endl;
> 
> ~  return 0;
> }
> 
> Let me know if it solves your problem.
> 
> - --
> - ------------------------------------------------------------------------
>                                 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
> 
> iD8DBQFCGMeG4y2uvMn0AkcRAo4KAJ9QSFD3l3X/dPdJMkYlVMwFri7xJwCglu0K
> ubIARcVdrxuMF6bIqQyQy0k=
> =uXug
> -----END PGP SIGNATURE-----
>


More information about the Programming mailing list