problem with looping and updating variables

I've having troubles trying to word this but basically my goal for the program is to have multiple people input how much change is in their pocket from toonies to pennies then after their turn is done it gives them there total; then at the end add up all the inputs of change to equal a complete total, and also give the average between all the inputs. so far I think i'm on the right track however when I run the code it gives me the personal totals but at the end it doesn't give me the complete total it just takes the last persons amount. any ideas?

I have no clue if i'm able to update a variable while its in a loop, for example say one person has $5.00 in their pocket and another person has $3.00 and another has $10.00; would I be able to have a variable that would update as new numbers get input?

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
60
61
62
63
64
65
66
67
#include <iostream>
#include <math.h>
#include <iomanip>
#include <cmath>
#include <string>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {



	cout<< "This program will count all the coins you and your friends pockets" <<endl; 
	cout << "and display the the total value and the average amount of money between you!"<<endl;
	cout<< " "<<endl;
	cout<< "Enter the total amount of people in your group (including yourself): ";
	long long friends;
	cin>> friends;
	long long person = 1;


//im pretty sure i need to use a for loop or a while loop
	for(int i = 1; i <= friends; i++)
	{
		cout<< "Person #"<< i << endl;
		cout<< "Enter number of Toonies: ";
		int two_dollar;
		cin >> two_dollar;
		cout<< "Enter number of Loonies: ";
		int one_dollar;
		cin >> one_dollar;
		cout<< "Enter number of Half dollars: ";
		int half_dollar;
		cin >> half_dollar;
		cout<< "Enter number of Quarters: ";
		int quarter;
		cin >> quarter;
		cout<< "Enter number of Dimes: ";
		int dimes;
		cin >> dimes;
		cout<< "Enter number of Nickels: ";
		int nickel;
		cin >> nickel;
		cout<< "Enter number of Pennies: ";
		int penny;
		cin >> penny;
		
		
		double change = 0;
		change = two_dollar * 2.00 + one_dollar * 1.00 + half_dollar * 0.50 + 
				quarter * 0.25 + dimes * 0.10 + nickel * 0.05 + penny * 0.01;//change is each persons cash
		cout<< "the amount of money in person " << person << "'s pocket is $" << change << endl;
		
		
		while(i == friends) //this is where i'm stuck 
		{	double pockets =0;
			pockets = pockets + change;
			cout<< pockets<< " this is the total amount of cash " << endl;
			break;	
		}
		
		++person;
		
		
	}	
	system("pause");
	return 0;
}
For each iteration of your for loop your change value gets reset to 0 and recalculated, so when your while loop runs, you only have the info of the last person who input his money.

You should maybe use a vector to keep an instance of all the people's pocket money and the, calculate the average and total.

You should define and initialise double pockets outside of your while loop because otherwise it keeps being reset. And about that while loop, It acts like an if statement, since i == friends will be true only for your last iteration of your for loop, for all the previous iterations, it won't do anything.

What is the use of the long long person variable? there doesn't seem to be any use for it.
@H00G0 how would I make a vector I haven't 'learned' that yet.
and when you say move double pockets outside do you mean inside the for loop or outside of everything? and long long person I made that a long long just to be the same as the others but i guess it could just be a int right?
A helpful link for vectors is https://www.geeksforgeeks.org/vector-in-cpp-stl/. Below is just another way to tackle the problem by using a struct and smart pointer to dynamically allocate an array.

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
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <memory> // For unique_ptr
#include <iomanip> // for setprecision.

struct Coins
{
	double two_dollar;
	double one_dollar;
	double half_dollar;
	double quarter;
	double dimes;
	double nickel;
	double penny;
};

int main()
{
	std::cout << "This program will count all the coins you and your friends pockets" << std::endl;
	std::cout << "and display the the total value and the average amount of money between you!" << std::endl;
	std::cout << " " << std::endl;
	std::cout << "Enter the total amount of people in your group (including yourself): ";
	int friends;
	std::cin >> friends;

	// Dynamically allocated arrays with smart pointers.
	std::unique_ptr<int[]> numOfPeople(new int[friends]);
	std::unique_ptr<Coins[]> coins(new Coins[friends]);

	double change{0.0}; // running total for change
	double pockets{0.0}; // running total for amount of cash.
	std::cout << std::fixed << std ::setprecision(2); //output for two decimal places.

	for (int count = 0; count < friends; count++)
	{
		std::cout << "\nPerson #" << count + 1 << std::endl;
		std::cout << "Enter number of Toonies: ";
		std::cin >> coins[count].two_dollar;
		change += coins[count].two_dollar * 2.00;

		std::cout << "Enter number of Loonies: ";
		std::cin >> coins[count].one_dollar;
		change += coins[count].two_dollar * 1.00;

		std::cout << "Enter number of Half dollars: ";
		std::cin >> coins[count].half_dollar;
		change += coins[count].two_dollar * .50;

		std::cout << "Enter number of Quarters: ";
		std::cin >> coins[count].quarter;
		change += coins[count].two_dollar * .25;

		std::cout << "Enter number of Dimes: ";
		std::cin >> coins[count].dimes;
		change += coins[count].two_dollar * .10;

		std::cout << "Enter number of Nickels: ";
		std::cin >> coins[count].nickel;
		change += coins[count].two_dollar * .05;

		std::cout << "Enter number of Pennies: ";
		std::cin >> coins[count].penny;
		change += coins[count].two_dollar * .01;

		std::cout << "The amount of money in person "
		<< count + 1 << "'s pocket is $ " << change << std::endl;
		pockets += change;

	}
	std::cout << "$" << pockets << " this is the total amount of cash\n";
	
	return 0;
}


Ouput:

This program will count all the coins you and your friends pockets
and display the the total value and the average amount of money between you!

Enter the total amount of people in your group (including yourself): 3

Person #1
Enter number of Toonies: 1
Enter number of Loonies: 2
Enter number of Half dollars: 3
Enter number of Quarters: 4
Enter number of Dimes: 5
Enter number of Nickels: 6
Enter number of Pennies: 7
The amount of money in person 1's pocket is $ 3.91

Person #2
Enter number of Toonies: 7
Enter number of Loonies: 6
Enter number of Half dollars: 5
Enter number of Quarters: 4
Enter number of Dimes: 3
Enter number of Nickels: 2
Enter number of Pennies: 1
The amount of money in person 2's pocket is $ 31.28

Person #3
Enter number of Toonies: 2
Enter number of Loonies: 4
Enter number of Half dollars: 6
Enter number of Quarters: 8
Enter number of Dimes: 10
Enter number of Nickels: 12
Enter number of Pennies: 14
The amount of money in person 3's pocket is $ 39.10
$74.29 this is the total amount of cash
Topic archived. No new replies allowed.