Clearing a variable?

This is a discussion on Clearing a variable? within the C++ Programming forums, part of the General Programming Boards category; I've made a menu, using a switch(case) function, onto which the user should only input integers, but i've been trying ...

  1. #1
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168

    Clearing a variable?

    I've made a menu, using a switch(case) function, onto which the user should only input integers, but i've been trying to make a small bit of code to validate the input. When a letter is input it runs the 'default:' sequence and loops it.

    Do i need to 'flush' the variable that the switch(case) uses or what? I've seen that term used on here a few times before

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Actually, if you are inputting integers with iostream functions and you input a char, then your stream will go into a fail state. You will need to do 2 things:

    1. clear the failbit with cin.clear();
    2. ignore anything left in the stream with cin.ignore(x); where x is some number big enough to make sure you've ignored everything left in the stream.

    Many choose to avoid this by inputting a char and doing the validation on it something like:
    Code:
    char input;
    cin >> input;
    if (input <'0' || input >'9')...

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Do i need to 'flush' the variable that the switch(case) uses or what?
    No, that is nonsensical. A variable has a value, and you can set that value to anything you want. Sometimes it can be useful to reset a variable to 0, but you do that by assigning 0 to the variable.

    On the other hand, flush() is a method that is used with output. It is inefficient to write each character or number one at a time to a file, for instance. Instead, there is something like an outbox, and the outbox collects your output until it's full and then everything in the outbox gets written to the file all at once. That means you only write to the file once in awhile instead of after every character.

    However, if your output doesn't fill up the outbox, and you are done, you need a way to get everything sitting in the outbox to be written to the file. That's where flush() comes in. It most situations, you don't need to worry about flush() because it is called automatically for you.
    Last edited by 7stud; 05-09-2005 at 06:36 PM.

  4. #4
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    *slaps head* thanks guys, should have thought of using char sooner, and thanks for the explaination 7stud :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  2. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  3. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  4. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 03:11 AM
  5. clearing a variable
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 02-09-2002, 02:49 PM

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