Thread: How do i get it to pause ?

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    91

    How do i get it to pause ?

    Hi ,

    Im Just starting C++ again,

    I need to find outhow you get a prog to pause for example a basic prog like this will just dissapear beofre you get to see the result.


    #include <iostream.h>

    int main()

    {

    int a,b,c,d

    cout<<"Prices of eggs";
    cin>>a;
    cout<<"Prices of bread";
    cin>>b;
    cout<<"Price of milk";
    cin>>c;
    cout<<"Prie of chicken";
    cin>>d;
    cout<<"total price = "<<a+b+c+d;
    return 0;

    }

    thanks !!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You can try putting in a cin.get(); or two at the end of your code, that's worked for me.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    22
    Here is something simple you can do. Just add the header file conio.h and at the end of your source code, just before return 0; add the line getch(); this will wait until you press any key, then close your program.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    91
    thanks

    getch(); worked fine, i remember that from C now,

    Whats the differnece between cin.get(); and getch(); ?

    They seems to be described as the same thing in the tutorials by cin.get(); doesnt seem to make any differnce ?

    thanks again

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by joeyzt
    thanks

    getch(); worked fine, i remember that from C now,

    Whats the differnece between cin.get(); and getch(); ?

    They seems to be described as the same thing in the tutorials by cin.get(); doesnt seem to make any differnce ?

    thanks again
    conio.h isn't standard and isn't supported by all compilers.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Whats the differnece between cin.get(); and getch(); ?
    cin.get() uses buffered IO (like all standard language IO functions). You have to press <enter> to force data into your program.

    getch() from conio.h uses non-buffered IO, so a simple press of (almost) any key will be passed straight to your program.

    From the FAQ (already linked to), try these to see the difference.
    Code:
    #include <iostream> 
    
    int main(void)
    {
      std::cout <<"Press [Enter] to continue" <<std::endl;
      std::cin.get();
      return(0);
    }
    Code:
    #include <iostream> 
    #include <conio.h>
    
    int main(void)
    {
      std::cout <<"Press the anykey to continue" <<std::endl;
      getch();
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Fork(), pause(), and storing PID's in a linked list
    By vital101 in forum C Programming
    Replies: 10
    Last Post: 09-28-2007, 02:16 AM
  3. Dev-C++ Compile and Run with Pause
    By xmltorrent in forum C++ Programming
    Replies: 12
    Last Post: 03-29-2006, 11:55 PM
  4. pause program at console
    By datainjector in forum C# Programming
    Replies: 1
    Last Post: 03-27-2003, 12:36 AM
  5. Pause function ???
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 09-02-2001, 08:22 PM