how do i exite a program with Ctrl+C

I can not figure out what code to use to allow a user to end my program in c++ by pressing ctrl+c can someone please help me?
closed account (ypfz3TCk)
What operating system are you using? On linux Ctrl-D ends the program - not sure about windows.
visual studio 2008
im just having trouble with the coding part
closed account (ypfz3TCk)
would you like to post the code you are having trouble with?
This is just my main. I left out all the functions I used. no need to take up space. but my if (ctrl+c) is the last code I have tried to use. I am currently all out of ideas.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#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;
		int Ctrl=0; 
		int c=0;
        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.";


	if (Ctrl + c)
	{
		std::exit(EXIT_FAILURE);
	}

         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;
   }
I think Ctrl-C is an EOF, maybe try if (EOF = cin.get()) return 0;.
now it complies. but with I press Control c my file just keeps running.
Second thought, ctrl Z is EOF.
do you have any other ideas? I tried doing something like while (ctrl+c!==something) but i couldn't figure that out either
closed account (ypfz3TCk)
line 63 cant compile. if (ctrl +c) has no meaning. to get it to work, try something simple like-
char input;
cin >> input;
bool status = false;
....if input valid, set bool status to true.

there are many ways of doing this. Good luck
Im not trying to be that guy that's looking for the answer but I burned myself out on this. is there anyway you could show me the full code to do it. Iv tried so many things I think I confused myself completely
You don't handle Ctrl+C yourself, that's done by the console.
@atown282

Here is a small program that I changed from checking for the ESC key, to looking for CTRL-C, to exit the program. Instead of ASCII 27, for ESC, it checks for ASCII 3, which is the code given with the CTRL-C combo. Hope it helps..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// CTRL-C.cpp : main project file.

#include <iostream>
#include <string>
#include <stdio.h>
#include <conio.h>

using namespace std;

int Function1(int func1)
{
	func1++;
	return func1;
}

int Function2(int func2)
{
	func2++;
	return func2;
}

int Function3(int func3)
{
	cout << "CTRL-C was pressed..\nEnding program.." << endl << endl;
	return func3;
}

int Function4(int func, string calling)
{
	cout << "Zeroing out " << calling << ".." << endl << endl;
	func=0;
	return func;
}

int main()
{
	char key;
	int call1=0,call2=0;
	do
	{
		cout << "Enter a '1', '2', or '0' character. Or press Ctrl-C to end program" << endl;
		key = _getch();
		switch(key)
		{
		case 49: //call to function 1
			{
				call1 = Function1(call1);
				cout << "call1 = " << call1 << endl;
				break;
			}
		case 50://call to function 2
			{
				call2 = Function1(call2);
				cout << "call2 = " << call2 << endl;
				break;
			}
		case 48://call to function 4
			{
				call1 = Function4(call1, "call1");
				call2 = Function4(call2, "call2");

				cout << "call1 now is " << call1 << " and call2 is " << call2 << " " << endl;
				break;
			}
		case 3:// Control-C is ASCII 3
			{
				Function3(key);
				break;
			}
		default:
			{
				cout << "** Error ** Only a 1, 2, 0 or Ctrl-C is recognized.." << endl << endl;
			}
		}
	}while (key!=3);
}
Topic archived. No new replies allowed.