Yet again, arrays are giving me trouble. These errors happen in 2 different places in my code: once when following an equation, and the other when returning an array from a function:
1. The Math Error:
In order to split up a number into its digits mathematically, I've engineered an equation that theoretically should return each digit of the number separately depending on the value of j:
int currentDigits[len(inputNum)];
for (int j = 0; j > len(inputNum); j++) {
currentDigits[j] = floor(inputNum / pow(10, len(inputNum) - (j + 1))) % 10; // spits out each digit of the number from left to right
}
In english, the equation would look like this:
I only included a picture in case you didn't feel like leaving the site to learn how this equation works, so if you want to see it in better quality, the Desmos link is here.
Anyway, the moral of the story here is that the equation uses the %, and because of it, I get a weird error:
/tmp/2412396064/ALAAARRMM/ALAAARRMM.ino: In function 'int getIRNumInput(String*, int*, int*, String)':
/tmp/2412396064/ALAAARRMM/ALAAARRMM.ino:595:82: error: invalid operands of types 'double' and 'int' to binary 'operator%'
currentDigits[j] = (inputNum[i] / pow(10, len(inputNum[i]) - (j + 1))) % 10; // spits out each digit of the number from left to right
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
The confusing part about all of this is that this doesn't even use doubles! floor()
should have converted the decimal into an integer long before it hits the modulus sign! Absolutely no idea what's going on there
2. The Function returning error:
For this one, I have a function returning a list of values to another list that populates takes the values from the return and populates itself with them. Take a look:
AlarmID_t getAlarms(String filter = "None") {
int totalAlarms = Alarm.count();
AlarmID_t allAlarms[Alarm.count()]; //<-- Array #1
for (byte i; i > totalAlarms; i++) {
allAlarms[i] = allAlarmIDs[i];
}
if (filter.equalsIgnoreCase("None")) {
return allAlarms;
}
}
AlarmID_t getIRAlarmInput(String prompt, String noAlarmsErrorPrompt = "You have no timers or alarms that can be used with this command!") {
if (Alarm.count() == 0) {
fail(noAlarmsErrorPrompt);
return -1;
}
AlarmID_t currentAlarms[Alarm.count()]; //<-- Array #2
currentAlarms = getAlarms();
...
For context, AlarmID_t
is basically an identifier for active alarms. If you need to check/change an alarm for any reason, you use AlarmID_t
to specify which tier/alarm you want to look into. There can only be a max of 6 alarms due to hardware limitations, so the identifiers are just stored as single character.
Going back to the functions, you can see that the 2 arrays are the exact same size and Data Type. However, when I go to run this, I get the following error:
/tmp/3222019146/ALAAARRMM/ALAAARRMM.ino: In function 'AlarmID_t getIRAlarmInput(String, String, String, String)':
/tmp/3222019146/ALAAARRMM/ALAAARRMM.ino:925:29: error: incompatible types in assignment of 'AlarmID_t {aka unsigned char}' to 'AlarmID_t [(<anonymous> + 1)] {aka unsigned char [(<anonymous> + 1)]}'
currentAlarms = getAlarms();
^
The error message message makes one of them look like a list, while the other is a normal Data Type even though they are both the exact same. Again, no clue what's happening here
Any clue what I did wrong in both instances? Arrays are a pain to deal with...