Adding arrays outside a while loop.

Using numbers from an array inside a while loop and using those numbers to add up outside the while loop. I'm not sure how to do so and I have looked up to no success.
to add up? What does that mean? Give an example and a more detailed explanation of what you want.
Want to sum up each items total each time the while loop is running.
Ex.
item choice #1 1001 apple $1.49 per pound
item choice #2 1002 yogurt $1.29
item choice #3 1010 bottled water $1.49
sum=(1.49*lb)+1.29+1.49
sum=number
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
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int main()
{
	int barcode[10]= {1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010};
	string item[10]= {"Apples", "Yogurt", "Raisins", "Onions", "Eggs", "Peanuts", "Cereal", "Bananas", "Beer", "Bottled Water"};
	double itemPrice[10]= {1.49, 1.29, 3.39, 0.79, 2.49, 5.99, 3.50, 2.55, 5.55, 1.49};
	int myArray[] = {};
	int a, sum;
	int arrayname[]={};
	int i, itemChoice, keepGoing, weight;
	bool run = true;
	while (run)
	{
		for (i=0; i<10; i++)
		{
			cout << "Barcode: " << barcode[i] << "-" << item[i] << " :" << itemPrice[i] << "\n";
		}	
		cout << "What item? (pick the number)\n";
		cin >> itemChoice;
		if ((itemChoice==1001)|| (itemChoice==1003)||(itemChoice==1004)||(itemChoice==1006)||(itemChoice==1008))
		{
			cout << "How much does it weigh in pounds?\n";
			cin >> weight;	
		} 
		else if ((itemChoice==1002)||(itemChoice==1005)||(itemChoice==1007)||(itemChoice==1009)||(itemChoice==1010))
		{
			exit;
		}
		else 
		{
			cout << "Error!!!\n";
		}
        for (int i=0; i < 3; ++i) {
        sum += itemChoice[i];
        }//Want to sum up each items total each time the while loop is running.
        cout << "the sum of the array elements is: " << sum << endl;
		cout << "Do you want to continue?(1 to add more items and 2 to proceed to the receipt)\n";
		cin >> keepGoing;
		if (keepGoing==2)
		{
			run = false;
		}
		else if (keepGoing==1)
		{
			run = true;
		}
	}
	return 0;
}

this is how my program should work when i'm done.
The program should behave as follows:
--------------------------------------------------------------------------
1001 - Apples        - 1.49/lb
1002 - Yogurt        - 1.29
1003 - Raisins       - 3.39/lb
1004 - Onions        - 0.79/lb
1005 - Eggs          - 2.49
1006 - Peanuts       - 5.99/lb
1007 - Cereal        - 3.50
1008 - Bananas       - 2.55/lb
1009 - Beer          - 5.55
1010 - Bottled Water - 1.49
Which item? 1001
Weight? 2.20
Apples 2.20 lbs @ 1.49 ==> 3.28
Press 1 to continue (or 2 to exit): 1
Last edited on
bump
Each time you get the information of how much the user spent money buying something, so each loop round, store that amount of money inside of itemChoice. Then just put it in sum at the end of each loop.

sum += itemChoice[i];?? itemChoice is not an array, you cant do this ya know.

sum += itemChoice;

Im wondering, why do you ask for the weights? You dont do anything with it so its pointless.

1
2
3
4
else if ((itemChoice==1002)||(itemChoice==1005)||(itemChoice==1007)||(itemChoice==1009)||(itemChoice==1010))
		{
			exit;
		}


This is also pointless, its like not having the else if statement at all.
Topic archived. No new replies allowed.