Total of vectors Problem

Ok its me again. Im getting really frustrated with my practice project... :s

I cant figure out what i have done wrong when adding the total of vectors. Also the compiler tells me that i have 2 warnings...

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
#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;
}




And this is what compiler says:

1>------ Build started: Project: Savings, Configuration: Debug Win32 ------
1>Compiling...
1>file.cpp
1>c:\users\mashikag\documents\visual studio 2008\projects\savings\savings\file.cpp(22) : warning C4018: '<' : signed/unsigned mismatch
1>c:\users\mashikag\documents\visual studio 2008\projects\savings\savings\file.cpp(41) : warning C4018: '<=' : signed/unsigned mismatch
1>Linking...
1>LINK : C:\Users\Mashikag\Documents\Visual Studio 2008\Projects\Savings\Debug\Savings.exe not found or not built by the last incremental link; performing full link
1>Embedding manifest...
1>Build log was saved at "file://c:\Users\Mashikag\Documents\Visual Studio 2008\Projects\Savings\Savings\Debug\BuildLog.htm"
1>Savings - 0 error(s), 2 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========


:S
Last edited on
As mentioned in the last post, you need to change [i] on line 43 to [y]. This is because you are using the index "y" in this for loop to iterate through all the different elements in the moneysaved array. You could also use "i" instead of "y" as your iterator. Either fix will work. As of now, "i" is a constant value from the last iteration of the previous for loop, so your total is just essentially (the last element of moneysaved)*(number of elements in the moneysaved array). Please also go close the previous topics you posted.
Already tried changing y to i and it didnt work tried again both solution and it doesnt work again... It increases the no. of warnings to 4 and total is wrong again... :S
When I run your code with line 43 looking like:

 
		temp += moneysaved[y];


It works great for me
does it give u the right total try for more than 3 numbers... ? :S
btw when i subbed y instead of i, I get an error when trying to obtain the total:
http://img707.imageshack.us/img707/4697/vectorstotalproble.jpg
oh yeah i found the problem, i shouldnt have put there y<=moneysaved.size() just y<moneysaved.size();

Thanks!
Topic archived. No new replies allowed.