Closing a programme with cin.get

This is a discussion on Closing a programme with cin.get within the C++ Programming forums, part of the General Programming Boards category; Hi I am a beginner and just wrote a very small programme to calculate the circumference and area of a ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    102

    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.

    Code:
    #include<iostream>
    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;
        cin.get();
        }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    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.
    Code:
    //try
    //{
    	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, 06:59 PM
  2. How to view the programme before it disappears
    By yousuf in forum C Programming
    Replies: 2
    Last Post: 03-22-2008, 09:12 AM
  3. Replies: 3
    Last Post: 05-13-2007, 09:55 AM
  4. cin.get() problem
    By Cilius in forum C++ Programming
    Replies: 20
    Last Post: 07-28-2005, 06:32 PM
  5. Gui Programme does'nt Compiles from DOS
    By Shadowhunt in forum C++ Programming
    Replies: 1
    Last Post: 06-06-2003, 09:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21