Error when compiling

closed account (iGLbpfjN)
Well, I'm doing a code that works like a register machine.
You can register a product, inserting the code and the price. Also, you can search a product by inserting its code.
The error happens in the search moment. When you put the product code, Visual Studio give me this: http://prnt.sc/c2scae and Dev-C++ stop working.
Below is some parts of the 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
class product
{
	private:
		string name;
		long code;
		float price;

	public:
		void print_registro();
		void print_search();
};

// (...)

void product::print_search()
{
	cout << "Código: " << code << endl;
	cout << "Nome: " << name << endl;
	cout << "Preço: " << price << endl;
}

// (...)

void search_product()  // Here is the search option
{
	char opt;
	product pdt;

	ifstream fin;
	fin.open("Produtos.dat");
	fin.seekg(0, ios::end);

	do
	{
		long code_search = ((long)fin.tellg()) / sizeof(product);
		int position = (code_search - 1) * sizeof(product);

		cout << "Insira o código do produto a ser pesquisado: ";
		cin >> code_search;

		fin.seekg(position);
		fin.read((char*)&pdt, sizeof(product));
		pdt.print_search();

		cout << "Deseja pesquisar outro produto? (S/N)\n";
		cin >> opt;
		system("cls");
	} while (opt == 'S' || opt == 's');
}

// (...) 

Last edited on
Without an example of "Produtos.dat" and all of the code it is impossible to check what is going wrong here, but as a wild guess:
What do you expect this line to do?
 
long code_search = ((long)fin.tellg()) / sizeof(product);

and how about this line?
 
fin.read((char*)&pdt, sizeof(product));


Personally I would expect the size of a "product"-object to be very difficult to define. It might be possible for the compiler, but I have no idea what the size of the implementation part of the class will be nor how much memory will be reserved for the variables. How did you account for that in your file?

And in the second one, personally I would expect something to break if some data from a file is stored in the memory where my product objects implementation is. And if it did not break at that time, I would be very surprised if the print_search function (that used to be in the memory where we just stored the data from the file) was still working afterwards.

Also, i would expect that you actually want to divide by the size of the representation of the product in the file unless you use a file containing product objects.

Finally, did you try to catch your exception and get the "what()"? It might be helpful to post that here along with the error you are getting? Also it might be helpful to find out when this error occurred (e.g. when you open the file, when you try to determine the file size, when you try to read the first product, when you try to read the second product, ..., the last product from the file.

Kind regards, Nico
Last edited on
Im pretty sure you accessed memory not belong to you.

Issue 1:
A very simple logic:
'code_search' could be zero,
which means 'position' could be negative,
which means 'fin->seekg' got some bad argument.

Issue 2:
Another issue is you can't use binary read/write on std::string.
You can't store the std::string directly to a binary file.
You can't read a std::string from a binary file.
Topic archived. No new replies allowed.