### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` `````` //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 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:
 ``21222324252627282930`` ``````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:
 ``212223242526272829303132333435`` ``````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
 ``404142434445`` ``````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.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` `````` //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 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.