ID Number Generator

Viewed 424

I keep getting an "int cannot convert to bool" error at if(number % NUMINROW), and I do not know how to fix it. I am fairly new to C#, but I know Python and how for loops work, but I am still confused.

// Program displays every possible ID number for a company
// ID number is a letter, followed by a two-digit number
// For example -- A00 or Z99
// Go to a new display line after every 20 IDs

using static System.Console;
class DebugFive3
{
   static void Main()
   {
      char letter;
      int number;
      const int LOW = 0;
      const int HIGH = 99;
      const int NUMINROW = 20;
      for(letter = 'A'; letter <= 'Z'; ++letter)
        for(number = LOW; number >= HIGH; ++number)
        {
         if(number % NUMINROW)
             WriteLine();
           Write("{0}{1} ", letter, number.ToString("D2"));
        }
   }
}
2 Answers

The reason you're getting that compile error is that number % NUMINROW returns the remainder after dividing number by NUMINROW, which is an int, whereas an if condition must return a bool (and there is no implicit conversion from int to bool in C#).

One way to fix this is to modify the condition so that it compares the result of the modulus operation to another int, for example:

if(number % NUMINROW == 0)

Other Issues

  1. There is no Console.ReadLine() at the end of the Main method, so as soon as the data is displayed, the console window will dissapear. Adding this line will pause execution until the user presses the Enter key, giving them time to see the data.
  2. This condition is incorrect: number = LOW; number >= HIGH. It reads, "set number to LOW, then loop while number is greater than or equal to HIGH." That condition will never be true!! We should use <= instead.
  3. The current code is completely ignoring the 20th column. To solve this, we can remove the else clause and switch the order of the code lines so that we always write the value, and then conditionally add a new line.
  4. Assuming we want NUMINROW columns displayed, our modulus condition should compare the next number to this value, since we're starting at 0 (so the first row would be 0-19, followed by 20-39). Another reason to add one to the value before doing the comparison is that 0 modulus any number is 0, so we would have an extra new line before the first item (where number is 0).
  5. Not a problem, really, but since we don't use the variables letter or number outside the loops, it's generally better to declare them as part of the for loop itself, to reduce their scope.

Here's a sample of how to solve these issues:

static void Main(string[] args)
{
    const int LOW = 0;
    const int HIGH = 99;
    const int NUMINROW = 20;

    for (char letter = 'A'; letter <= 'Z'; letter++)
    {
        for (int number = LOW; number <= HIGH; number++)
        {
            Write("{0}{1} ", letter, number.ToString("D2"));
            if ((number + 1) % NUMINROW == 0) WriteLine();
        }
    }

    ReadLine(); // Wait for user input
}

Output enter image description here


As a side note, here's a fun little sample using System.Linq that does it all in one line (not recommended, since it's hard to read/debug, but thought it might be interesting):

const int LOW = 0;
const int HIGH = 99;
const int NUMINROW = 20;

WriteLine(string.Concat(Enumerable.Range('A', 26)
    .Select(chr => string.Concat(Enumerable.Range(LOW, HIGH + 1)
        .Select(num => $"{(char) chr}{num:D2}" + ((num + 1) % NUMINROW == 0
            ? Environment.NewLine : " "))))));

ReadLine();

You have error on second for and in condition if(number % NUMINROW)

static void Main(string[] args)
    {
        char letter;
        int number;
        const int LOW = 0;
        const int HIGH = 99;
        const int NUMINROW = 20;
        for (letter = 'A'; letter <= 'Z'; ++letter)
        {
            for (number = LOW; number <= HIGH; ++number)
            {
                if (number % NUMINROW == 0)
                    Console.WriteLine();
                else
                    Console.Write("{0}{1} ", letter, number.ToString("D2"));
            }
        }
        Console.ReadLine();
    }
Related