alphabetical order and object orienting

I need to add the following things to my program i already have finished.
• Keep the inventory in alphabetical order. When a new entry is to be added, put the entry into the appropriate location in the array.
• add a command ‘d’ for delete to delete an entry corresponding to a product name typed by the user.
• Add a command “u” to allow a user to update (edit) one or more of the fields in the inventory
• Make the program object-oriented. This means having a separate class for the internal representation of the inventory, with methods like list or delete.

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
  #include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

class Entry {
	string name, quantity, notes;
};
Entry entryList[100];

int rec_num = 0;
int num_entries;

string toUpper (string S) {
	for (int i= 0; i<S.length(); i++)
		S[i] =  toupper(S[i]);
	return S;
}

void ReadFile () {
   string S, eol;
   fstream F("Inventory.txt");
   while (!F.eof() && !F.fail()){
	   F >> entryList[rec_num].name
	         >> entryList[rec_num].quantity;
      getline(F, eol); //reads the end-of-line marker
      getline(F, S);
	   entryList[rec_num].notes = S;
	   rec_num++;
   }
   cout << "Inventory read." << endl;
   num_entries = rec_num;
   F.close();
   return;
}
void StoreFile () {
   fstream F("Inventory.txt");
   rec_num = 0;

	while (rec_num < num_entries){
	  F << entryList[rec_num].name      << " "
	    << entryList [rec_num].quantity << " "
		 << entryList [rec_num].notes    << " " << endl;
	  rec_num++;
   }
   cout << "Inventory stored." << endl;
   return;
}

void add_item(string name, string quantity, string notes){
	entryList [num_entries].name   = name;
	entryList [num_entries].quantity = quantity;
	entryList [num_entries].notes  = notes;
	num_entries++;
    return;
}
void retrieve_item(string name){
    for (int i = 0; i < num_entries; i++){
        if (toUpper(entryList [i].name) == toUpper(name)) {
            cout << "Quantity: " << entryList [i].quantity << endl
                 << "Notes: " << entryList [i].notes << endl;
            return;
        }
    }
    cout << "Item not found" << endl;
    return;
}

void listAllItems() {
	int i = 0;
	while (i < num_entries) {
		cout << "-- " << entryList [i].name << " "
			  << entryList [i].quantity << endl
			  << "-- " << entryList [i].notes << endl << endl;
		i++;
	}
}

int main(){
    string name, quantity, notes;
    char command;

    ReadFile ();
    cout << "Use \"e\" for enter, \"f\" for find, \"l\" for list,  \"q\" to quit."
         << endl << "Command: ";
    cin >> command;
    while (command != 'q'){
        switch (command){
        case 'e': cin >> name;   cout << "Enter Quantity: ";
                   cin >> quantity; cout << "Enter Notes: ";
                   cin.ignore(); getline(cin, notes);
                   add_item(name, quantity, notes);    break;
        case 'f': cin >> name; retrieve_item(name); break;
case 'l': listAllItems(); break;
        }
        cout << "\nCommand: "; cin >> command;
    }
    StoreFile();
    cout << "All set !";
    return 0;
}
Well now it is saying that i cant access name quantity or notes throughout the whole program
1
2
3
4
5
6
8:9: error: 'std::string Entry::name' is private 
25:29: error: within this context 
8:15: error: 'std::string Entry::quantity' is private 
26:33: error: within this context 
8:25: error: 'std::string Entry::notes' is private
29:24: error: within this context


Class members are by default private, so they can't be directly accessed outside the class. Generally, I'd leave the data members private, but have public functions or methods.

1
2
3
4
5
6
class Entry {
    private:
	string name, quantity, notes;
    public:
        //public functions        
};

Topic archived. No new replies allowed.