For loop problem

Jun 27, 2013 at 8:24pm
closed account (EwCjE3v7)
I was just transforming a while loop into a for loop.to learn my for loops

working while loops
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream> 
using namespace std;

int main() 
{
	int sum = 0, value = 0;
	while (cin >> value)
	{
		sum += value;
	cout << "Sum is: " << sum << endl;
	}
	system("PAUSE");
	return 0;
}



for loop (not working)
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream> 
using namespace std;

int main() 
{
	

	for (int sum = 0, val = 0; cin >> val; sum += val)
		cout << "Sum is: " << sum << endl;
	system("PAUSE");
	return 0;
}
Last edited on Jun 27, 2013 at 8:24pm
Jun 27, 2013 at 8:36pm
That the loops would be identical you can write

1
2
3
4
5
	for (int sum = 0, val = 0; cin >> val; /* empty */ )
	{
		sum += val;
		cout << "Sum is: " << sum << endl;
	}

Jun 27, 2013 at 8:40pm
closed account (EwCjE3v7)
Thanks it worked...thanks
Jun 27, 2013 at 8:49pm
By the way I wrote a post about some loops here

http://cpp.forum24.ru/?1-3-0-00000059-000-0-0-1359294292

Though it is written in Russian however you can use for example the google translator that to read the post.
Jun 27, 2013 at 9:41pm
closed account (EwCjE3v7)
Thanks
Topic archived. No new replies allowed.