Help with structures

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

const int size = 15;
int i, j;

Products productArray[size] = {
{1, 120, 3.99, 478.80},
{2, 649, 1.29, 837.21},
{3, 76, 6.47, 491.72},
{4, 103, 2.99, 307.97},
{5, 89, 5.89, 524.21},
{6, 135, 2.53, 341.55},
{7, 291, 2.17, 631.47},
{8, 242, 1.99, 481.58},
{9, 97, 4.67, 452.99},
{10, 453, 0.99, 448.47},
{11, 200, 2.45, 490},
{12, 56, 7.83, 438.48},
{13, 114, 2.70, 307.80},
{14, 128, 2.59, 331.52},
{15, 81, 6.99, 566.19},
};
struct tempProducts
{
int a,b;
double c,d;
};

for (i = 0; i < size; i++)
{
for(j = i + 1; j <= size; j++)
{
if(productArray[i].productSales > productArray[j].productSales)
{
tempProducts.a = productArray[i].productID;
tempProducts.b = productArray[i].quantitySold;
tempProducts.c = productArray[i].unitPrice;
tempProducts.d = productArray[i].productSales;
productArray[i].productID = productArray[j].productID;
productArray[i].quantitySold = productArray[j].quantitySold;
productArray[i].unitPrice = productArray[j].unitPrice;
productArray[i].productSales = productArray[j].productSales;
productArray[j].productID = tempProducts.a;
productArray[j].quantitySold = tempProducts.b;
productArray[j].unitPrice = tempProducts.c;
productArray[j].productSales = tempProducts.d;
}
}
}

return 0;
}

Can anyone help me?
Last edited on
Please use code tags when posting code.

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.
Last edited on
Topic archived. No new replies allowed.