-2
char myChar; 

To be clear the above is what I mean by an "empty" value.

while(conditions)
{
userInput
if(userInput) 
  {do stuff}

assign userInput empty value  *where I need help
}

How do I reset my char value to nothing to prevent the if statement from running after looping with previous userInput? I read that you can't assign a pointer value (null) to char, what would I then do?

2 Answers2

1

You could set userInput to e.g. NUL character like this:

while (conditions)
{
    char userInput = ...;
    if ('\0' != userInput)
    {
        // Do stuff.
    }
    else
    {
        break;
    }

    userInput = '\0';
}

However, the method you use to read the input affects what is the most sensible thing to do. The user could input e.g. the newline character to indicate that they don't want to continue.

tsnorri
  • 1,966
  • 5
  • 21
  • 29
  • +1, but the key here (IMO) isn't the `'\0'`—it's the `break;`. Just stop when you're done, and you don't have to worry about "previous input" because each iteration of the loop assigns a new value to `userInput`. – wchargin Jan 19 '15 at 19:57
0

When you define a new variable its value isn't "empty" but unitialized. Trying to read an uninitialized variable is undefined behaviour and will not give you the result you desire.

If you want to have it in a certain state, you need to assign a value that you know you will not get in any other way, the best value for this would be NULL, but you may choose any value that you know the variable will not be assigned. If you can't guarantee any such value, you will have to use another variable (most fitting is bool). Best to use them together in a struct

char userInput = NULL;

//...

while (condition)
{
    userInput = GetInput();
    if (userInput) 
    {
        //do stuff
    }

    userInput = NULL;
}

Using a struct and an extra status variable:

struct UserInput {   // Declare UserInput struct type
    bool initiated;
    char c;
} userInput;         // create a variable userInput of type UserInput

userInput.initiated = false;

//...

while (condition)
{
    userInput = GetUserInput(); //GetUserInput also changes the value of initiated to true
    if (userInput.initiated) 
    {
        //do stuff
    }

    userInput.initiated = false;
}
Community
  • 1
  • 1
SIMEL
  • 8,745
  • 28
  • 84
  • 130
  • Except, of course, that the legal range of `char` varies. It's at least 255 distinct values, that cover at least 0-127 inclusive, but more than that is up to the implementation. – Ben Voigt Jan 19 '15 at 20:07
  • @BenVoigt, my bad with the 256, obviously I meant that the values are interpreted as unsigned ints – SIMEL Jan 19 '15 at 20:12
  • It's not only about wanting a particular state. If you're going to read from the variable, it needs to be initialized. Reading from an uninitialized variable is undefined behaviour. – juanchopanza Jan 19 '15 at 20:15
  • `NULL` would be a rather confusing value. Historically, it's been `0` (integer zero) but used for null _pointers_. Nowadays, it can expand to `nullptr` which really makes it clear it's intended as a pointer. Just use `\0`, which is the character literal zero. – MSalters Jan 19 '15 at 20:26