12 Replies - 1013 Views - Last Post: 28 October 2011 - 11:21 PM Rate Topic: -----

#1 sdla4ever  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 26-September 11

[c++] Inventory Project "Access Violation"

Posted 27 October 2011 - 04:26 PM

OK so I decided to try the inventory system project and so far have had great results. got the majority of the code typed out. It compiles but now and then will spit a Access Violation error.

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');
}



Is This A Good Question/Topic? 0
  • +

Replies To: [c++] Inventory Project "Access Violation"

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: [c++] Inventory Project "Access Violation"

Posted 27 October 2011 - 04:31 PM

where did the debugger say the program crashed?
Was This Post Helpful? 0
  • +
  • -

#3 sdla4ever  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 26-September 11

Re: [c++] Inventory Project "Access Violation"

Posted 27 October 2011 - 04:37 PM

I'm not entirely sure how to answer your question, but it throws those violations when I hit the viewinv or search inv, so only when it trys to read a file in.

If i hit break when it pops up it takes me to a & Myproxy and My firstiter code
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1675
  • View blog
  • Posts: 3,169
  • Joined: 30-May 10

Re: [c++] Inventory Project "Access Violation"

Posted 28 October 2011 - 01:35 AM

007 struct cloth{
008 string size;
009 int quantity;
010 string color;
011 string notes;
012 };
You can't just read/write non-POD data like std::string to a file as a crude block of data.

If you print sizeof(string), it will be something small like 4 or 8 (it's 8 on my machine). Obviously, this means the actual string itself is somewhere hidden away inside the class. Simple file.write() will just write those few bytes and NOT the whole string.

Use either
struct cloth{
	char size[50];
	int quantity;
	char color[20];
	char notes[200];
};


and make sure you don't overflow your char arrays.



Or read all about
https://encrypted.go...B+serialization
Was This Post Helpful? 2
  • +
  • -

#5 sdla4ever  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 26-September 11

Re: [c++] Inventory Project "Access Violation"

Posted 28 October 2011 - 02:23 PM

I did as you said and it worked the first time through but now when I hit view inventory it spits back null data.
Was This Post Helpful? 0
  • +
  • -

#6 sdla4ever  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 26-September 11

Re: [c++] Inventory Project "Access Violation"

Posted 28 October 2011 - 02:31 PM

EDIT: Fixed that my fault overlooked a ios::in flag. the problem now is we are back to throwing access violations but only when choosing to search tne inventory.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: [c++] Inventory Project "Access Violation"

Posted 28 October 2011 - 02:35 PM

Why are you opening and closing your file is so many places? When you open your file you should always check that the file opened correctly, and remember if you some how set an error flag on the stream, opening and closing the stream will not clear the error. You should also always check that the read and write operations succeed.

Jim

This post has been edited by jimblumberg: 28 October 2011 - 02:35 PM

Was This Post Helpful? 0
  • +
  • -

#8 sdla4ever  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 26-September 11

Re: [c++] Inventory Project "Access Violation"

Posted 28 October 2011 - 02:39 PM

View Postjimblumberg, on 28 October 2011 - 02:35 PM, said:

Why are you opening and closing your file is so many places? When you open your file you should always check that the file opened correctly, and remember if you some how set an error flag on the stream, opening and closing the stream will not clear the error. You should also always check that the read and write operations succeed.

Jim


I'm opening and closing in so many places because well basically i'm a noob. Also I figured if I could chop it up the chance of a file getting corrupted would be less.

The new access violation is

Unhandled exception at 0x003e5594 in 17A_Project1.exe: 0xC0000005: Access violation writing location 0xcdcd0053.
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: [c++] Inventory Project "Access Violation"

Posted 28 October 2011 - 02:51 PM

Actually the fewer times you open and close the file would probably be better. To find where your program is crashing run the program through your debugger. The debugger will tell you what line is causing the crash.

Jim
Was This Post Helpful? 1
  • +
  • -

#10 sdla4ever  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 26-September 11

Re: [c++] Inventory Project "Access Violation"

Posted 28 October 2011 - 03:04 PM

It says the error is in istream, so its with the first reinterpret_class in the fillInv function.

Can I just use cin to get the char[x] elements back from the file? or do I need to use get()?

EDIT: Oh I see something, it is looking for keyboard input instead of getting data from the file.

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[3];
		cout<<"done"<<endl;
		cin>>cptr[h].color[20];
		cout<<"done"<<endl;
		cin>>cptr[h].quantity;
		cout<<"done"<<endl;
		cin>>cptr[h].notes[20];
		cout<<"done"<<endl;

		invent.read(reinterpret_cast<char*>(&cptr),sizeof(cptr));
	}

	closeFile(invent, NAME);
}


This post has been edited by sdla4ever: 28 October 2011 - 03:09 PM

Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: [c++] Inventory Project "Access Violation"

Posted 28 October 2011 - 03:36 PM

Why would you try to read the file with both read() and the extraction operator>> ? You wrote the file using write(), so you will need to use read() to retrieve the data. Is there any specific reason you are trying to use binary file handling? It is much easier to see what is going on if you are using regular text files.

Jim
Was This Post Helpful? 1
  • +
  • -

#12 sdla4ever  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 26-September 11

Re: [c++] Inventory Project "Access Violation"

Posted 28 October 2011 - 04:02 PM

View Postjimblumberg, on 28 October 2011 - 03:36 PM, said:

Why would you try to read the file with both read() and the extraction operator>> ? You wrote the file using write(), so you will need to use read() to retrieve the data. Is there any specific reason you are trying to use binary file handling? It is much easier to see what is going on if you are using regular text files.

Jim


My teacher just said its normally safer to just use binary when you have multiple data types in a struct. Ok and thank you so much, totally skipped the idea that write would do the whole structure.

Now I'm on to getting my search functions to work. the int one does the char ones not at the moment but thats the joys of learning.
Was This Post Helpful? 0
  • +
  • -

#13 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1675
  • View blog
  • Posts: 3,169
  • Joined: 30-May 10

Re: [c++] Inventory Project "Access Violation"

Posted 28 October 2011 - 11:21 PM

01 void fillInv(cloth *cptr,int size)
02 {
03 fstream invent;
04 openFile(invent, NAME);
05
06 invent.read(reinterpret_cast<char*>(&cptr),sizeof(cptr));
this is wrong!

cptr is already a pointer, you don't need more dereferencing.

Assuming cptr is actually pointing to something valid, it would be
invent.read(reinterpret_cast<char*>(cptr),sizeof(*cptr));
Notice very carefully that a & has gone, and a * has arrived.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1