structure inventory program

I am writing a data structure program to store inventory data in a file, then I need to read and display that information. The problem is, is I am getting an error that i can not find where the problem is. Can any one help? Any kind of help would be appreciated.

This is my error:

error LNK2019: unresolved external symbol "void __cdecl viewRec(class std::basic_fstream<char,struct std::char_traits<char> > &)" (?viewRec@@YAXAAV?$basic_fstream@DU?$char_traits@D@std@@@std@@@Z) referenced in function _main
1>H:\C++\stream objects\Chapter12\Debug\Chapter12.exe : fatal error LNK1120: 1 unresolved externals

Here is my code:

#include<iostream>
#include<fstream>
#include<iomanip>
#include<cctype>

using namespace std;

//void main()

const int DESC_SIZE = 51; // holds inventory description size
const int DATE_SIZE = 11; // holds date size

struct InventoryItem // inventory structure

{

char desc[DESC_SIZE]; // description holds 31 charaters
int quantity; // variable to hold quantity
double whlCost; // variable to hold wholesale cost
double rtlCost; // variable to hold retail cost
char date[DATE_SIZE]; // variable to hold date

};

int main()
{

void addRec(fstream &); // function prototype to add a record
void viewRec(fstream &); // function prototype to view a record
void chgRec(fstream &); // function prototype to change a record


{

long selection; // variable to hold menu selection
long recNum; // variable to hold the record number of the inventory item

fstream inventory ("Inventory.dat", ios::in | ios::out | ios::binary);
InventoryItem record = {" ", 0, 0.0};

cout << fixed << showpoint << setprecision(2);
cout << "Inventory Managment"<<endl;


for (int count = 0; count < 5; count++) // write the blank records

{

inventory.write(reinterpret_cast<char *>(&record),sizeof(record));

}

inventory.close();

inventory.open("Inventory.dat", ios::out | ios::binary);

while (selection != 4)

{
switch (selection)
{

case 1: // Add a new record

{
addRec(inventory);
break;
}

case 2: //View record

{
viewRec(inventory);
break;
}

case 3: //Change record

{
chgRec(inventory);
break;
}

default: //Invalid selection

{
cout << "Invalid selection" << endl;
}

selection = menu();

}

}

cout<<"Hava a nice day."<<endl;
inventory.close();
system("pause");

return 0;

}
int menu()
{
int choice;

cout << "Please make a selection, 1 through 4." << endl;
cout << "1. Add a new record"<<endl;
cout << "2. View an exisitng record"<<endl;
cout << "3. Change an exisitng record"<<endl;
cout << "4. Exit"<<endl;
cout << endl;
cout << "Enter your choice (1-4): ";

cin>>choice;

while(choice < 1 || choice > 4)

{
cout<<"Invaild selection!"<<endl;
cout<<"Please enter your choice (1-4) : ";

cin>>choice;
}

cout << endl;
return choice;

}

void addRec(fstream &file) // function to add new information to inventory

{
cout<<"Enter the following inventory data:"<<endl;

fstream inventory("Inventory.dat", ios::out | ios::binary);

InventoryItem record;

//Enter new data

cout << "Description: ";

cin.ignore();
cin.getline(record.desc, 51);
cin >> record.desc;

cout < <"Quantity: ";
cin >> record.quantity;

cout << "Wholesale cost: ";
cin >> record.whlCost;

cout << "Retail price: ";
cin >> record.rtlCost;

cout << "Date added to inventory (in 00/00/0000 format): ";
cin >> record.date;

inventory.write(reinterpret_cast<char *>(&record),sizeof(record));
cout<<"Record added to file."<<endl;

file.close();

}

void viewRec(fstream &file) // function to view record

{

fstream inventory ("Inventory.dat", ios::out | ios::binary);
InventoryItem record;

long recNum;

cout << "Enter the record number of the item to view:";
cin >> recNum;

// Move to the record and read it.

inventory.seekg(recNum * sizeof(record), ios::beg);
inventory.read(reinterpret_cast<char *>(&record),sizeof(record));

cout << "Description: " << record.desc << endl;
cout << "Quantity: " << record.quantity << endl;
cout << "Wholesale cost: " << record.whlCost << endl;
cout << "Retail price: " << record.rtlCost << endl;
cout << "Date (in 00/00/0000 format): " << record.date << endl;

if(file.fail())
file.clear();
file.close();

}
void chgRec(fstream &file) // function to change a record
{

fstream inventory ("InventoryFile.dat", ios::out | ios::binary);
InventoryItem record;

long recNum;

cout << "Enter the record number of the item you want to edit: ";
cin >> recNum; // Move to the record and read it.

inventory.seekg(recNum * sizeof(record), ios::beg);
inventory.read(reinterpret_cast<char *>(&record),sizeof(record));

cout << "Description: " << record.desc << endl;
cout << "Quantity: " << record.quantity << endl;
cout << "Wholesale cost: " << record.whlCost << endl;
cout << "Retail price: " << record.rtlCost << endl;
cout << "Date (in 00/00/0000 format): " << record.date << endl;
cout << endl;

// Move back to the beginning of this record's position

inventory.seekp(recNum * sizeof(record), ios::beg);

// Write new record over the current record

// inventory.write(reinterpret_cast<char *>(&record),sizeof(record));

// Close the file.

inventory.close();
}

system("pause");
return 0;

}
You seem to have put the prototypes and definitions of your functions addRec, chgRec, ViewRec inside your main function. You cannot declare or define functions within other functions. You should move them outside of the main function.

Also, you should use [code]code[/code] tags to make your code more readable.

Hope this helps.
Thank you, I will change that and see what happens. Thank you for the help.
Now I am getting: 'menu' identifier not found.

this is a snippet of what I have :

{
cout << "Invalid selection" << endl;
}

-> selection = menu(); Here is where I am getting the error.

}

}

cout << "Hava a nice day." << endl;
inventory.close();
system("pause");

return 0;

}
int menu()
{
int choice;

cout << "Please make a selection, 1 through 4." << endl;
cout << "1. Add a new record"<<endl;
cout << "2. View an exisitng record"<<endl;
cout << "3. Change an exisitng record"<<endl;
cout << "4. Exit"<<endl;
cout << endl;
cout << "Enter your choice (1-4): ";

cin>>choice;

try putting int menu(); before main().
you need to declare your functions before main, the alternative is to define your functions before main.

p/s do learn how to use code tags
OK, I declared int menu() before main but it had no affect. I don't know, I have been working on this for weeks and i just am not seeing the mistakes.
So post your current code again, this time with code tags
I am sorry, here is my code, with tags, i apologize for that. Again, I moved my int menu() out of my main, but it is still not working. This is my first C++ class and any help or suggestions on how to get through this stuff would be great. I normally don't struggle this much and for what ever reason, it just is not clicking.

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

#include <iostream>  
#include <fstream>  
#include <iomanip>  
#include <cctype>  

 using namespace std;  

   
 const int DESC_SIZE = 51;										      // holds inventory description size  
 const int DATE_SIZE = 11;										      // holds date size  
    

 // Declaration of InventoryItem structure  

 struct InventoryItem											      // inventory structure  

 {  
     char desc[DESC_SIZE];											// description holds 31 charaters 
     int quantity;													// variable to hold quantity  
     double whlCost;												// variable to hold wholesale cost  		
     double rtlCost;												 // variable to hold retail cost  
     char date[DATE_SIZE];											 // variable to hold date   
	 int menu();
 };   

 void addRec(fstream &);											// function prototype to add a record  
 void dispRec(fstream &);											 // function prototype to view a record  
 void chgRec(fstream &);											// function prototype to change a record		


 int main()  

 { 
    long selection;												   // variable to hold menu selection  
    long recNum;												   // variable to hold the record number of the inventory item  

    fstream inventory ("Inventory.dat", ios::in | ios::out | ios::binary);  
    InventoryItem record = {" ", 0, 0.0};  

       
    cout << fixed << showpoint << setprecision(2);  
    cout<<"Inventory Managment"<<endl;  

    // write the blank records  
    for (int count = 0; count < 5; count++)  

    {  

       inventory.write(reinterpret_cast<char *>(&record),sizeof(record));  

    }  

    inventory.close();  

    inventory.open("Inventory.dat", ios::out | ios::binary);  

    while (selection != 4)  

    {  
        switch (selection)  
        {  

		case 1:															 // Add a new record  

            {  
                addRec(inventory);  
                break;  
            }  

        case 2:															 //View record  

            {  
			    dispRec(inventory);  
                break;  
            }  

        case 3:															//Change record  

            {  
                chgRec(inventory);  
                break;  
            }  

        default:														//Invalid selection  

            {  
                cout << "Invalid selection" << endl;  
            }  

            selection = menu();  
        }  

    }               

    cout << "Hava a nice day." << endl;  
    inventory.close();      

    system("pause");  
    return 0;  

 }  

 int menu()  

 {  
	 long selection;
     int choice;  
	 selection = menu(); 

     cout << "Please make a selection, 1 through 4." << endl; 
     cout << "1. Add a new record" << endl;  
     cout << "2. View an exisitng record" << endl;  
     cout << "3. Change an exisitng record" << endl;  
     cout << "4. Exit" << endl;  
     cout << endl;  
     cout <<"Enter your choice (1-4): ";  
     cin >> choice;  
     while(choice < 1 || choice > 4)  

     {  
         cout << "Invaild selection!" << endl;  
         cout << "Please enter your choice (1-4) : ";  
         cin >> choice;  
     }  
     cout << endl;  

     return choice;  

 }  
 
 void addRec(fstream &file)											// function to add new information to inventory 

 {  

     cout << "Enter the following inventory data:"<<endl;  
     fstream inventory("Inventory.dat", ios::out | ios::binary);  
     InventoryItem record;  

        //Get new data  

     cout << "Description: ";  
     cin.ignore();  

     cin.getline(record.desc, 51);  
     cin >> record.desc;  

     cout << "Quantity: ";  
     cin >> record.quantity;  

    cout << "Wholesale cost: ";  
     cin >> record.whlCost;  

     cout << "Retail price: ";  
     cin >> record.rtlCost; 

     cout << "Date added to inventory (in 00/00/0000 format): ";  
     cin >> record.date;  

	 inventory.write(reinterpret_cast<char *>(&record),sizeof(record));  
     cout << "Record added to file."<<endl;  

     file.close();  

 }  
    

 // function to view record  

 void dispRec(fstream &file)  

 {  

     fstream inventory ("Inventory.dat", ios::out | ios::binary);  
     InventoryItem record;  
     long recNum;  
     cout << "Enter the record number of the item to view:";  
     cin >> recNum;  

     // Move to the record and read it.  

     inventory.seekg(recNum * sizeof(record), ios::beg);  
     inventory.read(reinterpret_cast<char *>(&record),sizeof(record));  
    
     cout << "Description: " << record.desc << endl;  
     cout << "Quantity: " << record.quantity << endl;  
     cout << "Wholesale cost: " << record.whlCost << endl;  
     cout << "Retail price: " << record.rtlCost << endl;  
     cout << "Date (in 00/00/0000 format): " << record.date << endl;  
     cout << endl;  

   

     if(file.fail())  
     file.clear();  
     file.close();  

 }  

    

 // function to change a record  

 void chgRec(fstream &file)  

 {  

     fstream inventory ("InventoryFile.dat", ios::out | ios::binary);  
     InventoryItem record;   
     long recNum;  
     cout << "Enter the record number of the item you want to edit: ";  
     cin >> recNum;											// Move to the record and read it.  
     inventory.seekg(recNum * sizeof(record), ios::beg);  
     inventory.read(reinterpret_cast<char *>(&record),sizeof(record));  

     cout << "Description: " << record.desc << endl;  
     cout << "Quantity: " << record.quantity << endl;  
     cout << "Wholesale cost: " << record.whlCost << endl;  
     cout << "Retail price: " << record.rtlCost << endl;  
     cout << "Date (in 00/00/0000 format): " << record.date << endl;  
     cout << endl;  

     // Move back to the beginning of this record's position  

     inventory.seekp(recNum * sizeof(record), ios::beg);  

     // Write new record over the current record  

     inventory.write(reinterpret_cast<char *>(&record),sizeof(record));  

     // Close the file.  

     inventory.close();   
}
int menu(); is inside of your inventory struct. This is fine but to define int menu, you need to use the scope operator "::".
Line 104 should be: int InventoryItem::menu()
Thank you, I made a few changes, but the int menu(); section is really raising a lot of problems. I am not sure if there is an easier way to handle this area. Now I am getting a stack overflow problem and a recursive error with in the int menu() area.

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

#include <iostream>  
#include <fstream>  
#include <iomanip>  
#include <cctype>  

 using namespace std;  

   
 const int DESC_SIZE = 51;								// holds inventory description size  
 const int DATE_SIZE = 11;							    // holds date size  
    

 // Declaration of InventoryItem structure  

 struct InventoryItem								    // inventory structure catagories 

 {  
     char desc[DESC_SIZE];								// description holds 31 charaters 
     int quantity;										// variable to hold quantity  
     double whlCost;									// variable to hold wholesale cost  		
     double rtlCost;									// variable to hold retail cost  
     char date[DATE_SIZE];								 // variable to hold date
	 int menu();
	 
 };   

 void addRec(fstream &);								// function prototype to add a record  
 void dispRec(fstream &);								// function prototype to view a record  
 void chgRec(fstream &);								// function prototype to change a record		


 int main()  

 { 
    long selection;										 // variable to hold menu selection  
    //long recNum;										 // variable to hold the record number of the inventory item  
	int menu();

    fstream inventory ("Inventory.dat", ios::in | ios::out | ios::binary);  
    InventoryItem record = {" ", 0, 0.0};  

       
    cout << fixed << showpoint << setprecision(2);  
    cout<<"Inventory Managment"<<endl;  

    // write the blank records  
    for (int count = 0; count < 5; count++)  

    {  

       inventory.write(reinterpret_cast<char *>(&record),sizeof(record));  

    }  

    inventory.close();  

    inventory.open("Inventory.dat", ios::out | ios::binary);  
	
		 while (selection != 4) 

    {  
       switch(selection)  
        {  

		case 1:													 // Add a new record  

            {  
                addRec(inventory);  
                break;  
            }  

        case 2:													 //View record  

            {  
			    dispRec(inventory);  
                break;  
            }  

        case 3:													//Change record  

            {  
                chgRec(inventory);  
                break;  
            }  

        default:												//Invalid selection  

            {  
                cout << "Invalid selection" << endl;  
            }  

            selection = menu();  
        }  

    }               
  
    inventory.close();      

    system("pause");  
    return 0;  

 }  

 int menu() 

 {  
	 long selection;
     int choice;  
	 selection = menu(); 

     cout << "Please make a selection, 1 through 4." << endl; 
     cout << "1. Add a new record" << endl;  
     cout << "2. View an exisitng record" << endl;  
     cout << "3. Change an exisitng record" << endl;  
     cout << "4. Exit" << endl;  
     cout << endl;  
     cout <<"Enter your choice (1-4): ";  
     cin >> choice;  
     while(choice < 1 || choice > 4)  

     {  
         cout << "Invaild selection!" << endl;  
         cout << "Please enter your choice (1-4) : "<< endl;  
         cin >> choice;
         
	 }
	 system("pause");
     return choice;  

 }  
 
 
 void addRec(fstream &file)											// add new information to inventory 

 {  

     cout << "Enter the following inventory data:"<<endl;  
     fstream inventory("Inventory.dat", ios::out | ios::binary);  
     InventoryItem record;  

        //Get new data  

     cout << "Description: ";  
     cin.ignore();  

     cin.getline(record.desc, 51);  
     cin >> record.desc;  

     cout << "Quantity: ";  
     cin >> record.quantity;  

     cout << "Wholesale cost: ";  
     cin >> record.whlCost;  

     cout << "Retail price: ";  
     cin >> record.rtlCost; 

     cout << "Date added to inventory (in 00/00/0000 format): ";  
     cin >> record.date;  

	 inventory.write(reinterpret_cast<char *>(&record),sizeof(record));  
     cout << "Record added to file."<<endl;  

     file.close();  

 }  
    

 // view record  

 void dispRec(fstream &file)  

 {  

     fstream inventory ("Inventory.dat", ios::out | ios::binary);  
     InventoryItem record;  
     long recNum;  
     cout << "Enter the record number of the item to view:";  
     cin >> recNum;  

     // Go to desired record and read it.  

     inventory.seekg(recNum * sizeof(record), ios::beg);  
     inventory.read(reinterpret_cast<char *>(&record),sizeof(record));  
    
     cout << "Description: " << record.desc << endl;  
     cout << "Quantity: " << record.quantity << endl;  
     cout << "Wholesale cost: " << record.whlCost << endl;  
     cout << "Retail price: " << record.rtlCost << endl;  
     cout << "Date (in 00/00/0000 format): " << record.date << endl;  
    
     if(file.fail())  
     file.clear();  
     file.close();  

 }  

   void chgRec(fstream &file)								//  Change a record 

 {  

     fstream inventory ("InventoryFile.dat", ios::out | ios::binary);  
     InventoryItem record;   
     long recNum;  
     cout << "Enter the record number of the item you want to edit: ";  
     cin >> recNum;											//Go to record and read it.  
     inventory.seekg(recNum * sizeof(record), ios::beg);  
     inventory.read(reinterpret_cast<char *>(&record),sizeof(record));  

     cout << "Description: " << record.desc << endl;  
     cout << "Quantity: " << record.quantity << endl;  
     cout << "Wholesale cost: " << record.whlCost << endl;  
     cout << "Retail price: " << record.rtlCost << endl;  
     cout << "Date (in 00/00/0000 format): " << record.date << endl;    

     // Go to the beginning of this record's position  

     inventory.seekp(recNum * sizeof(record), ios::beg);  

     // Overwrite current record  

     inventory.write(reinterpret_cast<char *>(&record),sizeof(record));  

	 //Close

     inventory.close();   
}
line 38: you already declared int menu() inside of struct InventoryItem. Also, you cannot declare functions inside of functions.

line 93: this should be selection = record.menu(); This way, you are accessing the function member of your record object.

line 105: int menu needs to be declared like: int InventoryItem::menu()

line 110: this should be in main. Right now, it is creating an infinite loop because every time menu() is called, it calls itself again before it has a chance to get to the return statement.
Topic archived. No new replies allowed.