This is my first program. It work fine, but I need someone help me with "case 5" in code. My goal is when I enter more of one object (books) in the library and select "display data for a particular author" in Menu, the program to show me all books of that author. Now it show me only one book although I enter more of one books of that author. I must use only function in my code without constuctor and destructors.
// Library.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream.h>
#include <string.h>
class Library
{
public:
void getData();
void changeData();
void Display();
void Authorbooks();
char* getName();
private:
char authorname[20];
char booktitle[40];
char press[20];
int editingbook;
};
void Library::getData()
{
cout<<"Please insert author's name"<<endl;
cin>>authorname;
cout<<"Please insert book title"<<endl;
cin>>booktitle;
cout<<"Please insert year of publish"<<endl;
cin>>editingbook;
cout<<"Please insert publisher's name"<<endl;
cin>>press;
}
void Library::changeData()
{
cout<<"Please insert author's name"<<endl;
cin>>authorname;
cout<<"Please insert book title"<<endl;
cin>>booktitle;
cout<<"Please insert year of publish"<<endl;
cin>>editingbook;
cout<<"Please insert publisher's name"<<endl;
cin>>press;
}
void Library::Display()
{
cout<<endl;
cout<<"Book title: "<<booktitle<<endl;
cout<<"Author's name: "<<authorname<<endl;
cout<<"Publication date: "<<editingbook<<endl;
cout<<"Publisher: "<<press<<endl;
}
void Library::Authorbooks()
{
cout<<booktitle<<endl;
}
char* Library::getName()
{
return authorname;
}
void main()
{
Library books[400];
int number=0;
int a;
do
{
cout<<endl;
cout<<"Please press 1 to Enter data for a new book:"<<endl;
cout<<"Please press 2 to Search data for a particular book:"<<endl;
cout<<"Please press 3 to Update the data for a particular book:"<<endl;
cout<<"Please press 4 to Display books volume:"<<endl;
cout<<"Please press 5 to Display data for a particular author:"<<endl;
cout<<"Please press 6 for Exit:"<<endl;
cin>>a;
cout<<endl;
switch(a)
{
case 1:
{
books[number].getData();
number++;
break;
}
case 2:
{
int b;
cout<<"Please inser book number between 1 to "<<number<<endl;
cin>>b;
books[b-1].Display();
break;
}
case 3:
{
int c;
cout<<"Please inser book number between 1 to "<<number<<endl;
cin>>c;
books[c-1].changeData();
break;
}
case 4:
{
int result=400-number;
cout<<"You have "<<number<<" books in yours library"<<endl;
cout<<"You can enter only: "<<result<<" books"<<endl;
break;
}
case 5:
{
char copyname[20];
cout<<"Enter author name"<<endl;
cin>>copyname;
for(int i=0;i<number;i++)
{
if(strcmp(books[i].getName(),copyname)==0)
{
cout<<endl;
cout<<"Author's books are:"<<endl;
books[i].Authorbooks();
break;
}
else
cout<<"In the library not books from this author!"<<endl;
}
}
}
}
while(a<6);
}
As soon as you find the author's book you will (in line 140) break out of the for loop and finish this case. So you will find at most 1 book by this author.
Also, until you find a book by this author it will keep telling you there are no books by this author in the library.
Maybe line 140 should be used to increment the number of books by this author. The else statement should go outside the for loop and be an if statement operating only if the number of books by this author was still 0.