Infinite For Loops???

November 19, 2010 at 02:03:47
Specs: Linux x86_64
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
int numpart;
int UX;
int UY;
int UZ;
int factor;
cout << "How many particles are in one unit cube of your lattice?" << endl;
cin >> numpart;
cout << "What is the unit length (x) of your box?"<< endl;
cin >> UX;
cout << "What is the unit length (y) of your box?"<< endl;
cin >> UY;
cout << "What is the unit length (z) of your box?"<< endl;
cin >> UZ;
cout << "What is the multiplication factor to create this lattice?" << endl;
cin >> factor;
cout << "You are creating a lattice that contains "<< (UX * factor * UY * factor * UZ * factor * numpart)<< " particles"<< endl;
cout << "You are creating a lattice whose volume is " << (UX * factor * UY * factor * UZ * factor) << " volume units" << endl;

for (int x=0; x < factor;(x + UX))
{cout << "i am awesome" << endl;}

return 0;
}


I am so sorry I don't know how to put it in the "code" page...But I hope this shows up cleanly on the question page.

Nevermind the y and z values, I'm getting "i am awesome" in infinite loops! Why is this happening? Is it because the conditions inside the for () bracket doesn't recognize UX? How can I fix this problem?

Thanks!


See More: Infinite For Loops???

Report •


#1
November 19, 2010 at 02:12:47
Its ben sumtime since iv done this but you need to conecntrat on this bit of code.

for (int x=0; x < factor;(x + UX))

if you can explain what it is you want it to do it would help me understand how to solve. it seems to be the loop that is the problem you have set the int to 0 but i cant see what number in the counter you have set it to stop at. if there is not a number for it to reach and stop then it will continue in an infinite loop.

But i you can provide more information that might help as its been about 1 year since iv even look at code after finishing college.

Regards
Daniel


Report •

#2
November 19, 2010 at 02:19:30
Hi Daniel! yay! thanks for replying.
Yeah, I just realized that that's the problematic part of the code.

if you look up to where I'm declaring variables, I'm letting the user input what is UX and factor. I want to be able to use this within the condition of the for loop, but it ends up giving me an infinite loop of the output as i've written "i'm awesome"

How can I make sure that the computer knows that I want it to utilize user input values of UX and factor when i'm declaring conditions of the "for loop"?


Report •

#3
November 19, 2010 at 02:25:54
Erm wel im trying to think back. i think i understand what you cant which is sumthing we had to do where the user in put data an the the program showed the data back to the user.

im nt sure u need a loop just
cout <<" I CANT REMEMBER WHAT TO PUT HERE TO CALL THE USER INPUT. "<< endl;

Sorry i cant be more help. but it has been a while since iv done coding and dont have any of my old work with me at the moment.


Report •

Related Solutions

#4
November 19, 2010 at 05:06:54
I also have forgotten a lot. Try this.
....................................................................................................................
for (int x=0; x < factor;(x = x + UX))
....................................................................................................................

_Adnan_


Report •

#5
November 19, 2010 at 05:13:33
nope, still gives me the infinite loop of doom... ):

i figured out everything else but this. i'll ask my grad student advisor when she comes to school today...

thanks for your help though guys (:


Report •

#6
November 19, 2010 at 06:43:36
for (int x=0; x < factor;x += UX)

This should break you out of the loop, assuming UX is > 0.

How To Ask Questions The Smart Way


Report •

#7
November 19, 2010 at 06:47:18
This is what I came up with. This works, but I have tried all the options above and it still gives me the infinite loop of doom.

What I changed: UX/UY/UZ are now the same thing as x,y, and z.
factors is now f
numbpart is now numb

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
int numb;
int f;
double x;
double y;
double z;

cout << "How many particles are in one unit cube of your lattice?" << endl;
cin >> numb;
cout << "What is the unit length (x) of your box?"<< endl;
cin >> x;
cout << "What is the unit length (y) of your box?"<< endl;
cin >> y;
cout << "What is the unit length (z) of your box?"<< endl;
cin >> z;
cout << "What is the multiplication factor to create this lattice?" << endl;
cin >> f;
cout << "You are creating a lattice that contains "<< (x * f * y * f * z * f * numb)<< " particles"<< endl;
cout << "You are creating a lattice whose volume is " << (x * f * y * f * z * f) << " volume units" << endl;

for (x=0; x <3 ; x++ )
{for (y=0; y < 3; y++)
{for (z=0; z < 3; z++)
{cout<<"("<<x<<", "<<y<<", "<<z<< ")\n"<< endl;

}
}
}

for (x=0.5; x < 3 ; x++ )
{for (y=0; y < 3; y++)
{for (z=0.5; z < 3; z++)
{cout<<"("<<x<<", "<<y<<", "<<z<< ")\n"<< endl;

}
}
}

for (x=0; x < 3 ; x++ )
{for (y=0.5; y < 3; y++)
{for (z=0.5; z < 3; z++)
{cout<<"("<<x<<", "<<y<<", "<<z<< ")\n"<< endl;

}
}
}
for (x=0.5; x < 3 ; x++ )
{for (y=0.5; y < 3; y++)
{for (z=0; z < 3; z++)
{cout<<"("<<x<<", "<<y<<", "<<z<< ")\n"<< endl;

}
}
}

return 0;
}

I would really like to be flexible with this file, I don't want to have to change up the numbers in the .cpp file-- otherwise there's no point in asking for user input in the first place!

Thanks,


Report •

#8
November 19, 2010 at 07:04:11
Wait, in the original post, you're using ints. In this version, you're using doubles. This might be your problem.

How To Ask Questions The Smart Way


Report •

#9
November 19, 2010 at 07:14:08
I used doubles because I had to use four separate "for loops" and the initial values for x,y,and z are sometimes numbers with decimals.

that could potentially be a problem though i'll check it out, thanks


Report •

Ask Question