Not understanding the problem with my calculation not giving the total value

I'm in intro to c++, so using arrays is new to me right now. I'm having issues with the userAverage not giving me the total value I want it to give. It will either give me 0, or the negative number used to terminate the program. I really can't see what's wrong with 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
  //Ask the user to enter the price and quantity of items.  Store the prices and quantities in parallel arrays.
//Input will stop when the user enters a negative price or negative quantity.
//There will never be more than 999 items (for the size of your array).
//After input is finished write the total value of the entire inventory to a file: "inventory.txt".
#include <iostream>

using namespace std;

int main()
{
    double userAverage[998];
    double itemPrice[998];     //Variable for the price of the item.
    int itemQuantity[998];     //Variable for the amount of items.
    int i;                     //Variable for the loop to be displayed.

    cout << "Enter a negative number if you would like to end the program.\n";

    for(i = 0; i <= 998; i++){


        cout << "Please enter the price of the item: ";
        cin >> itemPrice[i];

        //If the user wants to end program.
        if(itemPrice[i] <= -1 || itemQuantity[i] <= -1 ){
                break;
        }

        cout << "How many items do you have: ";
        cin >> itemQuantity[i];

        //If the user wants to end the program.
        if(itemPrice[i] <= -1 || itemQuantity[i] <= -1 ){
                break;
        }

    }

    //Calculations for array.
    for(i = 0; i <= 998; i++){

        userAverage[i] += itemPrice[i] * itemQuantity[i];   //Total up all the prices and quantities.

    }

    cout << endl;

    cout << "Your total is $" << userAverage[i] << ". Have a nice day!";    //Display the total cost.

    return 0;
}
First of all, array indices go from 0 to size - 1, so change all of those
for (i = 0; i <= 998; i++)
to
for (i = 0; i < 998; i++).

Now, inside your for loop:
21
22
23
24
25
26
27
28
29
30
cout << "Please enter the price of the item: ";
cin >> itemPrice[i];

//If the user wants to end program.
if(itemPrice[i] <= -1 || itemQuantity[i] <= -1 ){
        break;
}

cout << "How many items do you have: ";
cin >> itemQuantity[i];

Here, you're checking whether itemQuantity[i] is negative, but you haven't even entered a value for it yet!

I would change it to something more like this:
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
cout << "Please enter the price of the item: ";
cin >> itemPrice[i];

//If the user wants to end program.
if(itemPrice[i] <= -1){ // We only have itemPrice[i] so far, so just check that
        break;
}

cout << "How many items do you have: ";
cin >> itemQuantity[i];

//If the user wants to end the program.
if(itemQuantity[i] <= -1 ){ // Already checked itemPrice[i]; no need to check it again
        break;
}

Also, when you're totaling up the prices, you don't take into account where the user's input stops (that is, where the first negative value for itemPrice[i] or itemQuantity[i] occurs).
So it should probably be something more like
40
41
42
43
44
45
for(i = 0; i < 998; i++){
    if (itemPrice[i] <= -1 || itemQuantity[i] <= -1) // End of input
        break;
    userAverage[i] += itemPrice[i] * itemQuantity[i];   //Total up all the prices and quantities.

}
Thank you so much
long double main
The last code you put didn't make sense to me though because it still didn't get the userAverage to total everything. I got it to work now, though with playing around.
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
  //Ask the user to enter the price and quantity of items.  Store the prices and quantities in parallel arrays.
//Input will stop when the user enters a negative price or negative quantity.
//There will never be more than 999 items (for the size of your array).
//After input is finished write the total value of the entire inventory to a file: "inventory.txt".
#include <iostream>

using namespace std;

int main()
{
    const int maxArray = 998;       //The max amount of arrays.
    double userAverage;             //Variable to add the total price and quantity.
    double itemPrice[maxArray];     //Variable for the price of the item.
    int itemQuantity[maxArray];     //Variable for the amount of items.
    int i;                          //Variable for the loop to be displayed.
    double totalPrice;              //Variable to place all the entered prices.
    int totalQuantity = 0;          //Variable to place all the entered quantity.

    cout << "Enter a negative number if you would like to end the program.\n";  //Announce an exit.

    for(i = 0; i < maxArray; i++){


        cout << "Please enter the price of the item: ";
        cin >> itemPrice[i];
        totalPrice += itemPrice[i];                 //Add the price(s) entered in.

        //If the user wants to end program.
        if(itemPrice[i] <= -1){
                break;
        }

        cout << "How many items do you have: ";
        cin >> itemQuantity[i];
        totalQuantity += itemQuantity[i];           //Add the quantity entered in.

        //If the user wants to end the program.
        if(itemQuantity[i] <= -1 ){
                break;
        }



    }

    //Calculations for array.
    for(i = 0; i < maxArray; i++){
        if (itemPrice[i] <= -1 || itemQuantity[i] <= -1)        // End of input
        {
        userAverage = totalPrice * totalQuantity;               //Total up all the prices and quantities.
        }
    }

    cout << endl;

    cout << "Your total is $" << userAverage << ". Have a nice day!";    //Display the total cost.

    return 0;
}
Topic archived. No new replies allowed.