How To Override A Member Function Under Class In C++ ?

I am working with a book database, in my code below the "changeEntry()" function is not working, in that function I want to override the old entry in the database and checking the entry by "show all Book entry", any help would be appreciated.


//code start

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include<vector>
#include <cstdlib>
#include <stdio.h>
#include<ostream>
#include<limits>

using namespace std;
enum book {Chemistry};

class addBook

{
public :
addBook()
{
count=0;

}

void addEntry();
void displayAll();
void displayEntry(int i);
void changeEntry();

class entry_struct
{
public :
char firstName[ 15 ] ;
char newName[ 15 ] ;
book Topic;

};


vector<entry_struct>entries;
unsigned int count;
string subject;
};

void addBook::addEntry()
{

entry_struct InputBook;
cout << "Entry number " << (count + 1) << " : " << endl;
cout << "Authors First Name: ";
cin >> InputBook.firstName;
string sInput;
cout << "Subject: ";
cin >> sInput;
if(sInput == "Chemistry")
{
entries.push_back(InputBook);
entries[count].Topic = Chemistry;}
++count; }

void addBook::displayEntry(int i)
{
cout << "Entry[" << i + 1 << "] : " << endl;
cout << "First name : " << entries[i].firstName << endl;
cout << "Subject : ";
switch (entries[i].Topic ){
case 0:
cout<<"Chemistry"<<endl;
break;}

}

void addBook::changeEntry()

{


cout<<"Which book do you want to chnage?"<<endl;

cin>>count;


entry_struct InputBook;
cout << "Entry number " << (count ) << " : " << endl;
cout << "Authors First Name: ";
cin >> InputBook.newName;
entries[count] = InputBook;
string sInput;
cout << "Subject: ";
cin >> sInput;
if(sInput == "Chemistry")
{
entries.push_back(InputBook);
entries[count].Topic = Chemistry;
entries[count] = InputBook;}
}
// What should I do here to override the entry in database ?


void addBook::displayAll()
{
cout << "Number of entries : " << count << endl;

for(unsigned int i = 0;i < count;i++)displayEntry(i);

}

addBook my_book;

int mainMenu()
{

bool bQuit = false;


while(bQuit == false)
{

cout << "(a) : New book entry " << endl;
cout << "(b) : Show all books entry" << endl;
cout << "(c) : Change Entry entry" << endl;
cout << " " << endl;
cout << "Please enter a letter for one of the above options." << endl;
cout << "Your choice : ";
char q;
cin >> q;

if (q == 'a')my_book.addEntry();
if (q == 'b')my_book.displayAll();
if (q == 'c')my_book.changeEntry();
}
return 0;
}


int main (){


mainMenu();
return 0;



}

//code end
Several problems in changeEntry().

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
void addBook::changeEntry()
{   cout<<"Which book do you want to chnage?"<<endl;
    cin>>count;

    entry_struct InputBook;
    cout << "Entry number " << (count ) << " : " << endl;  // When you display the books, you add 1 to the count, but you don;t account for that here.
    cout << "Authors First Name: ";
    cin >> InputBook.newName;  // Don't assign to newName.  You should be changing firstName.  newName is not needed.
    entries[count] = InputBook;
    string sInput;
    cout << "Subject: ";
    cin >> sInput;
    if (sInput == "Chemistry")
    {  //  entries.push_back(InputBook);  // This adds a new book.  You don't want to do that.
        entries[count].Topic = Chemistry;  // This will get overlaid by the next statement
        //  entries[count] = InputBook;          // You've already done this at line 82
    }
}


Suggestion: You should make all changes to InputBook before assigning it to entries[count] at line 82.

BTW, this has nothing to do with overriding a member function.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
Thanks for your help, some issues are arising....

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


void addBook::changeEntry()

{


cout<<"Which book do you want to chnage?"<<endl;

cin>>count;// if cin==0;

entry_struct InputBook;
cout << "Entry number " << (count+1 ) << " : " << endl;// taking entry number 2
cout << "Authors First Name: ";
cin >> InputBook.firstName;
entries[count] = InputBook;
string sInput;
cout << "Subject: ";
cin >> sInput;
if(sInput == "Chemistry")
{

entries[count].Topic = Chemistry;


}
if(sInput == "Math")
{

entries[count].Topic = Math;


}
}


finally not showing the changed entry, now I am adding Math in the previous code
Sorry, I forgot to put the code within [ code]-[ /code]
Line 10: User enters a 1 based entry number.

Line 13: You're displaying it as user count +1. If the user entered 1 for the first book, you would be displaying 2.

Line 16,23,30: You're replacing entries[1]. What you want is entries[0] for the first book.

Suggest: At line 11, do the following:
 
  count--;


This will adjust the count once from 1 based to 0 based. Adding 1 in line 13 would then be correct and the zero based reference to the vector will be correct in the remaining statements.
Last edited on
Thanks a lot for helping.
Topic archived. No new replies allowed.