Thread: Block Control Characters

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    104

    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?

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    23
    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.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    104
    how can i do that? the code im delaying with is :
    Code:
    do {
          cout << "Enter data: ";
          cin >> data}
    while (!validData);
    }

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Code:
    cout << "Enter data: ";
    while(cin >> data) {
       if(valid) {
       } else break;
        cout << "Enter data: ";
    }

  5. #5
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    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...

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    104
    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...
    Last edited by Hexxx; 02-11-2006 at 09:33 PM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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;
    }

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    104
    ok i figured out the cin.eof by myself. thanks a lot for the signal part!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with the control characters
    By vin_pll in forum C++ Programming
    Replies: 12
    Last Post: 10-22-2008, 07:28 AM
  2. (Multiline) Edit Control Limit
    By P4R4N01D in forum Windows Programming
    Replies: 9
    Last Post: 05-17-2008, 11:56 AM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  4. very weird .h problem
    By royuco77 in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 07:55 AM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM