is there anyway i can write ths code in a shorter way?

.....
Last edited on
static const char *const zip_symbol_array[] = { ":::||", "::|:|", "::||:", ... };

Now instead of the switch you can simply write:

1
2
3
FinalResult += zip_symbol_array[zip1];
FinalResult += zip_symbol_array[zip2];
...
He wrote: "without loops or arrays".

Functions are allowed. Perhaps?
1
2
3
4
5
std::string digit2word( int digit ) {
  std::string result;
  // the switch (digit)
  return result;
}

Now instead of the switch you can simply write:
1
2
3
FinalResult += digit2word(zip1);
FinalResult += digit2word(zip2);
...


He wrote: "without loops or arrays".

That seems like a rather arbitrary restriction given that the OP already uses arrays in the form of strings and string literals.

If one wanted to avoid explicitly using an array, but finds the use of std::string acceptable, and since the "zip symbols" are all the same length, one could just cram them all into a string and calculate the offset of a substring. That would be considerably shorter than using the switch statement.
It is the miracle of encapsulation.


PS. I see no input validation. No enforcement that the "ZIP numbers" asked from the user are actually single digits. Always assume that the user ignores instructions.
Topic archived. No new replies allowed.