Problem with while loop and variables

Hi, i'm trying to build a program to count squares inside of squares (i don't know how to explain better, take a look at the link). The program worked out all right, but when i tried to build a while loop, starts to output incorrect results... By the way, i'm using Visual C++ 2010.

http://docs.overviewer.org/en/mcregion/_images/chunk_topdown.png


Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdafx.h"
#include <iostream>
#include "math.h"
using namespace std;

double a,c,d;
int j=0;

int _tmain(int argc, _TCHAR* argv[])
{
	cout<<"What is the size of the sides? ";
	cin>>a;
	for(int b=1; b<=a;b++)
	{
		d=pow((float)b,2);
		c=c+d;
		cout<<d<<endl;
	}
	cout<<"Result: "<<c<<endl;
	system("pause");
	return 0;
}


J is only meant to be used in the while loop, as a check.
closed account (SECMoG1T)
Are you sure you are talking about a while loop? Well i din't see one in your code or do you mean a for loop?

Why do you have this d=pow((float)b,2); ? i dont think it does the right thing.
Last edited on
Line 16: Where is c initialized? You're going to adding d to garbage.
For that matter, what is the point of c, since you never use it?
The program is fine, woks as expected, but when i tried to add a while loop it started to give wrong values.
Topic archived. No new replies allowed.