I currently have a running program. All i want to do is be able to shut it down at anypoint by pressing CTRL C. I cannot figure out the code for this command for the life of me. Can someone please help me out? thank you. I am using c++ visual studio 2008

Recommended Answers

All 14 Replies

Dude i dont know visual studio for the life of me, but in Code::Blocks you just do Ctrl C.
really just press both keys and if the program does not shut down then im sorry. ill search the web for you in a bit too :)

if it does work it will always send you a error message, telling you a pre mature shut down(done by you).

message me if it worked. plus can you check my post i just did in the catagory C++, thanks maby you can help me.

I created the game of life. but i need it to end at anypoint using ctrl c and it wont recognize it and i dont no why,. thats why im stuck. i could post my code but i dont know if that will help

Oh it will please post,

by the way thats been happening to me to for ASCII key code's for example: if w pressed score++; nothing happens.

    #include <iostream>
    #include <cstdlib>
    #include <stdlib.h>  // You might be able to get away without this one.






    using namespace std;

    const int size = 24;

    typedef bool BoardType[size][size];

    // Function prototypes:


    void display(BoardType Board, int & iteration);

    bool Life(BoardType Board);

    void populate(BoardType Board, BoardType Board2);

    // A function prototype can't be used with an inline function (see below for function NumLiveNeighbors).  Just be careful to use it only after the function is defined.

    int main()
       {
            int Iteration = 0; // needed here to count the use of the display function
            int cycle;

            BoardType Board2;
            BoardType Board =
               {
                     {0,1,0,0,1,1,0,0,1,1,0,0,0,0,1,1,0,0,1,1,0,0,1,0,},
                      {1,0,1,1,0,1,0,1,0,1,0,1,0,1,1,1,1,0,0,1,0,1,1,1,},
                       {0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,},
                       {0,1,1,1,0,0,0,1,0,1,0,1,0,1,1,0,1,0,1,0,1,1,1,1,},
                       {1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,},
                       {1,0,1,0,1,0,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,1,1,1,},
                       {0,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,},
                       {0,0,1,0,1,1,1,1,1,1,0,1,0,1,1,1,1,0,0,0,0,1,1,1,},
                       {1,0,0,0,1,1,1,1,1,1,1,0,1,1,1,1,1,0,0,0,0,1,0,1,},
                       {1,0,1,0,1,0,0,0,0,0,1,0,1,1,1,1,1,1,0,1,0,1,0,1,},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
                       {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,},
                       {0,0,0,0,1,1,1,1,0,1,0,1,1,1,1,1,0,0,0,1,0,1,1,1,},
                       {0,1,0,1,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,0,1,0,},
                       {1,1,1,1,1,1,0,0,0,0,1,0,1,0,1,0,0,0,0,0,1,1,1,1,},
                       {1,0,0,0,0,1,0,1,0,1,0,1,1,1,0,1,0,1,0,1,0,1,1,1,},
                       {0,0,0,0,1,1,1,1,1,0,1,0,1,0,1,1,1,1,0,1,0,1,0,1,},
                       {0,1,1,1,1,0,1,0,1,0,1,0,1,0,1,1,1,1,0,1,0,1,0,1,},
                       {1,1,1,1,1,0,1,0,1,0,1,1,1,1,1,0,0,0,1,0,0,0,0,0,},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,},
                       {0,1,1,1,0,0,0,0,0,1,0,1,0,1,0,1,0,0,0,1,0,1,0,1,},
                       {0,1,1,1,1,0,0,0,1,1,1,1,0,0,0,0,0,1,0,1,1,1,1,0,},
                       {1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,},
                       {0,1,0,0,1,1,0,0,1,1,0,0,0,0,1,1,0,0,1,1,0,0,1,0,},
               };

            cout << "Enter the number of generations to run." << endl;
            cin >> cycle;
            cout << "You will need to press a key between cycles" << endl;
            cout << "Press CTRL + C to exit at any time.";




             system("PAUSE");
            system("cls");
            display(Board, Iteration);
            cout << endl << endl << endl;







            for (int i = 1; i <= cycle; i++)
               {
                    // If system("PAUSE") does not work with your version of Visual Studio
                    // try the method of prompting the user to press Enter to go on, then
                     // read that keypress into a char variable.  Similarly, if system("cls")
                    // does not work for you, you might be able to clear the screen by
                    // outputting the correct number of endl's.
                    system("PAUSE");        //creates the pause between generations
                    system("cls");          // Clears the screen
                    populate(Board, Board2);
                    display(Board, Iteration);
               }


            return 0;
       }

    /* Given:   Board       A 2D array of type Bool, serving as the matrix for our game
                Row         An integer defining the row of the cell being checked.
                Col         An integer defining the column of the cell being checked.
       Task:         Calculate the number of live neighboring cells.
       Return:  In the function name, return the count of the number of live neighbors.
    */



    inline int NumLiveNeighbors(BoardType Board, int Row, int Col)
       {
              int NumLiveNeighbors = 0;

            int top, bottom, right, left;

               if (Row==0)
               {
                       top=0;
                       bottom=1;
               }

                       else if (Row==23)
                       {
                               top=22;
                               bottom=23;
                       }
                       else
                       {
                               top=Row-1;
                               bottom= Row+1;
                       }

                       if (Col==0)
               {
                       left=0;
                       right=1;
               }

                       else if (Col==23)
                       {
                               left=22;
                               right=23;
                       }
                       else
                       {
                               left=Col-1;
                               right=Col+1;
                       }


              for (int k=top; k<=bottom; k++)
                      {
                              for (int c=left; c<=right; c++)
                                      if (Board[k][c]==true)
                                              NumLiveNeighbors++;
                      }

                      if (Board[Row][Col]==true)

                              NumLiveNeighbors--;



            // Write the code for this function.  There are several ways to cout the
            // number of neighbors.  Be careful not to go beyond the edges of the
            // Board.  That means there are several special cases to consider, such
            // as when Row and Col are for one of the corners of the Board, or when Row
            // and Col specify another location on the edge of the Board.
                    return NumLiveNeighbors;


       }

    /*      Given:  Board   A 2D array of type Bool, serving as the matrix for our game
                Row         An integer defining the row of the cell being checked.
                Col         An integer defining the column of the cell being checked.
       Task:         Calculate the number of live neighboring cells using checkmid,
                checktop, checkbottom. Determine if the cell being studied will be
                alive or dead for the next generation (See rules Above)
       Return:  True     if the cell will be live for the next generation
                False    if the cell will be dead for the next generation,
    */
    bool Life(BoardType Board, int Row, int Col)
       {
            int live = NumLiveNeighbors(Board, Row, Col);

                    if ((Board[Row][Col]== true) && (live==2 || live==3))  // if Board[Row][Col] is true then...
               {

                            return true;
               // If the number of live neigbors is too large or small (see the rules)
                  // then return false to say that the cell will die.
                 // Otherwise, return true to say the the cell will remain alive.
    /*1. Any live cell with fewer than two live neighbors dies, as if caused by underpopulation.
    2. Any live cell with more than three live neighbors dies, as if by overcrowding.
    3. Any live cell with two or three live neighbors lives on to the next generation.
    4. Any dead cell with exactly three live neighbours becomes a live cell.
    */

               }
            else if ((Board[Row][Col] == true) && (live < 2 || live > 3)) // if Board[Row][Col] is false then...
               {
                       return false;
                    // If the number of live neighbors is just right, return true to say
                    // that this dead cell will come to life.  Otherwise return false to say
                    // that this dead cell stays dead.  (Again, refer to the rules.)

               }
                    else if ((Board[Row][Col]==false) && (live==3))
                    {
                            return true;
                    }

                    else // ((Board[Row][Col]==false) && (live<3 || live>3))
                    {
                            return false;
                    }

       }

    // The rest of the program has been written for you.  Don't change it. ************

    /*      Given: Board    A 2D array of type Bool, serving as the matrix for our game.
                Board2      Board2 will be populated as the function Life determines
                         whether the cell is live or dead in the next generation,
                         after Board2 is populated, it will be copied into Board, to
                         represent itself as the current generation.
       Task:        Populate Board2 with the values from Life, representing the next
               generation. Copy the populated Board2 into Board, the contents of
               Board2 are now the current generation.
       Return:  Board    Containing the new generation of cells.
                Board2   Containing a copy of Board.
    */
    void populate(BoardType Board, BoardType Board2)
       {
            for (int row = 0; row < size; row++)
                    for (int col = 0; col < size; col++)
                            Board2[row][col] = Life(Board, row, col);

            for (int row = 0; row < size; row++)
                    for (int col = 0; col < size; col++)
                            Board[row][col] = Board2[row][col];
       }

    /*      Given:  Board     The Array to Display
                iteration  A count of cycles that the function has run.
       Task:          Display the contents of Board in an easy-to-read format.
       Return:  Iteration   Updated count of number of generations displayed.
    */
    void display(BoardType Board, int & iteration)
       {
            cout << "    ";
            for (int Col = 0; Col < size; Col++)
               {
                    if (Col < 10)
                            cout << Col << "  " ;
                    else
                            cout << Col << " ";
               }
            cout << endl << endl;

            for (int Row = 0; Row < size; Row++)
               {
                    if (Row < 10)
                            cout << Row << "   ";
                    else
                            cout << Row << "  ";
                    for (int Col = 0; Col < size; Col++)
                       if (Board[Row][Col])
                               cout << "X  " ;
                            else
                               cout << "   ";
                    cout << endl;
               }

            cout << " GENERATION: " << iteration << endl;
            iteration++;
       }

0_0
LOL

ok and CTRL C does not work?

weird.

by the way ive not got the slightest clue of the game of life and how it works.

sorry.

The Code is a little beyond me(only the BOOL part(Im still learning)), any ways Code looks great though :).

lol yeah i dont get it either.... ahah thanks anyways. and thank you

I don't see anything in your code that would cause Ctrl-C to not deliver a kill signal to the application. Since this is a console application, I assume you are running it in a cmd.exe window?

Problem is your are using system(pause) to pause your run. You are waiting at the OS at that point, not your program.

C++ has input functions, like cin, getline(), etc that waits better than system(pause). Use them instead. There is no reason to call the OS to pause any program. Same with cls...

Depeinding on your OS: if you do work in UNIX, it is possible by a C-signal handle function.

#include <signal.h>
#include <stdlib.h>

void handler(int sig){
    printf("Received signal %d and now I'm exiting.", sig); // or cout.
    exit(0);
}

int main(){
    signal(SIGINT, handler);
    //do stuff;
    return (0);
}

But this is done on UNIX, duno on Windows. From what I know, CTRL+C should work in Visual C++ in a console program.

commented: Since when does Visual Studio C++ work on Linux? -3

@rubberbandman yeah it runs on cmd.exe should it kill it because the command is in the consoul? or do i need special coding for that

I just cant figure this out. I was under the impression that since its an .exe it should do it its self. but its not. and I cannot figure out the code for it

nvm im an idoit thanks everyone for your help.

So what was the problem? And your solution?

Well that was a way of doing things in UNIX, no need for a down-vote on that but heh, it's your choice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.