Thread: keeping console window open

  1. #1
    Unregistered
    Guest

    keeping console window open

    i have a simple program i am learning on wich just ask you for in put to store in a variable then will multiply it by the other nuber you enter , then a message will apear if there == . Problem is after entering the second number and hitting the enter key the console window closes. i have cin.get() before return 0 .
    But i wish to know how to keep it open to see the full exacution of the code.

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Code:
    #include <cconio>
    #include <iostream>
    
    int main(int argc, char *argv[])
    {
         int number[2];
         std::cout << "Please enter a number: ";
         std::cin >> number[0];
         std::cout << "\nEnter another number: ";
         std::cin >> number[1];
         std::cout << std::endl << number[0] << '*' << number[1] << " = " << number[0] * number[1] << endl;
         std::getch();
         return 0;
    }

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    317
    The reason for this is that cin will leave the enter keystroke in the input buffer, therefore the cin.get at the end of the pregram simply retrieves that and exits. You can use either getch(), cin.get() twice, or cout.flush to empty the input buffer.

    Edit: Not stepping on your toes Dual Catfish, simply just explaining.

  4. #4
    Unregistered
    Guest
    Thank you for your help , that makes sence now.

  5. #5
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    system("PAUSE");
    .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  2. internet
    By yeah in forum C Programming
    Replies: 16
    Last Post: 02-12-2005, 10:37 PM
  3. Linux console window questions
    By GaPe in forum Linux Programming
    Replies: 1
    Last Post: 12-28-2002, 12:18 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Invoking MSWord
    By Donn in forum C Programming
    Replies: 21
    Last Post: 09-08-2001, 04:08 PM