book store program - function not working

The Function checkavailability is not working properly :
1.It does not takes input
2.Executes both if and else statements.

help please.

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
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
class BOOKS
{
private :
    static const int number_of_books = 2;
    static const int length_of_book_names = 100;
    char booknames[number_of_books][length_of_book_names];
    char publisher[number_of_books][length_of_book_names];
    char author[number_of_books][length_of_book_names];
    float price[10];
    int noofcopies[10];
    char status[10];
    char bookname[101];
public :
    void getbooksinfo()
    {
        int i ; // local scope
        cout<<"\nEnter The Information For Books :\n\n\n";
        for (i = 0 ; i < number_of_books ; ++i)
        {
            cout<<"\nBOOK " << i + 1 ;
            fflush(stdin);
            cout<<"\nEnter The Name Of Book  " ; cin.getline(booknames[i],length_of_book_names);
            fflush(stdin);
            cout<<"\nEnter The Author Name Please : " ; cin.getline(author[i],length_of_book_names);
            fflush(stdin);
            cout<<"\nEnter The Cost Of The book : " ; cin>>price[i];
            fflush(stdin);
            cout<<"\nEnter The Number Of Copies Available : " ; cin>>noofcopies[i];
            fflush(stdin);
            cout<<"\nEnter The Publisher Name : " ; cin.getline(publisher[i],length_of_book_names);
            fflush(stdin);
            cout<<"\nEnter The Status (I - Issued / A - Available ) : " ; cin>>status ;
        }
    }
    void showbooksinfo()
    {
        int x ;
        for (x = 0 ; x < number_of_books ; ++x)
        {
            cout<<"\n\nBook" << x  + 1 ;
            cout<<"\nName Of Book ";  puts(booknames[x]);
            cout<<"\nCost Of The Book : " << price[x];
            cout<<"\nAuthor Of The Book : " ; puts(author[x]);
            cout<<"\nPublisher Name : " ; puts(publisher[x]);
            cout<<"\nNumber Of Copies Available : " <<noofcopies[x];
            cout<<"\nStatus : " << status[x];
        }
    }
    void checkavailablity()
    {
        int n ;
        cout<<"\nEnter The Name Of The Book You Want To Issue : " ; cin.getline(bookname,101);
        for (n = 0 ; n < number_of_books ; ++n)
        {
            if (strcmp(booknames[n],bookname))
            cout<<"\nBOOK AVAILABLE!!!!";
            break ;
            else
                cout<<"\nBook Not Available!!!! SORRY COME after 2 weeks!!!";

        }
    }
    
};
int main()
{
    BOOKS obj ;
    obj.getbooksinfo();
    obj.showbooksinfo();
    obj.checkavailablity();
    return 0 ;
}
In your if and else statements, use braces, { and }. USE BRACES. USE BRACES.

1
2
3
4
5
6
7
8
9
if (strcmp(booknames[n],bookname))
{
    cout<<"\nBOOK AVAILABLE!!!!";
    break ;
}
else
{
    cout<<"\nBook Not Available!!!! SORRY COME after 2 weeks!!!";
}
Last edited on
The code you've posted won't even compile. It's not legal C++.

Look at lines 59 - 63 - that's not legal. it's the equivalent of:

59
60
61
62
63
64
65
            if (strcmp(booknames[n],bookname))
            {
                cout<<"\nBOOK AVAILABLE!!!!";
            }
            break ;
            else
                cout<<"\nBook Not Available!!!! SORRY COME after 2 weeks!!!";


You can see from that why it's not legal - the else isn't connected to an if statement.

If you want to have multiple statements in a conditionally-executed block, you need to use braces.
Last edited on
Got It Thank You Very Much!!!!!!
You're welcome. I'm still confused as to how you were able to run your code, when it wouldn't even compile.
I am not lying it did compile and it showed both the conditional statements.
Then the code you posted is not the code you compiled, because the code you posted is not legal C++, and will not compile.

Just click the "Edit and Run" button by the post and see for yourself, if you don't believe me.
Last edited on
Topic archived. No new replies allowed.