Little help with my program

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.

I use Microsoft Visual C++ 6.0

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

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





Last edited on
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.

Thank you very match "lastchance". The problem is solved. The program work fine.
Topic archived. No new replies allowed.