do/while loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tops
    New Member
    • Jan 2008
    • 4

    do/while loop

    Hey guys I'm trying to figure out how to input the do/while loop so that my program doesn't execute too fast and you wont be able to see the solution. So I read in one of the forums about using a do/while loop so that the program will ask if you want to run it again.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Hmm, that really depends on what your program does. Do-whiles are really good for iterations - sequences, things like that, because you want them to do something while you have something else.

    So if you are just doing a simple formula - calculating something that won't change, a do-while would not be ideal, unless you had the condition (the 'do' part) to keep asking if you wanted to wait to show the answer while (the 'while' part) the user would enter no (and then, if they answered yes, you would just exit the loop and print the answer).

    So what does your program do?

    Comment

    • alind
      New Member
      • Sep 2007
      • 7

      #3
      Check on the char in the wile loop for y/n will solve ur problem.
      pseudo code:

      condition = 'n'
      do
      {
      process...
      process...
      condition = <get char input from keyboard>
      }
      while(condition = 'y')

      //This wil solve ur probem (if i understod ur post). else describe ur problem wth some example.

      Comment

      • Tops
        New Member
        • Jan 2008
        • 4

        #4
        This is what I'm using

        int main()
        {
        double length;
        double width;
        double area;

        blah ...

        blah...


        return 0;
        }

        I don't know if that help at all, I just want the program to have a pause at least before it closes so you can see the solution it comes up with...

        Comment

        • Shashi Sadasivan
          Recognized Expert Top Contributor
          • Aug 2007
          • 1435

          #5
          Guess you are using one of the IDE's which closes the output window as soon as the program finishes executing.

          use getch(); so that the program waits for a user input. that does the trick

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by Shashi Sadasivan
            Guess you are using one of the IDE's which closes the output window as soon as the program finishes executing.

            use getch(); so that the program waits for a user input. that does the trick
            While this is the right idea, it's not the right implementation. Getch() is from the non-standard library conio.h, which is different from system to system, and version to version, meaning it can mess things up if you don't have the exact same version across all systems.

            A better way would be to, at the end of your program, prompt the user, "Press enter to end program" and then just have any sort of cin or scan() (as you don't mention if you are using C or C++), and just ignore the input values.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Are you using Visual Studio.NET???

              If so, run your program by selecting Start without Debugging. That will pause the display after the end of main() so you can see what happened.

              No need to added gimcracks to your code to pause it.

              This selection tells the compiler that you are not using your debugger and therefore do not have any pauses set up.

              The other selection is Start. Here, the program just runs and any pauses have to be part of the code.

              Comment

              Working...