conversion from int to double

my program is throwing an exception after i enter the last pieces of information to fill the array. here's the error i'm getting: EDIT: in the console window it prints the array, but then once it gets to the sortArray function, the error is being thrown.

Unhandled exception at 0x009f2327 in Labsheet 1.exe: 0xC0000005: Access violation reading location 0xfdfdfe01.

i'm also getting two warnings: 'conversion from double to int, possible loss of data', in my sorting function...

anybody have any ideas what's causing this?

here's the main:

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
#include "Product.h"
#include<iostream>
#include<string>
using namespace std;

void sortArray(Product productArray[], int n);

int main()
{
	//delcare array of pointers
	Product *productArray[10];

	//Get input
	int swPrice, bookPrice;
	cout<<"Enter price of software"<<endl;
	cin>>bookPrice;
	cout<<"Enter price of book"<<endl;
	cin>>swPrice;
	cout<<endl;

	//create items and store address in pointer
	Product *theBook = new Book(bookPrice);
	Product *theSoftware = new Software(swPrice);

	//check getPrice() works
	cout<<theBook->getPrice()<<endl;
	cout<<theSoftware->getPrice()<<endl;

	//store items in the array
	productArray[0] = theBook;
	productArray[1] = theSoftware;

	//access the elements from the array
	cout<<productArray[0]->getPrice()<<endl;
	cout<<productArray[1]->getPrice()<<endl;
	
	//get details for remaining 8 spaces in array
	for(int i = 2; i < 10; i++)
	{
		double price;
		cout<<"Is the product a book or software? Enter 'book' or 'software'"<<endl;
		string prodType;
		cin>>prodType;
		cout<<endl;
		if(prodType == "book")
		{
			cout<<"Enter price of book"<<endl;
			cin>>price;
			productArray[i] = new Book(price);
			cout<<endl;
		}
		else if(prodType == "software")
		{
			cout<<"Enter price of software"<<endl;
			cin>>price;
			productArray[i] = new Software(price);
			cout<<endl;
		}
	}

	//print out array
	for(int i = 0; i < 10; i++)
	{
		cout<<productArray[i]->getPrice()<<endl;
	}

	//sort array into ascending order
	sortArray(*productArray, 10);//Doesn't seem to accept the array for some reason without use of '*'??

	//print out array in ascending order
	for(int i = 0; i < 10; i++)
	{
		cout<<productArray[i]->getPrice()<<endl;
	}
	
	system("pause");
	return 0;
}

void sortArray(Product productArray[], int n)
{
	double values[10];
	for(int i = 0; i < 10; i++)
	{
		values[i] = productArray[i].getPrice();
	}

	int i, j;
	int min, minPos;
	for(i = 0; i < (n - 1); i++)
	{
		min = values[i];
		minPos = i;
		for(j = i+1; j < n; j++)
		{
			if(values[j] < min)
			{
				min = values[j];
				minPos = j;
			}
		}
		values[minPos] = values[i];
		values[i] = min;
		productArray[i] = values[i];
	}
}
	  
Last edited on
0xFDFDFDFD : Used by Microsoft's C++ debugging heap to mark "no man's land" guard bytes before and after allocated heap memory


Suggests that you're interpreting unallocated memory as a pointer. If you run using a debugger, or a memory checking tool such as valgrind, it will tell you exact line of code doing this.
i'm using visual studio, the two lines giving the warning are 92 and 98, which are:

min = values[i];

and

min = values[j];

and the warning are the 'conversion from double to int, possible loss of data'. would this be causing the problem?

and the warning are the 'conversion from double to int, possible loss of data'. would this be causing the problem?


No.

min is an int, values[] is an array which contains doubles. Converting from double to an int you can lose data. Doubles have precision and are probably 8 bytes on your system where integers are most likely 4 bytes on your system. So fix this by making min a double.

Change this to the way you wanted it..
1
2
3
void sortArray(Product productArray[], int n); //This is most likely what is causing the error...
//to...
void sortArray(Product* productArray[], int n);


The signature for your sort function takes an array whos elements are Products. Your array is an array whos elements are pointers to products.


When you make the call to it...
 
sortArray(productArray, 10); //with no * 



1
2
//Unless you've overloaded the [] operator I don't this is doing what you think it is.
productArray[i] = values[i];
Last edited on
i'm using visual studio,


Whilst I haven't used it for many years, I'm sure it comes with an integrated debugger that will tell you the problem line when it crashes.
Topic archived. No new replies allowed.