Keeping the console open.

This is a discussion on Keeping the console open. within the C++ Programming forums, part of the General Programming Boards category; Ok.. i use dev c++ and when i make a console app that requires a varible to type in, it ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    3

    Question Keeping the console open.

    Ok.. i use dev c++ and when i make a console app that requires a varible to type in, it doesnt stay open. I was trying one day to do one of the example programs off this site that required a varible which was your age, when i used a command the page said for dev to keep the window opn, it closed the window when enter was pressed


    What command do i use to keep the window open and close it if pushed.. like if i wanted a to close it while some part of code keeps it open until "a" is pressed

  2. #2
    Registered User SPiRiToFCaT's Avatar
    Join Date
    May 2002
    Posts
    35
    It's easy to do so that hitting a then enter will close it.
    To do that here is the code:
    Code:
    #include <iostream.h>
    
    int main()
    {
     char input;
     cin>>input;
     while(input!='a')
     {
      cin>>input;
     }
     return 0;
    }
    Define a character variable.
    Get a value from the user.
    If it was a, skip the loop and end.
    Otherwise, keep getting a character until it is a.

    Hope that's of use.
    - Well that's my 4c Australian.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Ok if i get what your saying the console is closing before you want it to. There are several ways to fix this. Heres what i use.

    Code:
    #include <iostream>
    #include <conio.h>  // for getch
    using namespace std;
    
    
    int main ()
    
    {
         int age;
        
        cout << "Enter your age";
        cin >> age;
    
        getch();  // ALLOWS console to close when use presses any key
        return 0;
    }
    Always put getch() before return 0. There are others like cin.get and system.pause but i found this to work better. Hoped i helped
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    3
    thanks but how then would i make it if u entered an number or text to have it so it doesnt close when that varible is typed

  5. #5
    Registered User SPiRiToFCaT's Avatar
    Join Date
    May 2002
    Posts
    35
    I don't quite understand what you mean?
    Do you mean you want it to close if you type a but not if you type man or aardvark or something like that?
    For that you can use strings.

    #include <string>
    Then define a string instead of a char and check against "a" instead of 'a'
    - Well that's my 4c Australian.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    well if you mean at the end of the program like my example before use that , getch(). Maybe try system("pause") in the area you want the program to stop. Did you try the examples we gave you. If that still doesnt help maybe show us and example of what you mean. Forgot to mention if you use system("pause") use the #include <stdlib.h> header file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open Source Licenses
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 10-10-2006, 09:53 PM
  2. Full Screen Console
    By St0rmTroop3er in forum C++ Programming
    Replies: 1
    Last Post: 09-26-2005, 10:59 PM
  3. Console Functions Help
    By Artist_of_dream in forum C++ Programming
    Replies: 9
    Last Post: 12-04-2004, 03:44 AM
  4. Problems with open and save boxes
    By pinkcheese in forum Windows Programming
    Replies: 3
    Last Post: 05-21-2002, 07:03 PM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 06:38 PM

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