ERRORS:
First-chance exception at 0x1044ad4a (msvcp100d.dll) in 17A_Project1.exe: 0xC0000005: Access violation reading location 0x005053f4.
Unhandled exception at 0x1044ad4a (msvcp100d.dll) in 17A_Project1.exe: 0xC0000005: Access violation reading location 0x005053f4.
My guess so far is that its not finding a eof bit.
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
struct cloth{
string size;
int quantity;
string color;
string notes;
};
void buildinv();
void openFile(fstream &,string);
void closeFile(fstream &,string);
void writeFile(fstream &, cloth*, int num);
int getSize();
void viewInv();
void menu();
void fillInv(cloth*,int);
void searchInv();
void sizeSearch(cloth*,int);
void colorSearch(cloth*,int);
void quantSearch(cloth*,int);
void brandSearch(cloth*,int);
const string NAME="inventory.dat";
int main(){
int choice;
do{
menu();
cin>>choice;
switch(choice){
case 1:
buildinv();
break;
case 2:
viewInv();
break;
case 3:
searchInv();
break;
case 4:
break;
}
}while(choice<5);
return 0;
}
void menu()
{
cout << "\t\t*********Inventory System*********"<<endl;
cout << "\t\t**********************************"<<endl<<endl;
cout<< "\t\t1. to input inventory"<<endl;
cout<<"\t\t2. View Inventory"<<endl;
cout<<"\t\t3. Search Inventory"<<endl;
cout<<"\t\t4. Edit Inventory"<<endl;
cout<<"\t\t5. Exit"<<endl;
}
void buildinv(){
int amt;
fstream invent;
cout<<"How many items would you like to enter? (each size and color should be a different item)"<<endl;
cin >> amt;
cloth *cptr=new cloth[amt];
for(int c=0;c<amt;c++){
cout<<"Please enter the size"<<endl;
cin >> cptr[c].size;
cout<<"Enter the color"<<endl;
cin >> cptr[c].color;
cout<<"Enter the amout"<<endl;
cin>>cptr[c].quantity;
cout<<"Enter brand"<<endl;
cin>>cptr[c].notes;
cout<<endl;
}
openFile(invent,NAME);
writeFile(invent,cptr, amt);
closeFile(invent, NAME);
}
void openFile(fstream &file, string FNAME)
{
file.open(FNAME, ios::in| ios::out|ios::binary);
if(file.fail()){
cout<<"File Failed to open"<<endl;
}
}
void closeFile(fstream &file, string FNAME)
{
file.close();
}
void writeFile(fstream &file, cloth *cptr, int num)
{
for(int c=0;c<num;c++){
file.write(reinterpret_cast<char *>(&cptr[c]), sizeof(cptr[c]));
}
}
void viewInv()
{
fstream invent;
cloth inv;
openFile(invent,NAME);
while(!invent.eof()){
invent.read(reinterpret_cast<char*>(&inv),sizeof(inv));
cout<<"Size : "<<inv.size<<endl;
cout<<"Color : "<<inv.color<<endl;
cout<<"Quantity : "<<inv.quantity<<endl;
cout<<"Brand : "<<inv.notes<<endl;
}
cout<<endl;
closeFile(invent,NAME);
}
int getSize()
{
fstream invent;
cloth inv;
openFile(invent,NAME);
int c=0;
invent.read(reinterpret_cast<char*>(&inv),sizeof(inv));
while(!invent.eof()){
c++;
invent.read(reinterpret_cast<char*>(&inv),sizeof(inv));
}
closeFile(invent,NAME);
return c;
}
void fillInv(cloth *cptr,int size)
{
fstream invent;
openFile(invent, NAME);
invent.read(reinterpret_cast<char*>(&cptr),sizeof(cptr));
for(int h=0;h<size;h++){
cin>>cptr[h].size;
cin>>cptr[h].color;
cin>>cptr[h].quantity;
cin>>cptr[h].notes;
invent.read(reinterpret_cast<char*>(&cptr),sizeof(cptr));
}
}
void searchInv()
{
int size = getSize();
fstream invent;
cloth *cptr=new cloth[size];
int choice;
fillInv(cptr,size);
do{
cout<<endl<<"1. Size Search"<<endl;
cout<<"2. Color search"<<endl;
cout<<"3. Quantity search"<<endl;
cout<<"4. Brand search"<<endl;
cout<<"5. Return"<<endl;
cin >> choice;
switch(choice){
case 1:
sizeSearch(cptr,size);
break;
case 2:
colorSearch(cptr,size);
break;
case 3:
quantSearch(cptr,size);
break;
case 4:
brandSearch(cptr,size);
break;
case 5:
break;
}
}while(choice<5);
}
void sizeSearch(cloth *cptr, int size)
{
string inp;
int k=0;
char ans;
do{
cout <<"What size?"<<endl;
cin>>inp;
for(int p=0;p<size;p++){
if(cptr[p].size == inp)
{
cout<<"Size : "<<cptr[p].size<<endl;
cout<<"Color : "<<cptr[p].color<<endl;
cout<<"Quantity : "<<cptr[p].quantity<<endl;
cout<<"Brand : "<<cptr[p].notes<<endl;
k++;
}}
if(k==0){
cout<<"No matches found"<<endl;
}
else
cout<<"Search again? (Y for Yes, N for NO"<<endl;
cin >> ans;
}while(ans=='y'||ans=='Y');
}
void colorSearch(cloth *cptr, int size)
{
string inp;
int k=0;
char ans;
do{
cout <<"What color?"<<endl;
cin>>inp;
for(int p=0;p<size;p++){
if(cptr[p].color == inp)
{
cout<<"Size : "<<cptr[p].size<<endl;
cout<<"Color : "<<cptr[p].color<<endl;
cout<<"Quantity : "<<cptr[p].quantity<<endl;
cout<<"Brand : "<<cptr[p].notes<<endl;
k++;
}}
if(k==0){
cout<<"No matches found"<<endl;
}
else
cout<<"Search again? (Y for Yes, N for NO"<<endl;
cin >> ans;
}while(ans=='y'||ans=='Y');
}
void quantSearch(cloth *cptr, int size)
{
int inp;
int k=0;
char ans;
do{
cout <<"How many?"<<endl;
cin>>inp;
for(int p=0;p<size;p++){
if(cptr[p].quantity >= inp)
{
cout<<"Size : "<<cptr[p].size<<endl;
cout<<"Color : "<<cptr[p].color<<endl;
cout<<"Quantity : "<<cptr[p].quantity<<endl;
cout<<"Brand : "<<cptr[p].notes<<endl;
k++;
}}
if(k==0){
cout<<"No matches found"<<endl;
}
else
cout<<"Search again? (Y for Yes, N for NO"<<endl;
cin >> ans;
}while(ans=='y'||ans=='Y');
}
void brandSearch(cloth *cptr, int size)
{
string inp;
int k=0;
char ans;
do{
cout <<"What brand?"<<endl;
cin>>inp;
for(int p=0;p<size;p++){
if(cptr[p].notes == inp)
{
cout<<"Size : "<<cptr[p].size<<endl;
cout<<"Color : "<<cptr[p].color<<endl;
cout<<"Quantity : "<<cptr[p].quantity<<endl;
cout<<"Brand : "<<cptr[p].notes<<endl<<endl;
k++;
}}
if(k==0){
cout<<"No matches found"<<endl;
}
else
cout<<"Search again? (Y for Yes, N for NO"<<endl;
cin >> ans;
}while(ans=='y'||ans=='Y');
}

New Topic/Question
Reply



MultiQuote









|