Value stored in array returning rubbish

Hi All, long time reader, first time poster here.

As a background, I have plenty of experience in JAVA, VBA and SQL. I haven’t coded in C or C++ for about 8 years when I was doing my computer science univeristy degree.

I have a problem (I think) printing the value stored in an integer array to the serial console.

I assume the correct way to print the value stored in each array index is:

//note array declaration is   int arrayname[array_length]

for(int i=0;i<array_index;i++)
{
    Serial.println(intarray[i]);
}

However, I seem to be getting rubbish on the serial console, almost like an overflow of some other variable.

Here is the code - very much a work in progress so excuse the mess…

#include <MAX31855.h>
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <SD.h>
#include <EEPROM.h>         //Include the EEPROM library to score the highscore
#include <MCUFRIEND_kbv.h>
#include <Wire.h>
#include "RTClib.h"
#include <max6675.h>
#include <Adafruit_NeoPixel.h>
//#include <EEPROM.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

........

int egt[8];

........


void setup()
{
  //initalise EGT array
  for(int m;m<8;m++)
  {
    egt[m] = 0;
  }
  
  ...........

void loop()
{
  
  ........

  if (currentpage == 3)  //EGT Monitor
  {
    
          gettemps();
          cleantemps();
          for(int egti=0;egti<8;egti++)
          {
            //sprintf(mesg, "%d", tempC);
            Serial.print("EGT" + (egti+1));
            Serial.println(": " + egt[egti]);
          }
       }
    }


void gettemps()
{  
  // Display the junction temperature
  float temperature; 
  // Display the temperatures of the 8 thermocouples
    // Select the thermocouple
  for (int therm=0; therm<8; therm++)
  {
    // Select the thermocouple 
    digitalWrite(T0, therm & 1? HIGH: LOW);
    digitalWrite(T1, therm & 2? HIGH: LOW);
    digitalWrite(T2, therm & 4? HIGH: LOW);
    // The MAX31855 takes 100ms to sample the thermocouple.
    // Wait a bit longer to be safe.  We'll wait 0.125 seconds
    delay(125);
    temperature = temp.readThermocouple(CELSIUS);
    if (temperature == FAULT_OPEN)
        continue;
    Serial.print("T");
    Serial.print(therm);
    Serial.print("=");
    printTemperature(temperature);
    Serial.println();
    //delay(1000);
    //egt[therm] = convTemperature(temperature,therm);  
    convTemperature(temperature,therm);
  }
}

void printTemperature(double temperature) 
{
  switch ((int) temperature)
  {
    case FAULT_OPEN:
      Serial.print("FAULT_OPEN");
      break;
    case FAULT_SHORT_GND:
      Serial.print("FAULT_SHORT_GND");
      break;
    case FAULT_SHORT_VCC:
      Serial.print("FAULT_SHORT_VCC");
      break;
    case NO_MAX31855:
      Serial.print("NO_MAX31855");
      break;
    default:
      Serial.print(temperature);
      break;
  }
  Serial.print(" ");
}

void convTemperature(double temperature,int arraddr) 
{
  int temp;
  switch ((int) temperature) 
  {
    case FAULT_OPEN:
      temp = 0;
      Serial.print("FAULT_OPEN");
      egt[arraddr] = temp;
      break;
    case FAULT_SHORT_GND:
      temp = 0;
      Serial.print("FAULT_SHORT_GND");
      egt[arraddr] = temp;
      break;
    case FAULT_SHORT_VCC:
      temp = 0;
      Serial.print("FAULT_SHORT_VCC");
      egt[arraddr] = temp;;
      break;
    case NO_MAX31855:
      temp = 0;
      Serial.print("NO_MAX31855");
      egt[arraddr] = temp;
      break;
    default:
      temp = ((int) temperature); 
      //Serial.print(temperature);
      egt[arraddr] = temp;;
      break;
  }
  Serial.print(" ");
}

void cleantemps()
{
  for (int m=0;m<8;m++)
  {
    if (egt[m]==NAN)
    {
      egt[m] = 0;
    }
  }
}

Below is an example of the console output rubbish verbatim:

GT:
T:
:
: Logger
:
:
Cripple Customs:
ripple Customs:

The output I expect to see given the code above would be:

EGT1: 0

EGT2: 0

EGT3: 23 (as an example, some non-integer value as this is the only thermocouple connected presently)

EGT4: 0

EGT5: 0

EGT6: 0

EGT7: 0

EGT8: 0

I have been searching for the last 3 hours on this issue across a number of forums, with no joy.

Is there some special way of working with arrays and serial output to the console or do I have to format in some way? If so, what is the approach and/or an example?

Many thanks in advance.

  for(int m;m<8;m++)
  {
    egt[m] = 0;
  }

m is un-initialized.

I think in Arduino C++ in Serial.println() you don't use "+" plus to concatenate values.

@arduino new...

Thanks for the heads up, although I am calling cleantemps() which will run over the entire array too (making the initialisation code practically redundant - I put that intialisation code in there as part of my error fixing efforts.

In any case, I have rectified the loop and the same problem persists

@ABernal

Spot on!!!

I really feel like an idiot now.....Too much time buried in coding without a break here.

Thanks mate