Handle Ctrl+Z & Ctrl+C through Switch Case - C/C++
Description: This code simply is a switch case that handles user inputs & checks it against the switch case. If input isn't matched each of the cases, then it goes to default. The only thing is, my switch case can not handle Ctrl+Z & Ctrl+C.
If user inserts Ctrl+Z, my code ends up in an infinite loop even though i handle it, infinite loop looks as:
Enter a choice: Choice is invalid.
Enter a choice: Choice is invalid.
Enter a choice: Choice is invalid.
Enter a choice: Choice is invalid.
Enter a choice: Choice is invalid.
Enter a choice: Choice is invalid.
Enter a choice: Choice is invalid.
Enter a choice: Choice is invalid.
Enter a choice: Choice is invalid.
==========================================
Please look between where i put asterisks.
Question:
1. After user insert data, i do !cin.eof() to prevent Ctrl+Z, if user Ctrl+Z not inserted, then proceed normally & return userChoice. If Ctrl+Z is inserted, return -20(could be any number that is not among one of the cases). When -20 is returned, it will go to receiveUserInput. Thus receiveUserInput carries -20, the switch case should take -20 as a regular user input & checks it against the cases & will end up in default, and repeat the request to user without going to infinite loop! Can you tell me why i end up in infinite loop & how i can fix it?
2. I got some issues with Ctrl+C, i got help before but it did not work. Whenever i press Ctrl+C, i get the image that you see below! I know its not easy to handle Ctrl+C. If you have some good resources to learn from, please refer me to it.
3. Is it possible to handle Ctrl+C through try/catch/throw? Why?
4. Right now, i'm concerned about Ctrl+Z. Ctrl+C is not a big deal for now.
I'm looking forward to hearing from you. Any answer will be appreciated
Code:
#include <sstream>
#include "MenusLayout.h"
#include "ConsoleDesign.h"
const int EXIT = -1;
ConsoleDesign windowDes;
MenusLayout::MenusLayout()
{
//empty constructor
}
MenusLayout::~MenusLayout()
{
//empty destructor
}
void MenusLayout::mainMenu()
{
do
{
choice_is_invalid = false;
windowDes.coloringText(3);
cout<<"\nEnter a choice: ";
windowDes.coloringText(7);
//*********RETURNED***************//
int receiveUserInput = getUserChoice<int>();
//************************************//
switch(receiveUserInput)
{
case 1:
//do something
break;
case 2:
//do something
break;
case 3:
//do something
break;
case EXIT:
exitApplication();
break;
default:
windowDes.coloringText(6);
cout<<"Choice is invalid."<<endl;
choice_is_invalid = true;
break;
}
}while(choice_is_invalid);
}
template <typename genericDataType>
genericDataType MenusLayout::getUserChoice()
{
genericDataType userChoice;
string input;
getline(cin, input);
stringstream ss(input);
//**********************************//
if(!cin.eof())
{
ss >> userChoice;
return userChoice;
}else{
return -20;
}
//**********************************//
}
void MenusLayout::exitApplication()
{
do
{
choice_is_invalid = false;
windowDes.coloringText(12);
cout<<"\nAre you sure you want to exit - Y/N? ";
char exitConfirmation = getUserChoice<char>();
switch(exitConfirmation)
{
case 'y':
exit(0);
break;
case 'Y':
exit(0);
break;
case 'n':
mainMenu();
break;
case 'N':
mainMenu();
break;
default:
windowDes.coloringText(6);
cout<<"Choice is invalid - Please enter Y or N"<<endl;
choice_is_invalid = true;
break;
}
}while(choice_is_invalid);
}
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.