EvilZone

Programming and Scripting => C - C++ => : Dijkstra May 03, 2013, 02:17:36 AM

: C++ Help!
: Dijkstra May 03, 2013, 02:17:36 AM
Well I am in a pickle. I really need to figure this out by tomorrow, but I cannot for the life of my figure out why this is looping. I am no expert by far, more of a networking guy. Any help would be greatly appreciated.


http://pastebin.com/RQGjCgTd
: Re: C++ Help!
: Muhammad May 03, 2013, 04:29:15 AM
can you explain the problem, so I can help ?
: Re: C++ Help!
: Dijkstra May 03, 2013, 04:47:52 AM

It is supposed to be able to solve any geometrical optics system using matrix algebra. But now, it just wants to infinitely loop itself.


When I compile it, all I get is:


This is a program that will perform matrix computations to output a system matrix, provided enough inputs are given. How many elements does this system have? Note that a thick lens counts as three elements: This is a program that will perform matrix computations to output a system matrix, provided enough inputs are given.


Thank you for your help
: Re: C++ Help!
: Muhammad May 03, 2013, 04:51:00 AM
so at the very beginning you set
:
decision == 1


but you never change its value, hence never break from the while loop :)
: Re: C++ Help!
: Dijkstra May 03, 2013, 04:57:40 AM
At the end I cin a new decision though?
: Re: C++ Help!
: Muhammad May 03, 2013, 05:02:43 AM


yep, you do that outside the while loop. your code right now is like:


:
case = true;     // initial value
while(case)
{


}                     // end of while loop (case still equals the initial value)
case = false; // you'll never get here !




you should change the decision at the end of the while loop
: Re: C++ Help!
: Dijkstra May 03, 2013, 05:12:17 AM
Thank you Muhammad,

I moved the "}" down.


I am still getting loops with the code (index of 1, and index of prime).


Loop shows:

Do you wish to analyze another optical system? Enter '1' for 'yes' or '2' for 'no': This is a program that will perform matrix computations to output a system matrix, provided enough inputs are given. How many elements does this system have? Note that a thick lens counts as three elements: 0x7ffff961cb40

http://pastebin.com/VUBEgUPz (http://pastebin.com/VUBEgUPz)
: Re: C++ Help!
: Muhammad May 03, 2013, 05:41:22 AM
I'm not sure why this is happening .. If you enter 2 at the end of the program it will break you out of the while loop.

Try adding this  at line 409:

: (cpp)
if (decision == 2 ) break;
: Re: C++ Help!
: Dijkstra May 03, 2013, 02:10:29 PM
Adding that fixes the planar refraction loop. The other options continue to loop for some reason. I know it has to be something simple, but it is not as obvious as I wish it was :(.
: Re: C++ Help!
: Deque May 03, 2013, 05:13:18 PM
:
if(counter = 0) //this is for the first matrix calculation, so the element isn't multiplied by zero
    {

It doesn't stop asking for new elements, because you assign 0 to the counter everytime in this if-statement.

Because of such hard to spot errors it is a better habit to make comparisons like this:

:
if(0 == counter) //this is for the first matrix calculation, so the element isn't multiplied by zero
    {

If you had done the same mistake here, the compiler would have told you.

Btw your intendation is bad. You should do something about it. It is hard to see the structure of your code.
: Re: C++ Help!
: Dijkstra May 05, 2013, 04:57:58 AM
Thank you Deque and Muhammad, I finally got this working with your assistance.