for loop working, line counter not working..

Hi, this is my first post.......yipeee!! :)

I'm a beginner to C++, and i've written a simple program to teach myself about for loops with a line counter. The for loop works fine, i see the numbers 1 to 150 stream down the page no problem, but alas, the line counter isnt working.

I expected to see when "counter" reaches 10, a blank line.. so i can have a blank line every 10 lines...But it doesnt seem to be doing Anything at all... I'm perplexed, I have looked at it and tried compiling with different compilers, with no luck.

Can anyone help me please?

Here's the code


// mucking about with the for loop.

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
int bush;
int counter;

counter = 0;

for(bush=1; bush <= 150; bush++)
cout << bush << "\n";

counter++;

if(counter == 10) {

cout << "\n";
counter = 0;
}
cin.get();
return 0;
}
Last edited on
This is the only statement in the loop. cout << bush << "\n";.

counter is incremented once and so is never equal to 10.

To perform many statements in the loop enclose them with braces.

Last edited on
It's funny how it makes so much sense when explained.

This has been a good lesson for me, basic but fundamental.

Thank you very much indeed, and for your fast response too :)

btw, here's another alternative:

1
2
3
4
5
6
for(bush=1; bush <= 150; bush++) {
	cout << bush << "\n";

	if(bush % 10 == 0)
		cout << "\n";
}
Topic archived. No new replies allowed.