-
Block Control Characters
I'm making a payroll application which takes input from cin, it validates the data (has a while loop in it). The problem is if I 'accidently' press Ctrl+Z it just keeps looping the prompt on the screen as I am locked out of the program. what canI do to block these special characters Ctr+Z and Ctrl+C and if there are others?
-
ctrl+z and ctrl+c are "EOF" some where in your program you are telling it to ignor them. In your test condition add a test for EOF to break your
loop.
-
how can i do that? the code im delaying with is :
Code:
do {
cout << "Enter data: ";
cin >> data}
while (!validData);
}
-
Code:
cout << "Enter data: ";
while(cin >> data) {
if(valid) {
} else break;
cout << "Enter data: ";
}
-
in 'ctype' header, there are functions like 'isalnum()', 'isdigit', etc which you can use to check for valid input or create one your self using the ascii chart...
-
we are suppose to use a do while loop because we are only suppose to prompt once! Our lecturer said prompting for the samething more than once was 'inefficient', so I can't use
Code:
cout << "Enter data: ";
while(cin >> data) {
if(valid) {
} else break;
cout << "Enter data: ";
}
would that even stop the control characters? i don't quite see how...
-
ctrl-z is the end of file marker for terminal input
ctrl-c is the interrupt signal, which kills your program unless you handle or ignore it
Code:
#include <iostream>
#include <string>
#include <signal.h>
using namespace std;
volatile bool sigCaught = false;
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_CRT_signal.asp
void catcher ( int signal ) {
sigCaught = true;
}
int main ( ) {
bool done = false;
/* catch ctrl-C, test sigCaught below if you want to know about it */
// signal ( SIGINT, catcher );
/* ignore ctrl-C */
signal ( SIGINT, SIG_IGN );
while ( !done ) {
char input[100];
while ( !done &&
cin.getline(input,sizeof input) ) {
string sInput = input;
cout << sInput << endl;
if ( sInput == "exit" ) {
done = true;
}
}
if ( cin.eof() ) {
cin.clear();
cout << "EOF detected, ignored" << endl;
}
}
return 0;
}
-
ok i figured out the cin.eof by myself. thanks a lot for the signal part!