Thread: clearing invalid value from memory

  1. #1
    Registered User
    Join Date
    Mar 2002

    Smile clearing invalid value from memory


    I'm having trouble with a validation bit of code.
    If the user enters an invalid character, I want to clear out that value from memory so the user can enter a valid character...

    char chOTyesno;
    ((chOTyesno != 'y') || (chOTyesno != 'Y') ||
    (chOTyesno != 'n') || (chOTyesno != 'N'))
       // this is where the statement would go, right?
      // how do I clear chOTyesno so that it is ready 
     // for a new value? 
      cout << "Did you work overtime? (y/n)\n";
      chOTyesno = getch();
    // thanks.

  2. #2
    Join Date
    Sep 2003
    There's no need to do an explicit clear unless you're leaving the data in stdin. Otherwise you can just overwrite whatever is in the variable:
    char yn = 0;
    while (true) {
      cout<<"Wazzup?: "<<flush;
      yn = getch();
      if (yn != 'y' && yn != 'Y' && yn != 'n' && yn != 'N')
      while (getch() != '\r')
    Or something like that.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    the way that piece of code is tricky because he relies on the input buffer not being flushed... (getch() hangs the character in the buffer for the next statement to catch it)

    that can be tricky...

    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003


    //try this
      cout << "Did you work overtime? (y/n)\n";
      chOTyesno = getch();
      //input code here.
    while ((chOTyesno != 'y') || (chOTyesno != 'Y'))
    But why would you want to test the loop exit?
    I would recomend you only let the fact of truth do the testing required. This will only allow an exit if y or Y is entered. All others are ignored, but if only want the buffer to be clean before your entry then use the flushall();.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. Help with insert/delete binary search tree
    By Nazgulled in forum C Programming
    Replies: 39
    Last Post: 03-25-2009, 04:24 PM
  3. Memory Leak Help
    By (TNT) in forum Windows Programming
    Replies: 3
    Last Post: 06-19-2006, 11:22 AM
  4. Tidying up memory
    By RobJ in forum C Programming
    Replies: 6
    Last Post: 04-15-2006, 07:36 AM
  5. Accessing Video Memory Information...need help
    By KneeLess in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2003, 03:53 PM