Self declare class array

May I know after I create a class and object and array like this

Book book[5]

how can I accept the input from user but not 1 times all input?

like inside the I am doing some question like asking the information about the book
1
2
3
for(i=0;i<5;i++){
cout<<"Enter Title : ";
}

Can I use the book[nob].setTitle(tit);?
is it possible to put like this way?
-----------------------------------------
1
2
3
4
5
6
7
8
9
string aut[4];
int noa=0;
do{
            cout<<"Enter Author name : ";
            getline(cin,aut[noa]);
            cout<<"Is there still got any others Author?(yes/no)";
            cin>>cont;
            noa++;
        }while(cont!="no"&&noa<4);

after I save all the author name from the user,how can I pass the array to my class?
book[nob].setAuthor(aut[]);
Is this work?let say if the numer of author is 3
if I want to pass the array to set the,Is this possible to write like this?
when I want to return the the author
Can I to write like this way

1
2
3
string* Book::getAuthor(){
    return author;
}

------------------
about the constructor
I had check a lot of the post about constructor
it is like create a situation that you expect will happen,and you are going to initialize it like

1
2
3
4
5
6
Book::Book(string &tit,string &aut,double price){
title=tit;
author=aut;
pRice=price;
}
Book abook("Doremon","Datuk",22.20);

How if I dont want to to initialize it?
is it necessary to be exist?
Hope somebody could help me with this,Thanks
Last edited on
Hey. Please edit your post and put all of the code between code tags - http://www.cplusplus.com/articles/jEywvCM9/

It looks you you initialize the title, author and the price of the book in a constructor. So you would want to call that constructor and send in those 3 info as parameters.
Yea,After I get the input from the user,then I will initialize those inside my class,but if I using set and get method to initialize it,what for I create the constructor?
Herm.all my coding is about 6-7 pages,will I get caught because of piagarism after I upload it?Cause it's my own work but I have a bit confuse some of them
code tags, CODE TAGS, use them :o
http://www.cplusplus.com/articles/jEywvCM9/

May I know after I create a class and object and array like this

Book book[5]

how can I accept the input from user but not 1 times all input?
like inside the I am doing some question like asking the information about the book
1
2
3
for(i=0;i<5;i++){
     cout<<"Enter Title : ";
}

Can I use the book[nob].setTitle(tit);?
is it possible to put like this way?
-----------------------------------------
1
2
3
4
5
6
7
8
9
string aut[4];
int noa=0;
do{
    cout<<"Enter Author name : ";
    getline(cin,aut[noa]);
    cout<<"Is there still got any others Author?(yes/no)";
    cin>>cont;
    noa++;
} while(cont!="no"&&noa<4);

yes, that looks fine

after I save all the author name from the user,how can I pass the array to my class?
book[nob].setAuthor(aut[]);
Is this work?let say if the numer of author is 3
if I want to pass the array to set the,Is this possible to write like this?

not exactly.
You will have to loop through all books and set the author.

You can just do it in the loop above like this
string aut; // not an array anymore
int noa=0;
do{
cout<<"Enter Author name : ";
getline(cin,author);
cout<<"Is there still got any others Author?(yes/no)";
cin>>cont;
book[noa].setAuthor(author);
noa++;
} while(cont!="no"&&noa<4);


when I want to return the the author
Can I to write like this way

1
2
3
string* Book::getAuthor(){
    return author;
}

You could but it's bad practice
you can just return a copy of the author string, otherwise he could be modified from outside

Just remove the * beside string* to do so
1
2
3
string Book::getAuthor(){
    return author;
}



------------------
about the constructor
I had check a lot of the post about constructor
it is like create a situation that you expect will happen,and you are going to initialize it like

1
2
3
4
5
6
Book::Book(string &tit,string &aut,double price){
    title=tit;
    author=aut;
    pRice=price;
}
Book abook("Doremon","Datuk",22.20);

How if I dont want to to initialize it?
is it necessary to be exist?
Hope somebody could help me with this,Thanks


If you don't want to initialize it you can write a "default constructor"
1
2
3
4
5
Book::Book(){
    title = "";
    author = "";
    pRice = 0;
}


Also: you should change those "string &" to "const string &" because you don't modify the strings.
Without them you say "give me a piece of paper with the name on it, but be careful, i might write on them, you get back the new version" (you change the string)
With these const string& basically say "give me a piece of paper with the name on it and I'll not write on it, you'll get it back after I'm finished" (you don't change the string, it is constant)
1
2
3
4
5
Book::Book(const string &tit,const string &aut,double price){
    title=tit;
    author=aut;
    pRice=price;
}
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include <string>
#include "Sales.h"
#include "Member.h"
#include "Book.h"

using namespace std;

int main(){
    int ans;
    int noa=0;
    int nob=0;
    int nom=0;
    string tit;
    string aut[4];
    string pname;
    string isbn;
    string cont;
    double pri;
    int ncopy;
    Book book[5]();
    Member member[5];
    Sales sales;

    do{

    cout<<"\nWelcome to Book Store Application!"
    <<"\nPlease select to continue:"
    <<"\n1)Add Books"
    <<"\n2)Add Member"
    <<"\n3)Update Number of Books"
    <<"\n4)Purchase Book"
    <<"\n5)Check the sales record"
    <<"\n0)Exit";
    cin>>ans;

    switch (ans){
    case 1:

        cout<<"Enter Title : ";
        cin>>tit;
        cin.ignore();
        do{
            cout<<"Enter Author name : ";
            getline(cin,aut[noa]);
            cout<<"Is there still got any others Author?(yes/no)";
            cin>>cont;
            noa++;
        }while(cont!="no"&&noa<4);
        cout<<"Enter Publisher Name : ";
        getline(cin,pname);
        cout<<"Enter ISBN : ";
        cin>>isbn;
        cout<<"Enter Price of the book : ";
        cin>>pri;
        cout<<"Enter Number of Copy of the Book ";
        cin>>ncopy;
        book[nob].setTitle(tit);
        book[nob].setAuthor(aut[]);
        book[nob].setPubName(pname);
        book[nob].setIsbn(isbn);
        book[nob].setPrice(pri);
        book[nob].setNoOfCopy(ncopy);
        book[nob].increaseNoOfCopy(ncopy);
        nob++;
        break;
    case 2:
        cout<<"Please enter your name : ";
        cin>>member[nom].setName();
        cout<<"Please enter your ID : ";
        cin>>member[nom].setId();
        cout<<"You need to pay $10.00 for the yearly membership!";
        member[nom].setTotalSpent(10);
        nom++;
        main();
        break;
    case 3:
        for(int i=0;i<=sizeof(book);i++){
            int z=1;
            cout<<"\n"<<z<<"\t"
            <<book[i].getTitle()<<"\t\t"
            <<book[i].getAuthor()<<"\t\t"
            <<book[i].getPubName()<<"\t\t"
            <<book[i].getIsbn()<<"\t\t"
            <<book[i].getPrice()<<"\t\t"
            <<book[i].getNoOfCopy();
            z++;}
            int choice;
            cout<<"Please choose the number of the book you want to update : ";
            cin>>choice;
            choice=choice-1;
            cout<<"Please enter the amount you want to add : ";
            cin>>book[choice].increaseNoOfCopy();
            cout<<"Updated succuessful!";
            break;
    case 4:
        for(int i=0;i<=sizeof(book);i++){
            int z=1;
            cout<<"\n"<<z<<"\t"
            <<book[i].getTitle()<<"\t\t"
            <<book[i].getAuthor()<<"\t\t"
            <<book[i].getPubName()<<"\t\t"
            <<book[i].getIsbn()<<"\t\t"
            <<book[i].getPrice()<<"\t\t"
            <<book[i].getNoOfCopy();
            z++;}
            int choice;
            string answer;
            string inID;
            int nobb;       //number of book want to buy
            double pay;
            double disc;
            cout<<"Are you a member or non member(y/n)?";
            cin>>answer;
                if((answer=='y')||(answer=='Y'){
                    cout<<"Please enter your ID : ";
                    cin>>inID;
                    for(int j=0;j<=sizeof(member);j++){
                        if(inID==member[j].getId()){
                            cout<<"Welcome to Bookstore "<<member[j].getId();
                            cout<<"Please choose the number of the book you want to purchase : ";
                            cin>>choice;
                            choice=choice-1;
                            cout<<"Please enter the amount you want to buy : ";
                            cin>>nobb;
                                if(book[choice].getNoOfCopy()!=0&&book[choice].getNoOfCopy!<nobb)
                                {
                                    cin>>book[choice].redNoOfCopy(nobb);
                                    cout<<"Bought Successful!";

                                    if(member[j].getNoOfBookBought()>50)
                                        pay = 0.9*book[choice].getPrice();
                                        disc=0.1*book[choice].getPrice();
                                    else
                                        pay = 0.95*book[choice].getPrice();
                                        disc = 0.05*book[choice].getPrice();


                                    sales.addtotal(book[choice].getPrice()*nobb);
                                    sales.setTotalNoOfBookSales(nobb);
                                    sales.addTotalDiscount(disc*nobb);
                                    member[j].setTotalSpent(pay*nobb);
                                   }
                                else
                                    cout<<"The Book are not enough,please tell seller to add more!";
                                    cout<<"Updated succuessful!";
                            }
                            else
                                cout<<"Your ID is not valid!";
                    }}
                    else if (answer=='n'||answer=='N'){
                            int nobb;
                            int choice;
                            cout<<"Please choose the number of the book you want to purchase : ";
                            cin>>choice;
                            choice=choice-1;
                            cout<<"Please enter the amount you want to buy : ";
                            cin>>nobb;
                                if(book[choice].getNoOfCopy()!=0&&book[choice].getNoOfCopy()!<nobb)
                                {
                                    cin>>book[choice].redNoOfCopy(nobb);
                                    cout<<"Bought Successful!";

                                    sales.addtotal(book[choice].getPrice()*nobb);
                                    sales.setTotalNoOfBookSales(nobb);
                                   }


                    }
                    else
                        cout<<"Invalid Input!";
                break;
    case 5:
        cout<<"\nTotal Sales : "sales.getTotalSales()
        <<"\nTotal Number of Book Sold : "<<sales.getTotalNoOfBookSales()
        <<"\nTotal Amount of Discount : "sales.getTotalDiscount();
        break;
    }


    }while(ans!=0);


}

This is my main.cpp
Is there got any problem with this?
Last edited on
I won't look at all of your code.
Copy the snippets you are interested in and I'll tell you if they are fine ^^
I know right,that's why I say my code is very long,lol...
all my question is actually in the first post....have a time to look at that..
1
2
3
4
5
6
7
8
9
10
string aut; // not an array anymore
int noa=0;
do{
cout<<"Enter Author name : ";
getline(cin,author);
cout<<"Is there still got any others Author?(yes/no)";
cin>>cont;
book[noa].setAuthor(author);
noa++;
} while(cont!="no"&&noa<4);


just now you mention loop like this
book[noa].setAuthor(author);
this only can set to the specific book,I know this,but what I need to know is how can I store the value in specific book with few authors,like one book we can have a lot of author,Example,I want to save 3 authors to book[1], if I don't use array to store the 3 input from the user,how can I store the 3 input?

1
2
3
string Book::getAuthor(){
    return author;
}

I did remove the * from the string,but it pop out error

1
2
3
4
5
could not convert ‘((Book*)this)->Book::author’ from ‘std::string* {aka std::basic_string<char>*}’ to ‘std::string {aka std::basic_string<char>}’
     return author;
In constructor ‘Book::Book(std::string&, std::string, std::string&, std::string&, double, int)’:
/home/guankin/Documents/C++/Assignment 1/Book.cpp:7:12: error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘std::string* {aka std::basic_string<char>*}’ in assignment
     author = aut;


If I am going to write a default constructor,is it will be like this?
1
2
3
Book::Book(){

}

since I got nothing to initialize myself
Last edited on
just now you mention loop like this
book[noa].setAuthor(author);


If that's the case, then your book has to have a dynamic amount off authors right?
So it has to be able to append the authors :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Book
{
public:
// stuff
    
    std::vector<std::string> authors;
};
// other stuff

string aut; // not an array anymore
int noa=0;
do{
    cout<<"Enter Author name : ";
    getline(cin,author);
    cout<<"Is there still got any others Author?(yes/no)";
    cin>>cont;
    book[noa].authors.push_back(author);
    noa++;
} while(cont!="no"&&noa<4);


I did remove the * from the string,but it pop out error

Could you please show me your book class? :)


If I am going to write a default constructor,is it will be like this?

jup, that's it
Last edited on
This is my book.h
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
#ifndef BOOK_H_INCLUDED
#define BOOK_H_INCLUDED
#include<iostream>
#include <string>

class Book {

    private:
        std::string title;
        std::string *author = new std::string;
        std::string pubname;
        std::string ISBN;
        double price;
        int nofcopy;
    public:
        Book(const std::string&,const std::string&,const std::string&,const std::string&,double,int);

        void setTitle(std::string &tit);
        std::string getTitle();
        void setAuthor(std::string &aut);
        std::string getAuthor();
        void setPubName(std::string &pname);
        std::string getPubName();
        void setIsbn(std::string &isbn);
        std::string getIsbn();
        void setPrice(double);
        double getPrice();
        void setNoOfCopy(int);
        int getNoOfCopy();
        void increaseNoOfCopy(int);
        void redNoOfCopy(int);
        void display();
};



#endif // BOOK_H_INCLUDED 


This is my book.cpp
I already change back the string* to string
but the error occur :/

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
#include "Book.h"
#include <iostream>
using namespace std;

Book::Book(const string &tit,const string &aut,const string &pname,const string &isbn,double pri,int ncopy){
    title = tit;
    author = aut;
    pubname = pname;
    ISBN = isbn;
    price = pri;
    nofcopy = ncopy;
}

void Book::setTitle(string &tit){
    this->title = tit;
}
string Book::getTitle(){
    return title;
}
void Book::setAuthor(string &aut){
    for(int i=0;i<4;i++)
        this->author[i]= aut;
}
string Book::getAuthor(){
    return author;
}
void Book::setPubName(string &pname){
    this->pubname = pname;
}

string Book::getPubName(){
    return pubname;
}
void Book::setIsbn(string &isbn){
    this->ISBN=isbn;
}
string Book::getIsbn(){
    return ISBN;
}
void Book::setPrice(double pri){
    this->price=pri;
}
double Book::getPrice(){
    return price;
}
void Book::setNoOfCopy(int ncopy){
    this->nofcopy = ncopy;
}
int Book::getNoOfCopy(){
    return nofcopy;
}
void Book::increaseNoOfCopy(int incre){
    nofcopy = nofcopy+incre;
}
void Book::redNoOfCopy(int dec){
    nofcopy = nofcopy-dec;
}
void Book::display(){
    cout<<"\nTitle : "<<title;
    cout<<"\nAuthor : ";
    for(int i=0;i<4;i++){
        cout<<author[i];
    }
    cout<<"Publisher Name : "<<pubname
    <<"ISBN : "<<ISBN
    <<"Price : "<<price
    <<"Number Of Copy : "<<nofcopy;
}

I think same to create a default constructor to book.cpp right?since I didnt initialize myself
so I don't need to create any non-default constructor
1
2
3
4
void Book::setAuthor(string &aut){
    for(int i=0;i<4;i++)
        this->author[i]= aut;
}


So you assign all 4 authors the value aut?
that doesn't seem to be what you want.


1
2
3
4
5
6
7
8
9
class Book {

    private:
        std::string title;
        std::string *author = new std::string;
        std::string pubname;
        std::string ISBN;
        double price;
        int nofcopy;

protip: use std::vector<std::string> instead of std::string*
Last edited on
Topic archived. No new replies allowed.