C++ how do I terminate my programm using ESC button - c++

Here is my main function i use visual studio 2012 express and the code works fine. My question is how will i terminate this loop when the user Presses the ESC button instead of -1. Although i would prefer a solution that works both in unix and windows, if it is not possible i am most interested in it working for windows.
int _tmain(int argc, _TCHAR* argv[])
{
list mylist;
int value;
cout<<"Give the numbers you want to insert to the list, press -1 to stop\n";
do
{
cin>>value;
mylist.insertf(value);
mylist.sort_list();
mylist.print();
}while(value!=-1);
}

Here are solution for Windows
First solution:
Esc will not be handled when user starts to type till pressing enter.
While idle Esc will be handled
#include <Windows.h>
#include <iostream>
#include <conio.h>
#include <vector>
int main(int argc, char **argv)
{
int value=0;
std::vector<int> mylist;
do
{
//check if any input.
if (_kbhit()){
//probable user started to type
//block to read till the user press Enter. If you want to handle Esc here .
//then you should manually do input reading . I will write that solution later
std::cin>>value;
//if success
if(std::cin.good()){
mylist.push_back(value);
}else{
//firstly, clear error flag
std::cin.clear();
//ignore
std::cin.ignore(10000,'\n');
}
//print list
std::cout<<"new list: { " ;
for(int i=0;i< mylist.size();i++){
std::cout<<mylist[i]<<'\t';
}
std::cout<<" }"<<std::endl;
}
//check if Esc Pressed
}while(GetAsyncKeyState(VK_ESCAPE)==0);
return 0;
}
Second Solution:
Esc will be handled always in another thread. Immediate exit can be undesirable on some cases
DWORD WINAPI CheckEscape( LPVOID lpParam ) {
while(GetAsyncKeyState(VK_ESCAPE)==0){
//sleep
Sleep(10);
}
exit(0);
}
int main(int argc, char **argv)
{
int value=0;
std::vector<int> mylist;
//create thread for handling ESC key
CreateThread( NULL, 0, CheckEscape,NULL , 0, NULL);
//loop infinitely
while(true)
{
std::cin>>value;
//if success
if(std::cin.good()){
mylist.push_back(value);
}else{
//firstly, clear error flag
std::cin.clear();
//ignore
std::cin.ignore(10000,'\n');
}
//print list
std::cout<<"new list: { " ;
for(int i=0;i< mylist.size();i++){
std::cout<<mylist[i]<<'\t';
}
std::cout<<" }"<<std::endl;
}
return 0;
}
Third Solution and the Best one .Do everything manually
Handling keypress manually.
Exit will be called when Esc is Pressed. You can change it to handle more right way
bool keypress( char &key){
INPUT_RECORD IR[1];
DWORD read;
static HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
while(PeekConsoleInputA(h,IR,1,&read)){
if(read!=0){
//check if it was Key Event
if(IR[0].EventType==KEY_EVENT){
key=IR[0].Event.KeyEvent.uChar.AsciiChar;
ReadConsoleInputA(h,IR,1,&read);
FlushConsoleInputBuffer(h);
return true;
}
if(ReadConsoleInputA(h,IR,1,&read)){
continue;
}
}else{
return false;
}
}
}
//getnumber
int cinnumb( ){
char buffer[32];
buffer[0]='\0';
int count=0;
DWORD key=-1;
while(true){
Sleep(100);
do{
//here I make it nonblockin keypress
//but actually we do not need it
//we can use blocking ReadConsoleInputA(h,IR,1,&read);
//this way we not even need sleep() and
//our keypress function will be simple
//anyway im posting nonblocking one
//nonblocking keypress
char key=0;
bool isOk=keypress(key );
if(!isOk ){
Sleep(20);
continue;
}
if(key>='0' && key<='9'){
buffer[count]=key;
std::cout<<key;
++count;
if( count==31)break;
}
// check Enter key and enough symbol
if( key==13 && count>0 ){
std::cout<<std::endl;
break;
}
//for windows
//check if Esc pressed
if(key==27) exit(0);
}while(true);
buffer[count]='\0';
int value=atoi(buffer);
return value;
}
}
int main(int argc, _TCHAR* argv[])
{
std::vector<int> mylist;
int value;
char buffer[100];
//infinite loop
while(true)
{
//get number
value=cinnumb();
mylist.push_back(value);
//print list
std::cout<<"new list: { " ;
for(int i=0;i< mylist.size();i++){
std::cout<<mylist[i]<<'\t';
}
std::cout<<" }"<<std::endl;
//sleep a little
Sleep(10);
} ;
return 0;
}

Related

Myshell Segmentation Fault, Possible issue with getting input?

I have been working on this project for a while. The purpose is to make a functioning shell that can do pretty much all the shell commands (except cd). It does almost everything I want it to do, except for a couple things. The first is that when I put an '&' to signify background processing, it does it, but then doesn't print another myshell> line. I can still input something, but the myshell> never shows up, no matter where I put another cout<<"myshell> ";.
Another issue is if I press enter, making myString empty, many times, it crashes the program with a seg fault. Also after I do the '&' background processing and press enter to get the myshell> to come back up, it prints one myshell> but then seg faults on the next hit of enter. I'm sorry if I didn't explain this well, but it is really driving me crazy. Please let me know if you have any suggestions.
#include <stdio.h>
#include <iostream>
#include <unistd.h>
#include <cstdlib>
#include <cstring>
#include <sys/types.h>
#include <cstdio>
#include <sys/wait.h>
#include <stdio.h>
/*Function that parses the command the user inputs.
It takes myArgv and myString as inputs.
It returns the value of exitcond, which is used to see if the user wants to exit or not.
Also, this is where myString is tokenized using strok()*/
int parseCommand(char *myArgv[10], char myString[255])
{
int exitcond=0;
if((strcmp(myArgv[0], "exit") == 0)||(strcmp(myArgv[0], "quit")==0))
{
exitcond = 1;
return exitcond;
}
int i;
char *token;
token = strtok(myString," ");
i=0;
while (token != NULL)
{
myArgv[i] = token;
token = strtok(NULL," ");
i++;
}
/*
* Set the last entry our new argv to a null char
* (see man execvp to understand why).
*/
myArgv[i] = '\0';
return exitcond;
}
/*Function that gets the command from the user and sees if they want
background processing or not (presence of '&').
It takes inputs of choose and myString. choose is the variable for
whether background processing is necessary or not, while myString is
an empty character array.
It outputs the value of the choose variable for lter use.*/
int getCommand(int choose, char myString[255])
{
int i;
choose=0;
fgets(myString, 256, stdin);
if (myString[0]=='\0')
{
choose=0;
return choose;
}
for (i=0; myString[i]; i++)
{
if (myString[i]== '&')
{
choose=1;
myString[i]=' ';
}
if (myString[i] == '\n')
{
myString[i] = '\0';
}
}
return choose;
}
/*Main function where all the calling of other functions and processes
is done. This is where the user enters and exits the shell also. All
usage of fork, pid, waitpid and execvp is done here.*/
int main()
{
using namespace std;
int exitCondition=0, i=0, status;
char myString[255];
char *token, *myArgv[10];
pid_t pid, waiting;
int bg=0;
while (!exitCondition)
{
/* print a prompt and allow the user to enter a stream of characters */
cout << "myshell> ";
bg=0;
int choose=0;
bg=getCommand(choose,myString);
exitCondition=parseCommand(myArgv,myString);
if(exitCondition==1)
{
cout<<"Thank you for using my shell.\n";
}
else {
/* while (myString[0]=='\0')
{
cout<<"myshell> ";
bg=getCommand(choose,myString);
}*/
/* The user has a command, so spawn it in a child process */
pid = fork();
if (pid == -1)
{
/* to understand why this is here, see man 2 fork */
cout << "A problem arose, the shell failed to spawn a child process" << endl;
return(1);
}
else if (pid == 0)
{
// Child process
execvp(myArgv[0],myArgv);
cout << "Bad command or file name, please try again!\n" << endl;
return 0;
} else {
/* This makes sure that the spawned process is run in the foreground,
because the user did not choose background */
if(bg==0)
{
waitpid(pid,NULL,0);
}
}
}
}
return 0;
}
Okay, you had three bugs, one of which caused the segfault. Of the others, one would put a garbage argument in the array passed to execvp and the other would leak zombie processes for background jobs.
I've corrected the code and annotated it with where the bugs were along with the fixes [please pardon the gratuitous style cleanup]:
#include <cstdio>
#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define AVCOUNT 100
#define STRBUFLEN 2000
/*Function that parses the command the user inputs.
It takes myArgv and myString as inputs.
It returns the value of exitcond, which is used to see if the user wants to
exit or not.
Also, this is where myString is tokenized using strok()*/
int
parseCommand(char **myArgv, char *myString)
{
char *token;
char *bp;
int exitcond = 0;
int i;
// NOTE/BUG: original check for exit/quit was here -- at this point
// myArgv is undefined (hence the segfault)
// NOTE/BUG: your original loop -- at the end i was one beyond where it
// should have been so that when myArgv gets passed to execvp it would
// have an undefined value at the end
#if 0
token = strtok(myString, " ");
i = 0;
while (token != NULL) {
myArgv[i] = token;
token = strtok(NULL, " ");
i++;
}
#endif
// NOTE/BUGFIX: here is the corrected loop
i = 0;
bp = myString;
while (1) {
token = strtok(bp, " ");
bp = NULL;
if (token == NULL)
break;
myArgv[i++] = token;
}
/*
* Set the last entry our new argv to a null pointer
* (see man execvp to understand why).
*/
// NOTE/BUG: with your code, i was one too high here
myArgv[i] = NULL;
// NOTE/BUGFIX: moved exit/quit check to here now that myArgv is valid
token = myArgv[0];
if (token != NULL) {
if ((strcmp(token, "exit") == 0) || (strcmp(token, "quit") == 0))
exitcond = 1;
}
return exitcond;
}
/*Function that gets the command from the user and sees if they want
background processing or not (presence of '&').
It takes inputs of choose and myString. choose is the variable for
whether background processing is necessary or not, while myString is
an empty character array.
It outputs the value of the choose variable for lter use.*/
int
getCommand(int choose, char *myString)
{
int i;
choose = 0;
fgets(myString, STRBUFLEN, stdin);
if (myString[0] == '\0') {
choose = 0;
return choose;
}
for (i = 0; myString[i]; i++) {
if (myString[i] == '&') {
choose = 1;
myString[i] = ' ';
}
if (myString[i] == '\n') {
myString[i] = '\0';
break;
}
}
return choose;
}
/*Main function where all the calling of other functions and processes
is done. This is where the user enters and exits the shell also. All
usage of fork, pid, waitpid and execvp is done here.*/
int
main()
{
using namespace std;
int exitCondition = 0;
int status;
char myString[STRBUFLEN];
char *myArgv[AVCOUNT];
pid_t pid;
int bg = 0;
while (!exitCondition) {
// NOTE/BUGFIX: without this, any background process that completed
// would become a zombie because it was never waited for [again]
// reap any finished background jobs
while (1) {
pid = waitpid(0,&status,WNOHANG);
if (pid < 0)
break;
}
/* print a prompt and allow the user to enter a stream of characters */
cout << "myshell> ";
bg = 0;
int choose = 0;
bg = getCommand(choose, myString);
exitCondition = parseCommand(myArgv, myString);
if (exitCondition == 1) {
cout << "Thank you for using my shell.\n";
break;
}
/* while (myString[0]=='\0') { cout<<"myshell> "; bg=getCommand(choose,myString); } */
/* The user has a command, so spawn it in a child process */
pid = fork();
if (pid == -1) {
/* to understand why this is here, see man 2 fork */
cout << "A problem arose, the shell failed to spawn a child process" << endl;
return 1;
}
if (pid == 0) {
// Child process
execvp(myArgv[0], myArgv);
cout << "Bad command or file name, please try again!\n" << endl;
return 1;
}
/* This makes sure that the spawned process is run in the
foreground, because the user did not choose background */
if (bg == 0)
waitpid(pid, &status, 0);
}
return 0;
}

How to open new console window to print output of second thread there?

I am using c++ 11 on windows platform, and i wanted to know how to open a new console window to display output from a child thread.
mutex mu;
bool flag = false;
void printg()
{
int j=printf("testing /");
int count=0;
for(int i=0;true;)
{
sleep(1.0);
printf("\b");
printf("\\");
fflush(stdout);
sleep(1.0);
printf("\b");
printf("/");
fflush(stdout);
count++;
lock_guard<mutex> gaurd(mu);
if(flag)
{
return;
}
}
}
int main(int argc, char** argv)
{
thread t(printg);
pid_t p ;
sleep(5);
mu.lock();
flag = true;
mu.unlock();
t.join();
cout<<endl<<"hello "<<endl;
return 0;
}
I want the output of thread t on a different window and/or console, separate from the parent thread. In the long run i want to detach the thread and make it powerful enough to display output on its own window. Is there a work around this. I am working on windows 7 machine.

program stuck when reading file on.get function

I'm creating a program on win32, the program stops responding on .get function, I don't know what to do else, I think the problem is with std::ifstream inFile; but I need it outside of the main program, so I can use it in everywhere, for example here I use inFile in WM_CREATE and also in WM_KEYDOWN and close the file when the window closes. is it posible to do?
char Map[10][10], c[256];
std::ifstream inFile;
//windProc
case WM_CREATE: {
inFile.open ("Map.txt");
if (!inFile) {
::MessageBox(hwnd, "Failed to open file", "Error", MB_OK);
}
int i,j;
do { //loop for skipping comments
Map[j][i] = inFile.get(); //<---stuck here
if (Map[j][i] ==';') inFile.getline(c, 256);
} while (Map[j][i] ==';');
while (Map[j][i] !=';') { //loop for painting
switch (Map[j][i]) {
//
//paint based on char
//
}
i++;
Map[j][i] = inFile.get();
if (i==10) {j++; i=0;}
}
}
case WM_KEYDOWN: {
//...
inFile.getline(c, 256); // continue reading from file
int j=0; int i=0;
Map[j][i] = inFile.get();
while (Map[j][i] !=';') {
switch (Map[j][i]) {
//
//paint based on char
//
}
i++;
Map[j][i] = inFile.get();
if (i==10) {j++; i=0;}
}
break; }
case WM_DESTROY:
inFile.close();
PostQuitMessage (0);
break;
If the steam enters error/eof state in your while loop, Map[j][i] won't ever be set to ';' resulting in an endless loop:
do { //loop for skipping comments
Map[j][i] = inFile.get(); //<---stuck here
if (Map[j][i] ==';') inFile.getline(c, 256);
} while (inFile.good() && Map[j][i] ==';');
Your problem is that you're not initializing i and j. Try initializing them to 0. Also, you presumably want to update them in your loop so you don't continuously store the character in the same element.
Then you need to reinitialize i and j to 0 before the painting loop. However, painting is usually done in WM_PAINT (I'm not even sure it'll work in WM_CREATE.)

pthread executes after expected

I'm trying to compress a file consisting of 1's and 0's as part of an assignment. I have succeeded in doing this, however to get a feel for threads I'm trying to display a simple progress display using a pthread. The problem is that the thread executes AFTER the compression is complete. Here is my program:
void* compressShow(void *)
{
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
cout<<"Compressing";
while(1)
{
sleep(1);
cout<<".";
sleep(1);
cout<<".";
sleep(1);
cout<<".";
sleep(1);
cout<<".";
cout<<"\b\b\b\b";
}
}
void compression(char *buffer, ofstream &outFile)
{
//Some Compression code. Function executes each time a new line is lifted off the file.
}
int main(int argc, char *argv[])
{
if(argc < 3)
{
cout<<"You entered an insufficient number of command line arguments."<<endl;
}
else
{
ifstream inFile;
inFile.open(argv[1], ios::in);
ofstream outFile(argv[2]);
char buffer[100] = {NULL};
pthread_t thread;
pthread_attr_t attribute;
pthread_attr_init(&attribute);
pthread_attr_setdetachstate(&attribute, PTHREAD_CREATE_DETACHED);
pthread_create(&thread, &attribute, compressShow, (void *)5);
while(inFile.good())
{
` inFile.getline(buffer, 100, '\n');
compression(buffer, outFile);
}
pthread_cancel(thread);
//pthread_join(thread, NULL);
}
return 0;
}
Since I'm creating the thread BEFORE the while loop, I expect it to run concurrently with the loop that is doing the compression.
This has nothing to do with threads. See the same effect with
int main()
{
compressShow(0);
}
Try sending the flush manipulator from time to time.

Wait for input for a certain time

Is there any function that can wait for input until a certain time is reached? I'm making kind of Snake game.
My platform is Windows.
For terminal based games you should take a look at ncurses.
int ch;
nodelay(stdscr, TRUE);
for (;;) {
if ((ch = getch()) == ERR) {
/* user hasn't responded
...
*/
}
else {
/* user has pressed a key ch
...
*/
}
}
Edit:
See also Is ncurses available for windows?
I found a solution using kbhit() function of conio.h as follows :-
int waitSecond =10; /// number of second to wait for user input.
while(1)
{
if(kbhit())
{
char c=getch();
break;
}
sleep(1000); sleep for 1 sec ;
--waitSecond;
if(waitSecond==0) // wait complete.
break;
}
Try with bioskey(), this is an example for that:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <bios.h>
#include <ctype.h>
#define F1_Key 0x3b00
#define F2_Key 0x3c00
int handle_keyevents(){
int key = bioskey(0);
if (isalnum(key & 0xFF)){
printf("'%c' key pressed\n", key);
return 0;
}
switch(key){
case F1_Key:
printf("F1 Key Pressed");
break;
case F2_Key:
printf("F2 Key Pressed");
break;
default:
printf("%#02x\n", key);
break;
}
printf("\n");
return 0;
}
void main(){
int key;
printf("Press F10 key to Quit\n");
while(1){
key = bioskey(1);
if(key > 0){
if(handle_keyevents() < 0)
break;
}
}
}
Based on #birubisht answer I made a function which is a bit cleaner and uses NON-deprecated versions of kbhit() and getch() - ISO C++'s _kbhit() and _getch().
Function takes: number of seconds to wait for user input
Function returns: _ when user does not put any char, otherwise it returns the inputed char.
/**
* Gets: number of seconds to wait for user input
* Returns: '_' if there was no input, otherwise returns the char inputed
**/
char waitForCharInput( int seconds ){
char c = '_'; //default return
while( seconds != 0 ) {
if( _kbhit() ) { //if there is a key in keyboard buffer
c = _getch(); //get the char
break; //we got char! No need to wait anymore...
}
Sleep(1000); //one second sleep
--seconds; //countdown a second
}
return c;
}