Why is the incorrect output being displayed?

Here's the program:
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
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>


struct BOOK
{
	int Bno;
	char Bname[20];
	char Author[20];
	float Price;
};


void EnterDetails(BOOK A[], int);
void DisplayDetails(BOOK A[], int);
void SearchByBno(BOOK A[], int);
void SearchByBname(BOOK A[], int);

void main()
{
	clrscr();
	int n;
	BOOK B[20];
	cout<<"Enter the number of different books you would be entering: ";
	cin>>n;
	cout<<endl<<endl;
	EnterDetails(B,n);
	DisplayDetails(B,n);
	SearchByBno(B,n);
	SearchByBname(B,n);
	getch();
}

void EnterDetails(BOOK A[], int N)
{

	for(int i=0; i<N; i++)
	{
		cout<<"Enter the details of book "<<i+1<<":\n\n";
		cout<<"Enter book number: ";
		cin>>A[i].Bno;
		cout<<"Enter book name: ";
		gets(A[i].Bname);
		cout<<"Enter Author of the book: ";
		gets(A[i].Author);
		cout<<"Enter price of the book in $: ";
		cin>>A[i].Price;
		cout<<"\n\n";
	}
	cout<<"\n\n";
}
void DisplayDetails(BOOK A[], int N)
{
	for(int i=0; i<N; i++)
	{
		cout<<"Details of book "<<i+1<<" "<<"are:\n\n";

		{
			cout<<"Book number: ";cout<<A[i].Bno<<"\n";

			cout<<"Book name: ";cout<<A[i].Bname<<"\n";

			cout<<"Author: ";cout<<A[i].Author<<"\n";

			cout<<"Price: "<<A[i].Price<<"\n\n";

		}
	}
	cout<<"\n\n";
}

void SearchByBno(BOOK A[], int N)
{

	int a, found=0;
	cout<<"Enter book number of the book you are looking for: ";
	cin>>a;
	for(int i=0; i<N, (!found);i++)
	{
		if(A[i].Bno==a)
		 found++;
	}
	if(!found)
		cout<<"No book matches this book number.";
	else if(found==1)
	{
		cout<<"The details of the book of book number \""<<a<<"\" are:\n\n";
	    cout<<"Name: "<<A[i].Bname<<"\n";
		cout<<"Author: "<<A[i].Author<<"\n";
		cout<<"Price: "<<A[i].Price<<"\n";
	}
	cout<<"\n\n";
}


void SearchByBname(BOOK A[], int N)
{
    int found=0;
	char Name[20];
	cout<<"Enter the name of the book you are looking for: ";
	gets(Name);

	for(int i=0; i<N, (!found); i++)
	{
		if(!(strcmpi(A[i].Bname,Name)))
			found++;
	}
	if(!found)
		cout<<"The name of the book you entered is not present.";
	else if(found==1)
	{
		cout<<"The details of the book of the name\""<<Name<<"\" are:\n\n";
		cout<<"Book number: "<<A[i].Bno<<"\n";
		cout<<"Author: "<<A[i].Author<<"\n";
		cout<<"Price: "<<A[i].Price<<"\n";
	}
}


The Output is:

Enter the details of book 1:

Enter book number: 45
Enter book name: Scorpia
Enter Author of the book: Anthony Horowitz
Enter price of the book in $: 50


Enter the details of book 2:

Enter book number: 32
Enter book name: Angels and Demons
Enter Author of the book: Dan Brown
Enter price of the book in $: 50




Details of book 1 are:

Book number: 45
Book name: Scorpia
Author: Anthony Horowitz
Price: 50

Details of book 2 are:

Book number: 32
Book name: Angels and Demons
Author: Dan Brown
Price: 50



Enter book number of the book you are looking for: 32
The details of the book of book number "32" are:

Name:
Author:
Price: 0


Enter the name of the book you are looking for: Angels and Demons
The details of the book of the name"Angels and Demons" are:

Book number: 0
Author:
Price: 0

But it should be like:

Enter the details of book 1:

Enter book number: 45
Enter book name: Scorpia
Enter Author of the book: Anthony Horowitz
Enter price of the book in $: 50


Enter the details of book 2:

Enter book number: 32
Enter book name: Angels and Demons
Enter Author of the book: Dan Brown
Enter price of the book in $: 50




Details of book 1 are:

Book number: 45
Book name: Scorpia
Author: Anthony Horowitz
Price: 50

Details of book 2 are:

Book number: 32
Book name: Angels and Demons
Author: Dan Brown
Price: 50



Enter book number of the book you are looking for: 32
The details of the book of book number "32" are:

Name: Angels and Demons
Author: Dan Brown
Price: 50


Enter the name of the book you are looking for: Angels and Demons
The details of the book of the name"Angels and Demons" are:

Book number: 32
Author: Dan Brown
Price: 50

Why is it not coming like this?
Need help urgently!
i variable will be incremented after loop ends, then conditions would be checked. So your value will be off by one.
Use break to stop loop prematurely.

Also if bokk isn't found, your loop will never ends because result of i<N will be discarded. Do not use comma if you do not know how it works.

Fixed SearchByBno:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void SearchByBno(BOOK A[], int N)
{
    std::cout << "Enter book number of the book you are looking for: ";
    int a;
    cin >> a;
    bool found(false);
    int i;//Compatibility with C++11
    for(i = 0; i < N; ++i) {
        if(A[i].Bno == a) {
            found = true;
            break;
        }
    }
    if(found) {
        std::cout << "The details of the book of book number \"" << a << "\" are:\n\n" <<
                  << "Name:   " << A[i].Bname  << "\n" <<
                  << "Author: " << A[i].Author << "\n" <<
                  << "Price:  " << A[i].Price  << "\n" <<
    } else
        std::cout << "No book matches this book number.";
    std::cout << "\n\n";
}
Last edited on
closed account (3qX21hU5)
Please please don't just paste a wall of code and another wall of output and tell us "Find the problem". When you ask questions in this manner you make us seem like your employees ;p

We are here to help because we want to, but we do like it when people put effort into asking their questions. If they put effort into their questions it shows us that this person is worth helping because he has actually tried to figure it out on his own.

So please post a detailed question about what you want to know, what line of output is wrong, what you have tried to fix it (We like this one), any error codes, any code that relates to the problem, what your program is suppose to do, what you don't understand, ect ect.

Be specific in your questions and make sure you put effort into them!
Zereo..never thought of it that way..i apologise would do that from next time :) But I seriously think I couldn't have written much for this one.. :/ Still would take care from next time :)
Last edited on
Thanks alot MiiniPaa! It was of great help :)
Topic archived. No new replies allowed.