Thread: Closing a programme with cin.get

  1. #1
    Registered User
    Join Date
    Mar 2009

    Closing a programme with cin.get

    Hi I am a beginner and just wrote a very small programme to calculate the circumference and area of a rectangle. The problem is when I compile the programme it just closes automatically in the end and I don't want it to close till I press enter. Here is the programme. The first time it calculate the circumference but when it reaches calculating the area, it just closes automatically after I enter the value for the width without showing the result. I used cin.get as you can see but it doesn't work.

    using namespace std;
    int main () {
        double l, w, C, A;
        cout <<"Length?";
        cin>> l;
        cout <<"Width?";
        cin >>w;
        C = w*2 + l*2;
        cout <<"The circumference of the rectangle is"<<C<< endl; 
        cout <<"Length?";
        cin>> l;
        cout <<"width?";
        cin>> w;
        A = l*w;
        cout<<"The area of the rectangle is"<< A;

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    operator >> leaves the \n character in the input buffer - you should get read of it before you start waiting on the press enter...

    alternatively - you could just run your program from the command prompt
    or - if you are using VS - using Ctrl+F5 from IDE
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    I strongly recommend just running the program manually from the cmd prompt instead of hitting F5. Having a pause/cin.get()/sleep() at the end of the program is convenient during development, but is a serious problem when the code goes to release.

    You shouldn't leave stuff in your code that's only useful to the developer, if it gets in the way of normal usage.
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. How to view the programme before it disappears
    By yousuf in forum C Programming
    Replies: 2
    Last Post: 03-22-2008, 08:12 AM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. cin.get() problem
    By Cilius in forum C++ Programming
    Replies: 20
    Last Post: 07-28-2005, 05:32 PM
  5. Gui Programme does'nt Compiles from DOS
    By Shadowhunt in forum C++ Programming
    Replies: 1
    Last Post: 06-06-2003, 08:05 AM