Hi guys, this program is suppose to ask user input for up to 10 book titles and date published using classes. However im having trouble with a certain part of the output. My coding as follows

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class bookInput
{
private: 
		 string title;
		 double date;
public:
	
	void setTitle(string);
	void setDate(double);
	void displaybook();
};


void bookInput::setTitle(string booktitle)
{
	title = booktitle;
}
void bookInput::setDate(double pDate)
{
	date = pDate;
}
void bookInput::displaybook()
{
	cout<< "\tName:"<< title << " \t\t\tDate Published: " << date<< endl;
	

}


void printbook();


int main()
{
	char selection =0;
	char ans;
		do
		{
			system("cls");
			cout<< "1. Insert booktitle and dates"<<endl;
			cout<< "2. Exit"<<endl;
			cout<<" Please enter selection:"<<endl;
			cin>>selection;

			if(selection=='1')
				printbook();
			else if(selection=='2')
			{
			cout<<"Exiting program"<<endl;
			system("pause");
			return 0;
			}
			else
				cout<<"Invalid selection"<<endl;
			cout<<"Do you wish to continue? Please enter Y/N"<<endl;
				cin>>ans;

		}while((ans!='N')&& (ans!='n'));
		return 0;


	
	system("pause");
return 0;
}


void printbook()
{
	const int MAX=3;
	bookInput myFav[MAX];
	int x;
	
	string Title,mystr;
	double date;
	
	for(x=0; x < MAX; ++x)
	{
		cout<< "Enter book title#" << (x+1) << " ";
		getline (cin, Title);

		cout << " Enter the published date" ;
		 getline (cin, mystr);
		stringstream(mystr) >> date;
		
		myFav[x].setTitle(Title);
		myFav[x].setDate(date);
		
	}
	cout<<endl<< " Book list:"<<endl;
	for (x=0; x<MAX; ++x)
		myFav[x].displaybook();
	
	
	
	
}

My trouble is with the first book title input. It prints out both booktitle and date published on the same line. It doesnt happen for the rest of the titles though. any help is much appreciated

secondly, i would like to implement a calendar class for the input of the dates. Can anyone guide me in the direction? thanks

Recommended Answers

All 2 Replies

My trouble is with the first book title input. It prints out both booktitle and date published on the same line. It doesnt happen for the rest of the titles though.

For questions like this, I would attempt to narrow down the problem as far as possible. That is, rather than show us 100 lines, probably only 3 of which have anything to do with this, create a 10 line compilable example which demonstrates the problem.

secondly, i would like to implement a calendar class for the input of the dates. Can anyone guide me in the direction?

This is tricky. To do it well is very complicated. I'd strongly suggest using a library that already has such functionality. Boost is sometimes a bear to work with, but here is where you should start: http://www.boost.org/doc/libs/1_43_0/doc/html/date_time.html

If you get it to work (or anyone else knows how to use it), it would be great if you could add a reasonable example to the "Boost" section here:
http://programmingexamples.net/index.php?title=CPP#Boost

Good luck.

David

void bookInput::displaybook() {
cout<< "\tName:"<< title << " \t\t\tDate Published: " << date<< endl;
}

Why are you only inserting tabs? Maybe it only goes to a newline because it ran out of space on the right side. Just insert a newline if you want a new line.

void bookInput::displaybook() {
cout<< "\nName:"<< title << " \nDate Published: " << date<< endl;
}

Assuming this is windows.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.