For loop problem

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

closed account (EwCjE3v7)
Thanks it worked...thanks
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.
closed account (EwCjE3v7)
Thanks
Topic archived. No new replies allowed.