clearing invalid value from memory

This is a discussion on clearing invalid value from memory within the C++ Programming forums, part of the General Programming Boards category; Hello. I'm having trouble with a validation bit of code. If the user enters an invalid character, I want to ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    2

    Smile clearing invalid value from memory

    Hello.

    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...



    Code:
    char chOTyesno;
    
      while
    ((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
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    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:
    Code:
    char yn = 0;
    
    while (true) {
      cout<<"Wazzup?: "<<flush;
      yn = getch();
      if (yn != 'y' && yn != 'Y' && yn != 'n' && yn != 'N')
        break;
      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
    Posts
    533
    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...

    -LC
    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
    Posts
    589

    flushall();

    Code:
     
    //try this
    
    do
    {
      flushall();
      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, 12:41 PM
  2. Help with insert/delete binary search tree
    By Nazgulled in forum C Programming
    Replies: 39
    Last Post: 03-25-2009, 05:24 PM
  3. Memory Leak Help
    By (TNT) in forum Windows Programming
    Replies: 3
    Last Post: 06-19-2006, 12:22 PM
  4. Tidying up memory
    By RobJ in forum C Programming
    Replies: 6
    Last Post: 04-15-2006, 08:36 AM
  5. Accessing Video Memory Information...need help
    By KneeLess in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2003, 04:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21