Loop to run the program again?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kick
    New Member
    • Feb 2010
    • 3

    Loop to run the program again?

    Hi!
    I´m a newbe at programming and c aswell.
    I wrote a simple program and have a problem with a loop.
    My last output line after running the program is: "Press any key to run the program again", and I can`t get it to work like this. Right now the program just starts again without waiting for the user input.

    Can anyone please help me and show a example of a loop that does this,

    thanks,
    Crissy
  • lzndcb
    New Member
    • Feb 2010
    • 3

    #2
    #include<iostre am>
    int main()
    {
    int val;
    std::cout << "Enter a int" << std::endl;
    while( std::cin>>val) //use a loop to the input
    std::cout << "What you input:" << val << std::endl;
    return 0;
    }

    Comment

    • kick
      New Member
      • Feb 2010
      • 3

      #3
      Not like that

      I mean C, not C++. But syntax the same, anayway I´m not sure if thats what I was looking for..
      I want, like I described in my first post, the program to run again, AFTER any key is pressed, not that the program just start again after last input, like your program does(?) - if I understand it correct.My program does that now, but I want it to run after any key is pressed.

      But thanks anyway! :-)

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Take your entire main() function and rename to OldMain.

        Then write a new main():

        Code:
        int main()
        {
             while (1)
             {
                  OldMain();
             }
        }
        I leave it to you to end the program.

        Comment

        • kick
          New Member
          • Feb 2010
          • 3

          #5
          Yeah right!

          Thanks it works now :-)

          Comment

          Working...