Good evening everyone,
I’m writing a simple C++ loop that compares a sample number such as the number 3 within a digit ranging from 1 to 42.
Thanks in advance for your help.
Hi @massimo.grieco,
Is this different than just writing a for
loop and comparing values?
Perhaps some more detail might be helpful?
– Dale
Hi Dale, thank you for your reply and I was hoping for your help. Sorry but I’m new to this language and I can’t formulate the code. I would like the loop to return the corresponding digits which contain the number 3. I would be grateful for an example.
Hi @massimo.grieco,
Consider a function such as this:
/// <summary>
/// Returns true if a value contains a digit.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="digit">The digit.</param>
/// <returns>True if the value contains the digit.</returns>
static bool ContainsDigit(
unsigned int value,
unsigned int digit
)
{
while (value > 0)
{
if (value % 10 == digit)
return true;
value /= 10;
}
return false;
}
Here is a simple test command:
CRhinoCommand::result CCommandTestDigits::RunCommand(
const CRhinoCommandContext& context
)
{
ON_RandomNumberGenerator rnd;
rnd.Seed();
const int count = 100;
ON_SimpleArray<unsigned int> values(count);
for (int i = 0; i < count; i++)
values.Append(rnd.RandomUnsignedInteger(0, 1000));
const int digit = 3;
for (int i = 0; i < count; i++)
{
if (ContainsDigit(values[i], digit))
RhinoApp().Print(L"%u\n", values[i]);
}
return CRhinoCommand::success;
}
– Dale
Thanks a lot for the support, Dale.