13 Replies - 367 Views - Last Post: 10 January 2013 - 10:15 PM Rate Topic: -----

#1 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Exiting loops in C++

Posted 10 January 2013 - 10:29 AM

Hi,

I am working on a Project called math Ops and using alot of While and Do while loops in it. But here is my question. Is there a way to exit the loops without having questions and start back the program from the top.

Here is the project by the way:
http://www.dreaminco...ations-program/

:)

Is This A Good Question/Topic? 0
  • +

Replies To: Exiting loops in C++

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9206
  • View blog
  • Posts: 34,589
  • Joined: 12-June 08

Re: Exiting loops in C++

Posted 10 January 2013 - 10:33 AM

Quote

is there a way to exit the loops without having questions

I am pretty certain you can just use "break;". Throw it in some sort of conditional logic and you are good to go.

Quote

and start back the program from the top.

I am certain your program has one uber loop that keeps it going until the user says 'quit!', right? That should automatically take care of the problem.
Was This Post Helpful? 1
  • +
  • -

#3 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Re: Exiting loops in C++

Posted 10 January 2013 - 11:19 AM

hmm a break statement. but how do i tell my program to start from top instead of continuing on?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9206
  • View blog
  • Posts: 34,589
  • Joined: 12-June 08

Re: Exiting loops in C++

Posted 10 January 2013 - 11:28 AM

Goto?
http://www.learncpp....oto-statements/

That or you need some sort of control structure setup. A simple bit flag would work.. something like 'bcontinue'.. at each loop if that is true continue into the loop.. and if you want to break a loop and go back to the top then right before you call 'break' set it false..

Of course semi hinky things like that may just point to a need to redesign.. but oh well.
Was This Post Helpful? 0
  • +
  • -

#5 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Re: Exiting loops in C++

Posted 10 January 2013 - 11:29 AM

I am in intermediate to C++ still have lots to learn
Was This Post Helpful? 0
  • +
  • -

#6 lukeme99  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 15-March 11

Re: Exiting loops in C++

Posted 10 January 2013 - 11:40 AM

Ahh! I have had this dilema before! A break, breaks out of the imediate loop that its inside, so you could do this!
#include <iostream>

using namespace std;

int main()
{
    while(true) // Loops main loop forever (until broken)
    {
        while(true) // Inner loop. Loops, again, forever!!
        {
        [...] <- Your code!
        if(blah blah = restart blah) break; <-breaks the inner loop.
        }
    if(blah blah = quit blah) break;
    }
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#7 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5314
  • View blog
  • Posts: 27,220
  • Joined: 10-May 07

Re: Exiting loops in C++

Posted 10 January 2013 - 11:44 AM

Break, Goto? How about just using proper logic in your conditional statement? That's the purpose of writing the loop. Do this until this statement.
Was This Post Helpful? 0
  • +
  • -

#8 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Re: Exiting loops in C++

Posted 10 January 2013 - 12:08 PM

I mean there is nothing wrong with the code as is. I just want to make things easier for me. I don't know if there is a way to do that lol

View Postlukeme99, on 10 January 2013 - 11:40 AM, said:

Ahh! I have had this dilema before! A break, breaks out of the imediate loop that its inside, so you could do this!
#include <iostream>

using namespace std;

int main()
{
    while(true) // Loops main loop forever (until broken)
    {
        while(true) // Inner loop. Loops, again, forever!!
        {
        [...] <- Your code!
        if(blah blah = restart blah) break; <-breaks the inner loop.
        }
    if(blah blah = quit blah) break;
    }
    return 0;
}



While (True).. Bad Idea. no offense but i rather have a condition in the while statement it prevents errors. I speak from class experience. I passed C++ with a B+ :)
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9206
  • View blog
  • Posts: 34,589
  • Joined: 12-June 08

Re: Exiting loops in C++

Posted 10 January 2013 - 12:08 PM

Yes.. the way is to redesign so you are not having this problem.. or forge ahead by checking some bit flag before each loop to see if it should continue being processed or not.
Was This Post Helpful? 0
  • +
  • -

#10 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Exiting loops in C++

Posted 10 January 2013 - 08:02 PM

Create a flag to represent the state of the process. This flag will be a condition for both your nested and outer loop.

i.e.
#include <iostream>

int main() {
   bool cond = true;

   for(int i=0; cond; i++) {
      for(int j=0; cond; j++) {
         if(/* condition to exit */)
            cond = false;
      }
   }

   return 0;
}



Was This Post Helpful? 1
  • +
  • -

#11 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Re: Exiting loops in C++

Posted 10 January 2013 - 09:17 PM

View Postjjl, on 10 January 2013 - 08:02 PM, said:

Create a flag to represent the state of the process. This flag will be a condition for both your nested and outer loop.

i.e.
#include <iostream>

int main() {
   bool cond = true;

   for(int i=0; cond; i++) {
      for(int j=0; cond; j++) {
         if(/* condition to exit */)
            cond = false;
      }
   }

   return 0;
}




Any thing simpler lol.. I am just lazy.. I hate for loops lol.. I perfer to use Do While or While loops in my programs. Unless its required by an assignment i never really use for loops.
Was This Post Helpful? 0
  • +
  • -

#12 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Exiting loops in C++

Posted 10 January 2013 - 10:06 PM

Quote

Any thing simpler lol ... I hate for loops

You hate for loops because ....?

Just use while loops then
Was This Post Helpful? 2
  • +
  • -

#13 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Exiting loops in C++

Posted 10 January 2013 - 10:12 PM

Well, yes...I have to agree with jjl :)

If you don't like for loops, all you need to do is reduce the for loop into a while loop.

Something like this

for(int i=0;i<10;i++)
{
  //some code
}


can be made as

int i=0;
while(i<10)
{
  //some code
  i++;
}


regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

#14 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Re: Exiting loops in C++

Posted 10 January 2013 - 10:15 PM

Topic Closed :)... Unless anyone else has any more suggestions.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1