If else statement doubt

The question is to search for a book title and display the details.If book title does not exist give appropriate error message.
When i tried this program it is unwantedly printing the else part. Can anyone give solution please...

Here is my code
1
2
3
4
5
6
7
case 2:{
	cout<<"\n\n Enter book title\t: ";
	gets(bt);
	for(i=0;i<5;i++)
	arr[i].srch(bt);
	break;
	}

1
2
3
4
5
6
7
8
9
10
11
12
void book::srch(char but[])
{
if((strcmp(but,title)==0))
	{
cout<<"\n\n ----------------------------------------------\n";
cout<<" Title\t\t\tAuthor\t\tQty\tPrice\n";
cout<<" ---------------------------------------------\n\n";
cout<<title<<"\t\t"<<aname<<"\t\t"<<qty<<"\t"<<price<<"\n";

}
else cout<<"\n\n Sorry entered book does not exist!!\n\n";
}


For example if i give book name that is in the list it is printing like this

Sorry entered book does not exist!!

Sorry entered book does not exist!!
---------------------------------------------------------
Title              Author           Qty             Price
---------------------------------------------------------
Hamlet             Shakespeare      40               350 

Sorry entered book does not exist!!

Sorry entered book does not exist!!
Did you try tracing your for loop by hand? You should be able to see the problem pretty easily if you do that.
:S

This is logic problem.

Your code does exactly what you wrote, but not what you wanted.

Did you try tracing your for loop by hand? You should be able to see the problem pretty easily if you do that.


His output shows everything he needs to trace an error. He only need to think a little bit.
Last edited on
Topic archived. No new replies allowed.