How to 'Clear' a variable?

This is a discussion on How to 'Clear' a variable? within the C++ Programming forums, part of the General Programming Boards category; Hey guys, is there any way you can clear what a variable holds? For E.G, if I was to write ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Tamworth, NSW, Australia
    Posts
    16

    How to 'Clear' a variable?

    Hey guys, is there any way you can clear what a variable holds?

    For E.G, if I was to write this:

    Code:
    int x;
    x = 10;
    cout << x << endl;
    What code would go after it so that I can clear the integer 'x' and assign it to another number, such as a user input? I'm hoping to do something like this:

    Code:
    int pin;
    int pin_attempt;
    pin = 2011;
       cout << "Enter Pin: ";
       cin.get >> pin_attempt;
       cin.ignore();
    
    if ( pin_attempt == pin )
    {
      // Don't really have to bore you with this code.. 
    }
    else
    {
      cout << "Incorrect Pin. Please reset: ";
      // Code to clear the int 'pin' needs to go here?
      cin.get >> pin;
      cin.ignore();
    }
    I've actually got a gut feeling I've made a fool of myself in the matter that it's probably as easy as just telling the compiler to overwrite it with another user input.. but hey, no point not getting some professional advice

  2. #2
    Registered User
    Join Date
    Sep 2011
    Location
    Tamworth, NSW, Australia
    Posts
    16
    Line 16 is not coded correctly, just realized :P

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,825
    I suppose you could just say
    pin_attempt = pin = -1;
    when you're done.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    There is no way to "clear" a variable. You can just overwrite it. If the PIN is wrong, input a new PIN, overwriting the old one.
    Don't want the PIN to stay in memory after you've entered correct pin? Set it to -1 or something to overwrite the correct PIN with a wrong one.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Location
    Tamworth, NSW, Australia
    Posts
    16
    Great, thanks alot!
    I have thought of another question.. heh sorry! :P (I also thought it would be best to re-comment on this seeming as I'm sure it's getting annoying me posting so much!)
    is there a way to have a variable accept only certain inputs?

    Code:
    string mystring;
    cout << "Are you sure? (Y/N)" << endl;
    cin >> mystring;
    cin.ignore();
    As an example to what I am talking about, how would you make mystring only accept 'Y' or 'N'? I have a feeling it might have something to do with cin.ignore.. can you ignore everything besides certain chars? because I know you can ignore certain characters, surely there is a way to turn that around and have it only accept certain chars?

    Thoughts? Thanks!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    There is no built-in function to do that. But you can it manually.
    It is simply to validate the input. For example, in your case, check if mystring is "Y" or "N", and if not, ask again until the user gets it right.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Due to the nature of standard input, your program will always recieve a line at a time. So there is no way to read a single character. You can read in a line, then only consider the first character, but the OS will buffer a whole line for you. You can ask the user to type "Y+enter", and then prompt again if the answer is not what you expect, but the screen will display every thing they type.

    cin>>mystring will read in the first word in the line. So if you compare that to =="Y" you will know that the user typed something that starts with the word "Y". To read the whole line, use getline().
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 04-16-2011, 01:28 PM
  2. Replies: 8
    Last Post: 02-14-2010, 04:14 PM
  3. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 04:35 AM
  4. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 04:31 PM
  5. Variable Names based on Variable Values
    By mrpickle in forum C++ Programming
    Replies: 6
    Last Post: 01-27-2003, 10:33 AM

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