For Loop Just Won't Work

Hi, I'm azüriin

My problem is I want to use a nested for loop and it seems that it won't run no matter what I do.

My program is, admittedly, a homework program but my teacher doesn't understand c++ (I'm an advanced student in the whole course) seeing as he mostly teaches Java and Turing. Plus, it is far from uncommon for the students in my class to use forums since it is a big class and it seems everybody needs help. The program is designed to display a table of squares using a loop, and he challenged me to use a for loop for it.

I tried using a function call inside the loop so it didn't have to be nested, but that didn't seem to make my teacher happy.

Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

void main() {
	for (int i = 10; i == 1; i++){
		int square = i*i;
		if (i == 10){ 
			cout<< i << "........." << square << "\n";
		}
		else {
			cout<< i<< ".........." << square << "\n";
		}
	}
	cin.get();
}

As you can see it's very basic.
I can post my output log as well if someone needs it.

Thank you for reading
Last edited on
This loop will not run - not even once.
The middle part of the for line - this bit here in bold for (int i = 10; i == 1; i++)
decides whether the loop runs. It must evaluate to true for the loop to run.
i == 1 means test if i is equal to 1

As you can see, i starts off with the value 10 so that check will fail at the very first time because i is obviously NOT EQUAL to 1 - so the test result is false so the loop
will not run.
Oh, okay I was thinking the middle value was what ended it. I'm skilled with Javascript and that's works in that language.

Thank you.

EDIT: The code works after another mistake was fixed. i++ Should have been i--.
Last edited on
main should have an int return type by the way.
main should have an int return type by the way


Thanks Warnis, I'm sure that will prevent some future problems, but may I ask why it's needed?
It tells the compiler that the program ran without failures. Sometimes not returning 0 causes errors. Therefore, change void main() into int main() and add return 0; at the and of your program.
Last edited on
I'm guessing it would exit with a native code of 0x00000000?
Last edited on
It tells the operating system, not the compiler, that the program finished correctly.
Note, however, that the OS may not actually use this number for anything. At least Windows and UNIX don't use it unless the user writes a script that uses it.

It's possible to not explicitly return anything from main(). Then main() will return 0 by default.
This is the shortest valid C++ program:
int main(){}
Topic archived. No new replies allowed.