Okay so I have a c++ project due soon involving structures. I'm trying to sort an array of structures in increasing sales using the bubble sort but I keep getting errors that say things like 'expected unqualified id before '.' token'. Here is my code so far:
int main()
{
struct Products
{
int productID, quantitySold;
double unitPrice, productSales;
};
Please post the complete error messages exactly as they appear in your development environment.
Do you know you can "swap" a whole structure at one time?
What is the purpose of the tempProducts structure? Why not just use the Products structure? By the way you define a tempProducts structure but you never create an instance of that structure.
Line 41-44: tempProducts is a type name, not an instance. You can't store into a type name.
In fact, you don't even need to declare tempProducts since it is the same as products. You can create a much simpler program:
29 30 31 32 33 34 35 36 37 38
Products temp;
for (i = 0; i < size; i++)
{ for(j = i + 1; j < size; j++) // Changed <= to <
{ if(productArray[i].productSales > productArray[j].productSales)
{ temp = productArray[i];
productArray[i] = productArray[j];
productArray[j] = temp;
}
}
}
You're going to cause an out of bounds condition at line 33:
32 33
for(j = i + 1; j <= size; j++)
{ if (productArray[i].productSales > productArray[j].productSales)
j goes from 1 to 15, but the only valid occurrences are 0-14.
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.