Why do I get 789 at the end of this char array?

So this is my code:

#include <LiquidCrystal_I2C.h>
#include <wire.h>
#include "Adafruit_MCP23008.h"
#include <cckeypad.h>
#include <randExec.h>


bool press;                                                                     //bool for detecting buttons on Keypad
String AnswerForEq;                                                             // String that stores the correct Answers
char c[7] = {"       "};
char answ[6] = {"      "};                                                      // Char Array that stores user input (answer)
char num[4][4] = { { '7', '8', '9', 'A' },
                  { '4', '5', '6', 'B' },
                   { '1', '2', '3', 'C' },
                   { '.', '0', '-', 'D' } };               
char z = 0;
int length1, length2, pos;
unsigned long Tstart;
bool test, ans;
String q = "               ";

randExec randExec(A1);
cckeypad cckeypad(1, num);
LiquidCrystal_I2C lcd(0x27,20,4);



void setup() {
  Serial.begin(9600);
  Serial.println("START");
  pinMode(13, OUTPUT);
  lcd.init();
  lcd.backlight();
  lcd.clear();
  delay(100);
  cckeypad.Kbegin();
  for(int i; i<6; i++){
    answ[i]=' ';
  }
}

void loop() {
  getAns();
}

void clearAns(){
  for(int i; i<6; i++){
    answ[i]=' ';
  }
  clearA();
  pos=0;
}

void getKeypad(){
  cckeypad.Kcheck(&z);
  if(z!=' '){
    if(!press){
      if(z=='D'){
        pos=0;
        press = true;
        clearAns();
        clearA();
      }else if(z=='C'){
        ans=1;
      }else{
        answ[pos] = z;
        pos++;
        if(pos>=6){pos=0;}
        press = true;
        lcd.setCursor(0, 3);
        for(int i=0; i<6; i++){
          if(answ[i]!=' '){
            lcd.setCursor(i, 3);
            lcd.print(answ[i]);
          }
        }}
      delay(10);
    }
  }else{
    press = false;
  }
}

void Comp(){
  float answerFloat = AnswerForEq.toFloat();
  float answer2Float = String(answ).toFloat();
  Serial.println(answer2Float);
  if(answerFloat == answer2Float){
    test=true;
  }else{
    test=false;
  }
}

void ShowQuestion(){
  lcd.setCursor(0, 0);
  lcd.clear();
  q = randExec.gen();
  while(q.length()<=2){
    q = randExec.gen();
  }
  lcd.print(q);
  Serial.println(q);

}

void Rig(){
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("RICHTIG");
  delay(2000);
}

void Wro(){
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("FALSCH");
  delay(2000);

}

void clearA(){
  for(int i; i < 20; i++){
    lcd.setCursor(i, 3);
    lcd.print(" ");
  }
}

void getAns(){
  ans=0;
  ShowQuestion();
  AnswerForEq=randExec.res();
  Serial.println(AnswerForEq);
  Tstart=millis()+10000;
  while(ans==0){
    getKeypad();
    if(millis()>=Tstart){
      break;
    }
  }
  Comp();
  if(test){
    digitalWrite(13, HIGH);
    Rig();
  }else{
    digitalWrite(13, LOW);
    Wro();
  }
  clearAns();
}

the problem is that in the answ char array I get 789 at the end and I don't know why. The libraries are linked below:
randExec.zip (1.1 KB)
cckeypad.zip (1.3 KB)

What do you mean by 'I get 780 at the end'. Is this displayed on the lcd? Can you explain what your code is supposed to do?

Oops

At least three times

I mean it shows on serial and on lcd an 789 on the end of the char array. for example the char array is set to be 54321, the lcd and serial show 54321789

What do you mean?

You may find what she or he is getting at by looking here at the syntax of a for and compare to yours.

  for (int i; i < 20; i++)

What is the value of i at the start of this for loop ?

As @anon73444976 is hinting, what did you want the initial value of i to be in your for loop? The value may default to 0, or it may not. Best to make sure, so perhaps you meant:

for(int i=0; i<6; i++){

I'm on my phone and can't get to an editor, but does that fit?

Your char array that holds a string of 7 spaces needs an additional char to hold the null terminator from the end of the string. Same for you answ array.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.