uhm..

Hi everyone.I'm new here,and new to C++ coding.I have an assignment that can add, edit, delete, search, and display records.I've managed to add, and display using different ways like Struct,and Class by following some codes from my book.But my problem is Searching.I found some codes in my book under Array, on how to search but its only for when array is created and initialized already.But I need to use files.Can anyone help me?

Also when I run my codes, I get:

In function 'void Menu()'
no matching function for call to 'Inventory::Add()'
no matching gunction for call to 'Inventory::displayAll()'

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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cctype>
using namespace std;

class Inventory
{
    public:
      Inventory()
      {  code = 0;  gName = " ";  bName = " ";  dForm = " ";  dStr = " "; day = 0;  month = 0;  year = 0;  qty = 0;  price = 0.00; }
      Inventory(int c, string gN, string bN, string dF, string dS, int d, int m, int y, int q, double p)
      { code = c; gName = gN; bName = bN; dForm = dF; dStr = dS; day = d; month = m; year = y; qty = q; price = p;}
      
      int getCode()
      { return code;}
      
      string getGName()
      { string gN = gName; return gN;}
      
      string getBName()
      { string bN = bName; return bN;}

      string getDForm()
      { string dF = dForm; return dF;}

      string getDStr()
      { string dS = dStr; return dS;}
      
      int getDay()
      {return day;}
      
      int getMonth()
      {return month;}
      
      int getYear()
      {return year;}
      
      int getQty()
      {return qty;}
      
      double getPrice()
      {return price;}
      
      void Add(Inventory[], int, string, string, string, string, int, int, int, int, double);
      void Search();
      void displayAll(Inventory[]);
      private:
          int code;
          string gName, bName, dForm, dStr;
          int day, month, year, qty;
          double price;
};
int searching(Inventory[], int , int);
void Menu();
bool redo();

const int SIZE = 100;
      int product[SIZE];
      int recNum = 0;
fstream file;
bool again = true;

void Inventory::Add(Inventory product[], int code, string gName, string bName, string dForm, string dStr,int day, int month, int year, int qty, double price)
{
     file.open("zzz.txt", ios::out | ios::app);
     
     if(!file)
     {
          cout << "Error opening file."<< endl;
          system("PAUSE");
          exit(1);
     }
     cout << "ADD ME" << endl;
    do
    {
          if(recNum < SIZE)
          {
            cout << "- Code: ";
                 cin >> product[recNum].code;
                 file << product[recNum].code << endl;
                 cin.ignore();

            cout << "- Generic Name: ";
                 getline(cin, product[recNum].gName);
                 file << product[recNum].gName << endl;

            cout << "- Brand Name: ";
                 getline(cin, product[recNum].bName);
                 file << product[recNum].bName << endl;

            cout << "- Dosage Form: ";
                 getline(cin, product[recNum].dForm);
                 file << product[recNum].dForm << endl;

            cout << "- Dosage Strength: ";
                 getline(cin, product[recNum].dStr);
                 file << product[recNum].dStr << endl;

            cout << "- Date of Expiry" << endl;
            cout << "  (DD): ";
                 cin >> product[recNum].day;
                 file << product[recNum].day << endl;
                 cin.ignore();
            cout << "  (MM): ";
                 cin >> product[recNum].month;
                 file << product[recNum].month << endl;
                 cin.ignore();
            cout << "  (YYYY): ";
                 cin >> product[recNum].year;
                 file << product[recNum].year << endl;
                 cin.ignore();

            cout << "- Quantity: ";
                 cin >> product[recNum].qty;
                 file << product[recNum].qty << endl;
                 cin.ignore();

            cout << "- Price: RM ";
                 cin >> product[recNum].price;
                 file << product[recNum].price << endl << endl;
                 cin.ignore();
            recNum++;
            file.close();
            }
        cout << "\nRecord Saved!" << endl;
        redo();
    }while (again == true);
     system("PAUSE");
     Menu();
}
void Inventory::displayAll(Inventory product[])
{
     file.open("zzz.txt", ios::in);
     if(!file)
     {
          cout << "No Records in File" << endl;
          system("PAUSE");
          Menu();
     }

     for(int index = 0; index < recNum; index++)
     {
         cout << "\nCode: " << product[index].getCode() << endl;
         cout << "\nGeneric Name: " <<  product[index].getGName() << endl;
         cout << "\nBrand Name: " << product[index].getBName() << endl;
         cout << "\nDosage Form: " << product[index].getDForm() << endl;
         cout << "\nDosage Strength: " << product[index].getDStr() << endl;
         cout << "\nDate of Expiry: " << product[index].getDay() << "-" << product[index].getMonth() << "-" << product[index].getYear() << endl;
         cout << "\nQuantity: " << product[index].getQty() << endl;
         cout << fixed << showpoint << setprecision(2);
         cout << "\nUnit Price: " << product[index].getPrice() << endl;
         cout << "\n" << "-----------------------------------------------------------------" << endl;
     }
    file.close();
    system("PAUSE");
}
void Inventory::Search()
{
     Inventory product[SIZE];
     int scode;
     int pos;

     file.open("zzz.txt", ios::out);
     if(!file)
     {
          cout << "No Records in File" << endl;
          system("PAUSE");
          Menu();
     }

     do
     {
          cout << "Search Code: ";
               cin >> scode;
               cin.ignore();

          pos = searching(product, SIZE, scode);

          if (pos == -1)
             cout << "Record not found" << endl;
          else
          {
              cout << "Record found!" << endl;

             cout << "\nCode: " << product[pos].getCode() << endl;
             cout << "\nGeneric Name: " << product[pos].getGName() << endl;
             cout << "\nBrand Name: " << product[pos].getBName() << endl;
             cout << "\nDosage Form: " << product[pos].getDForm() << endl;
             cout << "\nDosage Strength: " << product[pos].getDStr() << endl;
             cout << "\nDate of Expiry: " << product[pos].getDay()
                  << "-" << product[pos].getMonth()
                  << "-" << product[pos].getYear() << endl;
             cout << "\nQuantity: " << product[pos].getQty() << endl;
             cout << fixed << showpoint << setprecision(2);
             cout << "\nUnit Price: " << product[pos].getPrice() << endl;
             file.close();
          }
          redo();
     }while(again == true);
}
void Menu()
{
     int opt;
     Inventory a,s, dA;

     cout << "1. Add" << endl;
     cout << "2. Display" << endl;
     cout << "3. Search" << endl;
     cout << "SELECTION: ";
          cin >> opt;
          cin.ignore();  
     switch(opt)
     {
        case 1: a.Inventory::Add();
             break;
        case 2: dA.displayAll();
             break;
        case 3:s.Search();
             break;
        default: cout << "Invalid Selection!" << endl;
                 break;
     }
}
int main()
{
    Menu();
    system("PAUSE");
    return 0;
}
int searching(Inventory object[], int size, int value)
{
    int index = 0;
    int position = -1;
    bool found = false;

    while (index < size && !found)
    {
      if(object[index].getCode() == value)
      {
         found = true;
         position = index;
      }
      index++;
    }
    return position;
}
bool redo()
{
char x;
bool v = true;
do
{
 cout << "\nWould you like to do another?";
  cin >> x;    
  cin.ignore();
if (toupper(x) == 'Y')
{
 v = true;
 again = true;
}
else if (toupper(x) == 'N')
{
 v = true;
 again = false;
}
else
{
 cout << "Invalid Option. Please try again" << endl;
 v = false;
}
}while(v == false);
return again;
}
Last edited on
You're getting those errors because on line 216 - 218 you are calling Add and displayAll without any arguments.
Hi. Thank you so much for replying. Can you give example what is arugments? I was playing around with it.. and I tried doing this:

1
2
3
4
5
6
7
8
9
10
11
     switch(opt)
     {
        case 1: a.Inventory::Add(Inventory[], int, string, string, string, string, int, int, int, int, double);
             break;
        case 2: dA.displayAll();
             break;
        case 3:s.Search();
             break;
        default: cout << "Invalid Selection!" << endl;
                 break;
     }


But a whole lot more errors came out.. like expected primary-expresions before......
Okay, in the class Inventory you've declared the function

void Add(Inventory[], int, string, string, string, string, int, int, int, int, double);

the part in brackets is your arguments. These are what you want to pass into the function.
Looking at your code Add shouldn't take any arguments.

http://www.cplusplus.com/doc/tutorial/functions/
Hi LBEaston,

thank you so much for your reply. I was still confused so I went to ask my lecturer, and even he got confused. He said I was doing it too complicated than what I'm supposed tobe doing, so he asked me to try again.. T_T But thank you again for your help. :)
It's understandable he was confused, it's hard to follow the thought process of someone who didn't quite "get it" yet.
Last edited on
You just need void Add(Inventory[]);. The rest of the parameter is part of Inventory itself

As you use Inventory[] on line 161 it's supposed to be in main() (after line 227) and then pass it to Menu(Inventory[] product)

The functions Add(), Search(), displayAll() are not supposed to be part of the class Inventory. Only functions that deals with the member variables are supposed to be part of the class Inventory.
Ok. Thanks. Can you help me with another thing? I was going through the forum.. And I saw someone post some codes.. I tried to add a search function but it doesnt work. It only shows the first record. Any ideas why?

Here is the original post:
http://www.cplusplus.com/forum/beginner/48075/


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
void searchCD()
{
	string cdName = "";
	string artistName = "";
	string scdName = "";


	ifstream inFile;
	inFile.open("cds.txt", ios::in);


	if (inFile.is_open())
	{
		cout << "CD name: ";
		getline (cin, scdName);

		getline(inFile, cdName, '#');
		getline(inFile, artistName);

		while (scdName == cdName)
		{

			cout << cdName << ", " <<
				artistName << endl;

			getline(inFile, cdName, '#');
			getline(inFile, artistName);
		}

		//close the file
		inFile.close();
	}
	else
		cout << "File could not be opened." << endl;
	system("PAUSE");
}
One topic per question.
Hm, you need to make clear to yourself what you're doing.

line 20 means: loop as long as scdName is equal to cdName. That means it stops at the first line where it's not so. The original poster was right. You need to loop as long as it is not end of file.

line 23: You want to cout only if scdName is equal to cdName.
@hanst99

Oh.. Sorry.. My bad..

@coder777
I managed to do it after you explained it more clearly!! Thank you thank you so much!! :D :D :D
Topic archived. No new replies allowed.