Phone Database Program - Need to Call One Function to Another

Hello, I am trying to write a mini-phone database program that can display phones based on name, productID, add them, delete them so forth. So I managed to complete most of it. However, I would like to ask a few questions.

1) How can I use function query_database to call function printArray if the user input matches data within the database. Everytime I do it, it gives me this:
no matching function for call to 'printArray()'|
.

2) If the user chooses price, how can I allow them to enter a range of prices and list all info with phones that match the price.

3) Finally, how can I make the user put in the productID only in this format: AA00. Where the first two characters are letters and the last two are numbers.

My code is below:
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
int findID (char [][5], string, int&);
double buyPhone(int, double, int&);
double updatePrice(double[], int);
void printArray(char [][5], string[], double[], int[], int*);
void query_database(char [][5], string[], double [], int[], int);
void exit();

int main()
{
    string name[1000];
    char productID[1000][5];
    double price[1000];
    int quantityonHand[1000];
    int n = 0;
    string lookID;

  bool r = true;

  while (r = true)
  {

    int choice;
     cout << "\n \t\t\t ====== PHONE DATABASE ======";
     cout <<"\n\n                                          ";
     cout << "\n\n";
     cout<<" \n\t\t\t ======================";
     cout << "\n \t\t\t  1. Start  Program";
     cout << "\n \t\t\t  2. Find   Phone";
     cout << "\n \t\t\t  3. Add    Phone";
     cout << "\n \t\t\t  4. List   Phone";
     cout << "\n \t\t\t  5. Advanced Search";
     cout << "\n \t\t\t  6. Delete Phone";
     cout << "\n \t\t\t  7. Exit   Program";
     cout <<" \n\t\t\t ======================";
     cout << "\n\n";
     cout << "\t\t\t Select Your Choice ::";
     cin >> choice;

     switch (choice){
    case 1:
         {
   cout<<"How much info do you need to enter?"<<endl;
    cin>>n;
    cout<<endl;

    while (!cin>> n )
    {   cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
     cout<<"Invalid input. Please try again."<<endl;
     cout<<endl;
     cout<<"How much info do you need to enter?"<<endl;
     cin>>n;
    cout<<endl;
    }


for (int c=0; c<n; c++)
{
    cout<< "What is the name of your phone?"<<endl;
    cin>> name[c];
    cout<< "Please enter the product ID."<<endl;
    cin>> productID[c];

    for(int b=0; b<4; b++)
    {
        while (strlen(productID[c])>4 || !((productID[c][b]>='A' && productID[c][b]<='Z')||(productID[c][b]>='a' && productID[c][b]<='z')) )

        {
            cout<<"It has to be four characters."<<endl;
            cin>>productID[b];
        }
    }


       cout<<endl;

       cout<<"Please enter the price for the phone."<<endl;
        cin>>price[c];
                cout<<endl;
        while(price[c]<=0)
            {
        cout<<"Please put in a price larger than 0."<<endl;
            cin>>price[c];
                cout<<endl;
                }

    cout<<"How many phones do you have at the moment?"<<endl;
        cin>>quantityonHand[c];

            if(quantityonHand[c]>0)
            {
                cout<<"You have "<<quantityonHand[c]<<" copies of this model."<<endl;
            }
               else if (quantityonHand[c]<0)
                        {
                    cout<<"You have no more copies of this model."<<endl;
                        }
                        cout<<endl<<endl;

                            buyPhone(quantityonHand[c], price[c], quantityonHand[c]);
                                    updatePrice(price, c);
                                            }


         }


            case 2:
            {

                string point;
                            cout<<"Would you like to continue?"<<endl;
                        cin>>point;
                            if (point == "yes" || point=="y")
                                    {
                                     findID(productID, lookID, n);

                                    }
                                     else
                                        cout<<endl;
                                        break;

            }


case 4:
         {

             printArray (productID, name, price, quantityonHand, &n);
             break;
         }

case 5:
    {
        query_database(productID, name, price, quantityonHand, n);
    }

          case 7:
              {
                r = false;
                  exit();
                    return 0;
                }

     }


  }

}

    int findID(char x[][5], string z, int &r)
    {
        string exit;
        while(exit!="yes" || exit!="y")
        {


            cout<<"Please enter the productID you want to look up."<<endl;
            cin>>z;
            for(int w=0; w<r; w++)
                {
                    if(x[w]==z)
                    {
                    cout<<"Found! Your product ID is found in database " <<(w+1)<<"."<<endl<<endl;
                    }
                    else
                        cout<<"Product not found."<<endl;
                }
                    cout << "Would you like to exit?"<<endl;
                                    cin >> exit;
                                    if (exit == "yes" || exit=="y")
                                                break;
    }}

void printArray(char arr[][5], string n, double b[], int c[], int *w)

{

    for ( int i = 0; i <*w; i++)
        {
        cout<< "Name: " << n[i] << ' '<<endl;
        cout<<"ProductID: "<< arr[i] << ' '<<endl;
        cout <<"Price: " <<"$"<<b[i]<<' '<<endl;
        cout <<"Amount: "<<c[i]<<' '<<endl;
        cout<<endl<<endl;
    }
    cout << endl;

}

double updatePrice(double wer[], int e)
{
    string s;
    cout<<"Would you like to change the price? Please enter y or yes as input."<<endl;
    cin>>s;
    if(s=="y" || s=="yes")
    {


                    cout<<"Enter new price :";
                        cin>>wer[e];
                            cout<<"Price Updated...  Successfully "<<endl;
    }
                     else
                    cout<<"Price change canceled."<<endl<<endl;

}

    double buyPhone(int quant, double total, int &lft)
    {

        int *crt=&lft;
        string output;
        if(quant!=0)
        {
        cout<<"Would you like to buy this model?"<<endl;
        cin>>output;
        if(output=="y" || output=="yes")
        {
            cout<<"How many would you like to buy?"<<endl;
            cin>>quant;
            if(quant>=*crt){
                cout<<"There are not enough of these models available. Please accept what's left."<<endl;
                    cin>>quant;}
            cout<<"Your total cost is $"<<(total=total*quant)<<". Thank you for shopping with us."<<endl;
            cout<<endl;
            cout<<"There are now "<<(*crt=lft-quant)<<" version(s) of this model left."<<endl;
        }
                else
                    cout<<"Transaction canceled."<<endl<<endl;
        }
    }

    void query_database(char ar[][5], string gt[], double p[], int c[], int amt)
    {
        string q;
        string name;
        string id;
        string price;
       cout<<"Do you want to search by name, product ID, or price?"<<endl;
       cin >> q;

       if(q == "name")
       {
           cout<<"Please enter the name you want to look for?"<<endl;
           string name;
           for(int i=0 ; i<amt; i++)
           {
               if(name == gt[i])
             printArray ();
                else
                    cout<<"Not found."<<endl;
           }

       }
    }
void exit()
{
        cout << "\n \t\t\t *******    T H A N K  Y O U    *******" << endl;
        cout << "\n \t\t\t *******    F O R  U S I N G    *******" << endl;
        cout << "\n \t\t\t ******* T H I S  P R O G R A M *******" << endl;
}
Last edited on
Line 251.
printArray ();

printArray takes paramters. You need to put something in those brackets:

printArray ( PARAMETERS GO HERE );
Have you considered using a structure or class to hold the information instead of all of those parallel arrays? Also instead of arrays your should consider using std::vector. And instead of C-strings you should stick to using std::strings.

You also seem to be using the same names for functions and variables, this is really a bad practice and can get confusing rather quickly. Also having a function named exit() is not a good idea since there is a standard function that already has that name.

As far as your problem with print_array() your error messages should be giving you much more information that should help you solve that issue.

Here are the error messages the online compiler issues about your code:
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
1:25: error: 'string' has not been declared 
4:29: error: 'string' has not been declared 
5:33: error: 'string' has not been declared In function 'int main()': 
10:5: error: 'string' was not declared in this scope 
15:12: error: expected ';' before 'lookID' 
19:14: warning: suggest parentheses around assignment used as truth value [-Wparentheses] 
23:6: error: 'cout' was not declared in this scope 
37:6: error: 'cin' was not declared in this scope 
42:49: error: 'endl' was not declared in this scope 
48:20: error: 'numeric_limits' was not declared in this scope 
48:35: error: 'streamsize' was not declared in this scope 
48:46: error: '::max' has not been declared 
60:11: error: 'name' was not declared in this scope 
66:35: error: 'strlen' was not declared in this scope 
111:24: error: expected ';' before 'point' 
112:66: error: 'endl' was not declared in this scope 
113:30: error: 'point' was not declared in this scope 
116:56: error: 'lookID' was not declared in this scope 
129:37: error: 'name' was not declared in this scope 
135:35: error: 'name' was not declared in this scope At global scope: 
152:29: error: 'string' has not been declared In function 'int findID(char (*)[5], int, int&)': 
154:9: error: 'string' was not declared in this scope 
155:21: warning: comparison with string literal results in unspecified behaviour [-Waddress] 
155:21: error: comparison between distinct pointer types 'void (*)()' and 'const char*' lacks a cast [-fpermissive] 
155:36: warning: comparison with string literal results in unspecified behaviour [-Waddress] 
155:36: error: comparison between distinct pointer types 'void (*)()' and 'const char*' lacks a cast [-fpermissive] 
159:13: error: 'cout' was not declared in this scope 
159:70: error: 'endl' was not declared in this scope 
160:13: error: 'cin' was not declared in this scope 
163:30: error: ISO C++ forbids comparison between pointer and integer [-fpermissive] 
172:49: warning: comparison with string literal results in unspecified behaviour [-Waddress] 
172:49: error: comparison between distinct pointer types 'void (*)()' and 'const char*' lacks a cast [-fpermissive] 
172:64: warning: comparison with string literal results in unspecified behaviour [-Waddress] 
172:64: error: comparison between distinct pointer types 'void (*)()' and 'const char*' lacks a cast [-fpermissive] 
174:6: warning: no return statement in function returning non-void [-Wreturn-type] At global scope: 
176:32: error: 'string' has not been declared In function 'void printArray(char (*)[5], int, double*, int*, int*)': 
182:9: error: 'cout' was not declared in this scope 
182:31: error: invalid types 'int[int]' for array subscript 
182:41: error: 'endl' was not declared in this scope 
188:5: error: 'cout' was not declared in this scope 
188:13: error: 'endl' was not declared in this scope In function 'double updatePrice(double*, int)': 
194:5: error: 'string' was not declared in this scope 
195:5: error: 'cout' was not declared in this scope 
195:82: error: 'endl' was not declared in this scope 
196:5: error: 'cin' was not declared in this scope 
196:10: error: 's' was not declared in this scope 
208:1: warning: no return statement in function returning non-void [-Wreturn-type] In function 'double buyPhone(int, double, int&)': 
214:9: error: 'string' was not declared in this scope 
217:9: error: 'cout' was not declared in this scope 
217:52: error: 'endl' was not declared in this scope 
218:9: error: 'cin' was not declared in this scope 
218:14: error: 'output' was not declared in this scope 
233:5: warning: no return statement in function returning non-void [-Wreturn-type] At global scope: 
235:39: error: 'string' has not been declared In function 'void query_database(char (*)[5], int*, double*, int*, int)': 
237:9: error: 'string' was not declared in this scope 
238:16: error: expected ';' before 'name' 
239:16: error: expected ';' before 'id' 
240:16: error: expected ';' before 'price' 
241:8: error: 'cout' was not declared in this scope 
241:70: error: 'endl' was not declared in this scope 
242:8: error: 'cin' was not declared in this scope 
242:15: error: 'q' was not declared in this scope 
247:19: error: expected ';' before 'name'
250:19: error: 'name' was not declared in this scope 
251:26: error: no matching function for call to 'printArray()' 
251:26: note: candidates are: 4:6: note: void printArray(char (*)[5], int*, double*, int*, int*) 
4:6: note: candidate expects 5 arguments, 0 provided 
176:6: note: void printArray(char (*)[5], int, double*, int*, int*) 
176:6: note: candidate expects 5 arguments, 0 provided At global scope: 
235:36: warning: unused parameter 'ar' [-Wunused-parameter] 
235:61: warning: unused parameter 'p' [-Wunused-parameter] 
235:70: warning: unused parameter 'c' [-Wunused-parameter] In function 'void exit()': 
260:9: error: 'cout' was not declared in this scope 
260:71: error: 'endl' was not declared in this scope

Most of the errors are being caused by the lack of your #include files but if you study the error messages you should see some regarding the print function.

@Repeater Which parameters, because I tried putting in the local variables from the function and it gives me this:

 invalid conversion from 'int' to 'int*' [-fpermissive]|
 initializing argument 5 of 'void printArray(char (*)[5], std::__cxx11::string*, double*, int*, int*)'|
@jib the #include files I didn't add them because there was no more space to add them onto the post.

I have to use C-strings as ordered.
If you have to use C-strings why are you also using std::strings?

I have to use both types/
Last edited on
I suggest you post your assignment text, since you seem to have some seemingly bizarre limitations.

@jib

In the second part of this assignment, you will:
Add a menu item allowing the user to delete a phone.
Allow lookup queries by different criteria.
Change the productID to be a 4 character C-string. The first 2 should be alphabetical characters (A-Z) and the last 2 should be digits (0-9). You will use C-string functions to compare two productID's. The array of productID's will be a 2D array (viewed as a 1d array of C-strings).
Use dynamic memory allocation to allocate the arrays, and then double the size of the array if it gets filled. (Note: You can use dynamic memory allocation for only one array and you will still get full credit. E.C. to use for all arrays.)
Add another array of type C++ string to store the names of the phones.
Necessary Functions

Here are some of the new functions that should be written:
delete_phone: Given a product number, remove this phone from the database. This function should call find_phone to locate the location of the phone in the array. It should then remove the phone from the array if the quantity on hand is 0. This can be implemented in one of several ways (some choices are: (a)you can shift over everything else in the array (b) you can place a special value in the productnum array at that location indicating that it is empty.)

Some things to think about: you most likely will have to maintain the number of phones in the database. Be sure to update this when both adding and deleting. If you use option b, you will have to change your add_phone function to find an empty entry in the array.

query_database: This function should have a submenu allowing the user to query the database by product number, name, or price. If the user chooses product number or name, you will call find_phone and then display_phone.If the user chooses price, allow them to enter a range of prices.

grow_array: This function will enlarge the size of the array using dynamic memory allocation. (Hint: we wrote a similar function in class.)
Error Checking

Some new things to check for: if the user tries to delete a phone that is not in your database, or if the user enters an invalid product number.
Since your must use the horrible C-strings you need to be very careful about buffer overruns. Look at this snippet:
1
2
3
4
    cout<< "Please enter the product ID."<<endl;
    cin>> productID[c];
...
        while (strlen(productID[c])>4 || !((productID[c][b]>='A' && productID[c][b]<='Z')||(productID[c][b]>='a' && productID[c][b]<='z')) )


If that first item in your while statement evaluates to true, you've already overran your buffer and are now into Undefined Behavior. You should never Never NEVER use a function that retrieves a C-string that doesn't limit the number of characters it will try to retrieve. With the extraction operator you should use the setw() manipulator to limit the input:

cin >> setw(size of your array) >> productID[c];

This will not prevent the user from entering more or less than the limit but it will prevent the extraction of more than the desired number of characters.



Last edited on
So, I should use format it like this:

cin >> setw(1000) >> productID[c];


Sorry I am pretty new to C++.
What is the size of your C-string? Is it 1000? Hint: No it's not 1000.

Oh wait its 4.
Was it?
char productID[1000][5];
sorry so 5.

if that is the case: then I should implemented it like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for (int c=0; c<n; c++)
{
    cout<< "What is the name of your phone?"<<endl;
    cin>> name[c];
    cout<< "Please enter the product ID."<<endl;
    cin>> setw(5) >> productID[c];

    for(int b=0; b<4; b++)
    {
        while (strlen(productID[c])>4 || !((productID[c][b]>='A' && productID[c][b]<='Z')||(productID[c][b]>='a' && productID[c][b]<='z')) )

        {
            cout<<"It has to be four characters."<<endl;
            cin>> productID[b];
        }
    }
The cin statement looks correct, as long as 'c' is an index to the array of C-strings. Using meaningful variable names would help.

What are you actually trying to do in those last two loops?

Well I am trying to only allow 4 characters for the productID, but have the user put as much info as they ask, up to 1000 times.

I would prefer the first two characters of the ID to be letters and the last two numbers.
Well I am trying to only allow 4 characters for the productID,

Okay that's what the setw() is doing.

but have the user put as much info as they ask, up to 1000 times.

But isn't this the job of the first for() loop?

I would prefer the first two characters of the ID to be letters and the last two numbers.

Is this what you're attempting to do in the last two loops? If it is it probably isn't working correctly.

Also do you know about the functions like isalpha(), isdigit(), etc. that are defined in <cctype>?


Yea the first loop is doing that.
No 4 characters, based on x amount of times user inputs. I would want for the first two characters to be letters and the last two numbers.
Topic archived. No new replies allowed.