Sum Up Multiple Numbers Entered By A User

Dear forumites, please, how do I sum up certain integers entered by a user?
This is what I have:

1
2
3
4
5
6
7
8
int main()
{
for (char i=0; i<6; i++){
        cout<<"Enter credit unit";
        cin>>CreditUnit;
        cout<<endl;
        }
}


How do I then get the sum of the various credit units entered in each repetition upto the 6th time????

Thanks in arrears!!
Last edited on
Try declaring a variable outside of the loop, that will handle the sum. Let's say int sum = 0;. Initializing to 0 is very important, as you will build upon that zero.

So then after you enter the "CreditUnit" you could do: sum = sum + CreditUnit;. In the end if you output "sum" it will contain the sum of the CreditUnits that were entered. Simple. :)
Last edited on
Thank you @Sasauke for your effort to helping me.
I have done just that but it didn't work.
I entered:
2, 2, 3, 2, 2, 1 and instead of getting 12 as the sum total, I got 1.
If you did this, it should've worked:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main()
{
	int sum = 0;
	int CreditUnit;

	for (int i = 0; i < 6; i++)
	{
		cout << "Enter credit unit: ";
		cin >> CreditUnit;
		sum = sum + CreditUnit;
	}
	cout << "Sum: " << sum;
	
	return 0;
}
Great!
Sasauke, you are a genius. Thanks a million.

It worked...and I have realised my bug; I placed the sum = sum + CreditUnit; outside the loop.

Thanks a million times.
Topic archived. No new replies allowed.