Vectors

Hey, im having problems with vectors again... :S

OK first of all the source 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
#include <iostream>
#include <vector>
using namespace std;

int main() 
{
	int b=1;
	int money;
	char x;
	vector<int>moneysaved;
	
	system("TITLE Savings account details");

	do
	{
		system("CLS");
		for (int a=moneysaved.size(); a<b; a++)
		{
			cout << "Enter money you have put away:";
			cin >> money;
			moneysaved.push_back(money);
		}
		for (int i=0; i<b ; i++) 
		{
			cout << moneysaved[i];
			cout << '+';
		}
		cout << endl << endl << "Would you like to try again? (Y or N)";
		cin >> x;
		b++;
	}while (x == 'y' || x == 'Y');

	system("CLS");
	cout << "Total: " << "?" << endl << endl; 

	system("pause");
	return 0;
}




Now i want it to show the total of moneysaved[i] where the "?" is. How would i do this? :S
Use a loop and sum all the values yourself.

You might also use some of the STL stuff from functional, but that's probably a bit advanced for this stage.
Just as a matter of curiosity, who taught you all those DOS commands ("pause", "cls", and "title")? Bane of secure programming, they are.
youtube o_O why is that? :P

i finally got the total by using a temporary variable.

1
2
	for (int y=0; y<moneysaved.size(); y++)
		temp += moneysaved[i];

and also did some changes to the whole code...
ok now i just dont know what i did wrong :S i changed the whole code around so it looks like:

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
#include <iostream>
#include <vector>
using namespace std;

int main() 
{
	int money;
	char x;
	int i=0;
	int temp=0;
	vector<int>moneysaved;
	
	system("TITLE Savings account details");

	while (true)
	{
		system("CLS");
		for (int a=moneysaved.size(); a<b; a++)
		{
			cout << "Enter money you have put away:";
			cin >> money;
			moneysaved.push_back(money);
		}
		cout << endl << endl << "Would you like to try again? (Y or N)";
		cin >> x;
		for (x=='n' || x=='N')
			break;
	}
	for (int i=0; i<moneysaved.size(); i++) 
	{
		cout << moneysaved[i];
		cout << '+';
	}
	cout << endl << endl << "Would you like to try again? (Y or N)";
	cin >> x;
	

	system("CLS");
	cout << "Total: ";
	
	for (int y=0; y=<moneysaved.size(); y++)
		temp += moneysaved[i];
	cout << temp << endl;

	system("pause");
	return 0;
}



but for shit i cant tell whats wrong it just wont compile... :S It tells me there is 6 error(s), 1 warning(s). Im totally stuck...
Try reading the errors. They tell you what's wrong with the code.

If you don't understand them, post them here and we can explain them.
Last edited on
There's a small but important error on line 42. Moneysaved is using the wrong variable.

Edit: variable b on line 18 isn't declared.
Last edited on
for (x=='n' || x=='N')

should be:

if (x=='n' || x=='N')

AND

for (int y=0; y=<moneysaved.size(); y++)

should be:

for (int y=0; y<=moneysaved.size(); y++)

Also, you have dropped your declaration of b. That should do it.

why moneysaved is using wrong variable?
int i = stands for what is stored in the vector... :s
i can see the output of the programme is wrong then its what to be after removing all the syntax mystakes but why is this? :S



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
#include <iostream>
#include <vector>
using namespace std;

int main() 
{
	int money;
	char x;
	int i=0;
	int temp=0;
	vector<int>moneysaved;
	
	system("TITLE Savings account details");

	while (true)
	{
		system("CLS");
		cout << "Enter money you have put away:";
		cin >> money;
		moneysaved.push_back(money);
		
		for (int i=0; i<moneysaved.size(); i++) 
		{
			cout << moneysaved[i];
			cout << '+';
		}
		
		cout << endl << endl << "Would you like to try again? (Y or N)";
		cin >> x;

		if (x=='n' || x=='N')
		{
			break;
		}
	}


	system("CLS");
	cout << "Total: ";
	
	for (int y=0; y<=moneysaved.size(); y++)
		temp += moneysaved[i];
	cout << temp << endl;

	system("pause");
	return 0;
}
Topic archived. No new replies allowed.