Author Topic: C++ Help!  (Read 2283 times)

0 Members and 1 Guest are viewing this topic.

Offline Dijkstra

  • Serf
  • *
  • Posts: 44
  • Cookies: 6
    • View Profile
C++ Help!
« on: 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

Offline Muhammad

  • /dev/null
  • *
  • Posts: 10
  • Cookies: 4
    • View Profile
Re: C++ Help!
« Reply #1 on: May 03, 2013, 04:29:15 am »
can you explain the problem, so I can help ?

Offline Dijkstra

  • Serf
  • *
  • Posts: 44
  • Cookies: 6
    • View Profile
Re: C++ Help!
« Reply #2 on: 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

Offline Muhammad

  • /dev/null
  • *
  • Posts: 10
  • Cookies: 4
    • View Profile
Re: C++ Help!
« Reply #3 on: May 03, 2013, 04:51:00 am »
so at the very beginning you set
Code: [Select]
decision == 1


but you never change its value, hence never break from the while loop :)
« Last Edit: May 03, 2013, 04:53:49 am by Muhammad »

Offline Dijkstra

  • Serf
  • *
  • Posts: 44
  • Cookies: 6
    • View Profile
Re: C++ Help!
« Reply #4 on: May 03, 2013, 04:57:40 am »
At the end I cin a new decision though?
« Last Edit: May 03, 2013, 04:57:54 am by Dijkstra »

Offline Muhammad

  • /dev/null
  • *
  • Posts: 10
  • Cookies: 4
    • View Profile
Re: C++ Help!
« Reply #5 on: May 03, 2013, 05:02:43 am »


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


Code: [Select]
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
« Last Edit: May 03, 2013, 05:04:14 am by Muhammad »

Offline Dijkstra

  • Serf
  • *
  • Posts: 44
  • Cookies: 6
    • View Profile
Re: C++ Help!
« Reply #6 on: 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
« Last Edit: May 03, 2013, 05:17:58 am by Dijkstra »

Offline Muhammad

  • /dev/null
  • *
  • Posts: 10
  • Cookies: 4
    • View Profile
Re: C++ Help!
« Reply #7 on: 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:

Code: (cpp) [Select]
if (decision == 2 ) break;
« Last Edit: May 03, 2013, 05:48:02 am by Muhammad »

Offline Dijkstra

  • Serf
  • *
  • Posts: 44
  • Cookies: 6
    • View Profile
Re: C++ Help!
« Reply #8 on: 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 :(.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: C++ Help!
« Reply #9 on: May 03, 2013, 05:13:18 pm »
Code: [Select]
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:

Code: [Select]
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.
« Last Edit: May 03, 2013, 05:13:38 pm by Deque »

Offline Dijkstra

  • Serf
  • *
  • Posts: 44
  • Cookies: 6
    • View Profile
Re: C++ Help!
« Reply #10 on: May 05, 2013, 04:57:58 am »
Thank you Deque and Muhammad, I finally got this working with your assistance.