Book Store Program - Classes - if else errors,avoiding public data members

1.From Line 66,the whole switch seems buggy. When it outputs the the total cost it's always of the different book rather than of the one whose name has been put in.
2.I had to make nearly all data members public,in future how can i avoid while still fulfilling the purpose?
3.In the switch case 1 from line 66 how can i add an else statement which only execues after the loop is over and not executes everytime for the false of if.

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
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
class BOOK
{
private :
    char nameofpublisher[80] ;

public :
    char status ;
    char nameofbooks[100] ;
    float cost ;
    float noofcopies ;
    char nameofauthor[70] ;
    void readbooks()
    {
        cout<<"\nEnter The The Name of The Book : " ; gets(nameofbooks) ;
        fflush(stdin);
        cout<<"\nEnter The Cost Of The Book : " ; cin>>cost ;
        fflush(stdin);
        cout<<"\nEnter The Name Of The Author :" ; gets(nameofauthor) ;
        fflush(stdin);
        cout<<"\nEnter The Name Of Publisher : " ; gets(nameofpublisher) ;
        fflush(stdin);
        cout<<"\nEnter The Status Of The Book ( I - Issued / A - Available ) : " ; cin>>status;
        fflush(stdin);
        cout<<"\nEnter The Number Of Copies Available : " ; cin>>noofcopies ;
        fflush(stdin);
    }
    void displaybooks()
    {
        cout<<"\nName Of The Book : " ; puts(nameofbooks) ;
        cout<<"\nAuthor Of The Book : " ; puts(nameofauthor) ;
        cout<<"\nPublisher Of The Books " ; puts(nameofpublisher) ;
        cout<<"\nStatus Of The Book : " <<status ;
        cout<<"\nCost Of The Book : " <<cost ;
        cout<<"\nThe Number Of Copies Available : " << noofcopies ;
    }




};
int main()
{
    BOOK stock[10] ;
    int i ;
    for ( i = 0 ; i < 2 ; ++i)
    {
        cout<<"\n\nEnter The Information For Book " << i + 1 ;
        stock[i].readbooks();
    }
    system("cls");
    for (i = 0 ; i < 2 ; ++i)
        {
            cout<<"\n\nBook " << i + 1 ;
            stock[i].displaybooks();

        }
    int choice,totalcost,reqcopies;
    char searchbookname[100],searchauthorname[100];
    cout<<"n\n\nSearch Menu\n1.Search Via Book Name\n2.Search Via Author Name " ;
    cin>>choice ;
    fflush(stdin);
    switch (choice)
    {

        case 1 : cout<<"\nEnter The Name Of The Book You Want To Buy : " ;
                 gets(searchbookname) ;
                 cout<<"\nEnter The Number Of Copies You Need : " ;
                 cin>>reqcopies ;
        for (i = 0 ; i < 2 ; ++i)
        {

            if ((strcmp(stock[i].nameofbooks,searchbookname)) && (stock[i].status == 'A' || stock[i].status == 'a') && (stock[i].noofcopies >= reqcopies))
              {
                 cout<<"\nBOOK FOUNDDDD!!!!!" ;
                 cout<<"\nSTOCK AVAILABLE \t Total Cost = " << stock[i].cost * reqcopies
                 <<"\nThanks For Visiting COME AGAIN!" ;

              }
              else ;

        }

        break ;
        case 2 : cout<<"\nEnter The Name Of The Author Whose Books You Want Search/BUY : " ;
                 gets(searchauthorname);
        cout<<"\nBooks By The Searched Author : " ;
        for (i = 0 ; i < 2 ; ++i)
        {
            if(!strcmp(stock[i].nameofauthor,searchauthorname))

                {
                    cout<<"\n"<<stock[i].nameofbooks ;
                }
                else
                {
                    cout<<"\nNo Books Found By The Author!!!!!!" ;
                }
        }


    }
        return 0 ;
}
It is hard to help because it seems some of your code is missing.
For example the "gets()" function. But one note I might make is since you have the header <string.h> why not use it instead of the char arrays. Like so...

1
2
3
4
5
6
7
8
9
class BOOK
{
private:
	string nameofpublisher;
	char status;
	string nameofbook;
	float cost;
	int noofcopies;
	string nameofauthor;
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
#include<iostream>
#include<string>

using namespace std;
class BOOK
{
private :
    string nameofpublisher ;
    char status ;
    string nameofbook;
    int cost ;
    int noofcopies ;
    string nameofauthor ;
public:
    string get_nameofpublisher() { return nameofpublisher; }
    void set_nameofpublisher(const string & name) { nameofpublisher = name;}
    
    char get_status() { return status; }
    void set_status( char ch) { status = ch; }
    
    string get_nameofbook() { return nameofbook; }
    void set_nameofbook(const string & name) { nameofbook = name; }
    
    int get_cost() { return cost; }
    void set_cost( int c) { cost = c; }
    
    int get_noofcopies() { return noofcopies; }
    void set_noofcopies( int noof) { noofcopies = noof; }
    
    string get_nameofauthor() { return nameofauthor; }
    void set_nameofauthor(const string & name) { nameofauthor = name;}
       
    void readbooks()
    {
        string tmp;
        
        cout<<"\nEnter The The Name of The Book : " ; 
        getline(cin, tmp) ;
        set_nameofbook(tmp);
        
        cout<<"\nEnter The Cost Of The Book : " ;
        getline(cin, tmp) ;
        set_cost( stoi(tmp));
        
        cout<<"\nEnter The Name Of The Author :" ; 
        getline(cin, tmp) ;
        set_nameofauthor( tmp);

        cout<<"\nEnter The Name Of Publisher : " ; 
        getline(cin, tmp) ;
        set_nameofauthor( tmp);

        cout<<"\nEnter The Status Of The Book ( I - Issued / A - Available ) : " ; 
        getline(cin, tmp);
        set_status( tmp[0]);
        
        cout<<"\nEnter The Number Of Copies Available : " ;
        getline(cin, tmp);
        set_noofcopies(stoi(tmp));
    }
    void displaybooks()
    {
        cout<<"\nName Of The Book : " << nameofbook ;
        cout<<"\nAuthor Of The Book : "  << nameofauthor ;
        cout<<"\nPublisher Of The Books " << nameofpublisher ;
        cout<<"\nStatus Of The Book : " <<status ;
        cout<<"\nCost Of The Book : " <<cost ;
        cout<<"\nThe Number Of Copies Available : " << noofcopies ;
    }
};

int main()
{
    BOOK stock[10] ;
    int i ;
    for ( i = 0 ; i < 2 ; ++i)
    {
        cout<<"\n\nEnter The Information For Book " << i + 1 ;
        stock[i].readbooks();
    }
    system("cls");
    for (i = 0 ; i < 2; ++i)
    {
        cout<<"\n\nBook " << i + 1 ;
        stock[i].displaybooks();
    }
    int choice, totalcost, reqcopies;
    string searchbookname, searchauthorname;
    cout<<"n\n\nSearch Menu\n1.Search Via Book Name\n2.Search Via Author Name " ;
    string tmp;
    getline(cin, tmp);
    choice = stoi(tmp);
    switch (choice)
    {
        case 1 :
            cout<<"\nEnter The Name Of The Book You Want To Buy : " ;
            getline( cin, searchbookname) ;
            cout<<"\nEnter The Number Of Copies You Need : " ;
            getline(cin, tmp);
            reqcopies = stoi(tmp);
            for (i = 0 ; i < 2 ; ++i)
            {
                if ((stock[i].get_nameofbook() == searchbookname) 
                    && (stock[i].get_status() == 'A' || stock[i].get_status() == 'a') 
                    && (stock[i].get_noofcopies() >= reqcopies))
                {
                cout<<"\nBOOK FOUNDDDD!!!!!" ;
                cout<<"\nSTOCK AVAILABLE \t Total Cost = " << stock[i].get_cost() * reqcopies
                    <<"\nThanks For Visiting COME AGAIN!" ;

                }
                else ;
            }
            break ;
        
        case 2 : 
            cout<<"\nEnter The Name Of The Author Whose Books You Want Search/BUY : " ;
            getline( cin, searchauthorname);
            cout<<"\nBooks By The Searched Author : " ;
            bool book_found = false;
            for (i = 0 ; i < 2 ; ++i)
            {
                if( (stock[i].get_nameofauthor() == searchauthorname) )
                {
                    book_found = true;
                    cout<<"\n"<<stock[i].get_nameofbook() ;
                }
            }
            if (book_found == false) cout<<"\nNo Books Found By The Author!!!!!!" ;
    }
        
    return 0 ;
}
Thanks @nuderobmonkey,seriously thank you,I have just started working with classes and that was a goddamn big help but can you please elaborate what does the stoi() and set_ does in some plain english,I am not wholly familiar with string library just some of the stuff from it,I would be very thankful.
'stoi' means 'string to int', and it tries to extract an integer from the content of a std::string.

The set_ and get_ means nothing special. They are just a convention of naming for these methods which 'get' or 'set' a private member variable of a class.

The std::string class is so fundamental that i suggest that you take a closer look at it. Further i suggest a closer look at the class std::stringstream (which handles a string as an iostream).
I think cost should be of type float, in case you have a book cost 5.99 for example.
Topic archived. No new replies allowed.