This is my header file ,it contains the class definition
class books {
public:
books() = default; // this is to create an object | so class obj
books(string name, string auth, double pri, int copy) {
book_name = name;
author = auth;
price = pri;
};
int sell_books();
bool status();
int zero_copies(int c);
void book_search(string name, string author);
void buy_book(string name,string auth,double money);
void print_();
bool data{};
double price{};
string book_name{};
string author{};
int copy{};
private:
//ClassName ObjectName[number of objects];
};
int books::sell_books() {
std::cout << "type in the name of your book " << std::endl;
getline(cin>>ws,book_name);
std::cout << "type in the name of your author " << std::endl;
getline(cin >> ws,author );
std::cout << "type in the price that you want for it " << endl;
cin >> price;
std::cout << "type in the number of copies you want to sell " << endl;
cin >> copy;
return 1; //function will return 1 to show that it is succesful
}
void books::print_() {
cout << std::endl;
cout << "This is the book name : " << book_name << endl;
cout <<"This is the new author " << author << endl;
cout <<"This is the price " << price << endl;
cout << std::endl;
}
int maximum(int a, int b) {
if (a > b) {
return a;
}
else {
return b;
}
}
void books::book_search(string name, string auth) {
int a = name.length();
int b = (book_name).length();
int result = maximum(a, b);
for (int i = 1; i < result; ++i) {
int k = strncmp(name.c_str(), (book_name).c_str(), i);
}
}
void books::buy_book(string name ,string auth, double money) {
if (book_name == name) {
cout << "book found " << endl;
cout << "this is the price " << price << endl;
int value = zero_copies(copy);
if (value = 0) {
if (money > price) {
money = money - price;
std::cout << "this is your account balance " << money << endl;
}
else if (money < price) {
cout << "there is not enough money " << endl;
}
}
else if (value == 1) {
cout << "book no found " << endl;
}
}
}
int books::zero_copies(int c) {
int k{};
if (c == 0) {
cout << "there are no copies remaining " << endl;
k = 1;
}
else if (c > 1) {
std::cout << endl;
k= 0;
}
return k;
}
bool books::status() {
if (book_name == "") { //if the book name of that object is null then we shall return false
if (author == "") {
return false;
}
}
else if (book_name != "") {
if (author != "") {
return true;
}
}
}
int choice{0};
void menu_func() {
std::cout << "MENU" << std::endl;
std::cout << "1 . sell your books " << '\n' << " 2 . buy your books " << "3 . check your account balance " << endl;
std::cout << "choose your options " << endl;
cin >> choice;
}
Now this is my main cpp file ,here i will show you the code
using namespace std;
int main()
{
double balance{24.54};
menu_func();
books store[10]{};
int i{ 0 };
switch (choice) {
case 1: {
char reply;
//address of class is null
while (true) {
if (store[i].status() == false) { //false means that there is no data inside them
store[i].sell_books();
balance = balance + store[i].price; //*(store)[i].price
std::cout << "do you want to continue " << std::endl;
cin >> reply;
if (reply == 'Y') {
++i;
continue;
}
if (reply == 'N') {
cout << "Balance " << balance << endl;
break;
return 1;
}
else if (reply == 'B') {
cout << "Going back to main menu " << endl;
menu_func();
}
}
else if(store[i].status() == true) {
++i;
continue;
if (i == 10) {
cout << "you have reached the end of the file " << endl;
break;
}
}
// the program usually stops at the i
}
for (int i = 0; i < 10; ++i) {
std::cout << std::endl;
cout << "This is at index " << i << endl;
store[i].print_();
std::cout << std::endl;
}
break;
}
The program works it allows me to type into the member variables of the object however it is not saved ,it will reset after debug time.I want it to stay inside that object until i let it go myself.
I tried to make the member variables a pointer to point to an adress of another member variable.But that turned out to be a hassle.I couldn't really find what i was looking for online