MMOCoin

Likes Likes:  0
Results 1 to 6 of 6

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Beginner

    Join Date
    Feb 2010
    Posts
    24
    Post Thanks / Like
    Rep Power
    17
    Reputation
    3

    Pausing a C++ Program

    Greetings everyone!



    I'm currently trying to grasp the basics of C++. I've been using 'system("PAUSE")' to pause a program but according to a number of programmers, this is a rather bad habit. Is there a better, more efficient way to pause a program please? I'm currently using Visual Studio 2008.

    Help is always much appreciated.



  2. Related Threads - Scroll Down after related threads if you are only interested to view replies for above post/thread

  3. #2
    Banned

    Join Date
    Sep 2009
    Posts
    90
    Post Thanks / Like
    Rep Power
    0
    Reputation
    103
    Why would you need to pause a program?

  4. #3
    Beginner

    Join Date
    Feb 2010
    Posts
    24
    Post Thanks / Like
    Rep Power
    17
    Reputation
    3
    Let's take this program as an example:

    Code:
    // This programs converts feet to metres or vice versa
    
    #include <iostream>
    using namespace std;
    
    int main ()
    {
        short userchoice;
        double metres;
        double feet;
    
        cout << "To convert feet to metres, please press 1\n";
        cout << "To convert metres to feet, please press 2\n";
        cin >> userchoice;
        cout << "\n";
    
        if (userchoice == 1)
        {
            cout << "Please enter the number of feet: ";
            cin >> feet;
            metres = feet/3.28;
            cout << feet << " feet = " << metres << " metres\n";
        }
    
        if (userchoice == 2)
        {
            cout << "Please enter the number of metres: ";
            cin >> metres;
            feet = metres*3.28;
            cout << metres << " metres = " << feet << " feet\n";
        }
    
        cout << "\n";
        system("PAUSE");
        return 0;
    }
    In this case, a pause is needed after displaying the result or else the program would instantly close without allowing the user to see it. Should I stick to 'system("PAUSE")' for simple programs?

  5. #4
    Banned

    Join Date
    Sep 2009
    Posts
    90
    Post Thanks / Like
    Rep Power
    0
    Reputation
    103
    Quote Originally Posted by Synapse View Post
    Let's take this program as an example:

    Code:
    // This programs converts feet to metres or vice versa
    
    #include <iostream>
    using namespace std;
    
    int main ()
    {
        short userchoice;
        double metres;
        double feet;
    
        cout << "To convert feet to metres, please press 1\n";
        cout << "To convert metres to feet, please press 2\n";
        cin >> userchoice;
        cout << "\n";
    
        if (userchoice == 1)
        {
            cout << "Please enter the number of feet: ";
            cin >> feet;
            metres = feet/3.28;
            cout << feet << " feet = " << metres << " metres\n";
        }
    
        if (userchoice == 2)
        {
            cout << "Please enter the number of metres: ";
            cin >> metres;
            feet = metres*3.28;
            cout << metres << " metres = " << feet << " feet\n";
        }
    
        cout << "\n";
        system("PAUSE");
        return 0;
    }
    In this case, a pause is needed after displaying the result or else the program would instantly close without allowing the user to see it. Should I stick to 'system("PAUSE")' for simple programs?
    I suggest that you download Visual Studio, since you don't need to pause the program if you use it.
    To run the program simply press ctrl + F5

  6. #5
    Beginner

    Join Date
    Feb 2010
    Posts
    24
    Post Thanks / Like
    Rep Power
    17
    Reputation
    3
    Thanks for the tip mate!

  7. #6
    Banned

    Join Date
    Aug 2008
    Location
    Spartanburg, SC
    Posts
    71
    Post Thanks / Like
    Rep Power
    0
    Reputation
    68
    you could create a loop that after the program is run, it asks if the user wants to run the program again. If so loop to the beginning, if not the program exits.

 

 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •