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...
#include "Product.h"
#include<iostream>
#include<string>
usingnamespace 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;
}
elseif(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];
}
}
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.
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];