Page 1 of 1

Need help: Solved

Posted: 28 Oct 2010, 22:31
by dudmaster
Well i am trying to make a text input using 3 keys. L/R to select letter, enter to choose.

My code gives me a file error. Code 5.

Code: Select all

task main() {
string Alphabet[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
"v", "w", "x", "y", "z"};
string L1, L2, L3, L4, L5, L6, l7, L8, Sen, Sen2;   // Hax Ngn: Direct NeX Edit  :o (hide the answer!!!!!)
bool S;
int N;
while (S == false){
while (0 == ButtonCount(BTNCENTER,true)){
if (ButtonCount(BTNLEFT,true))
{
  N--;
}
if (ButtonCount(BTNRIGHT,true))
{
  N++;
}
Sen2 = Alphabet[N];
TextOut(0, LCD_LINE3, Sen, true);
TextOut(0, LCD_LINE5, Sen2);
}
StrCat(Sen, Sen2, Sen);
}
}
Not telling what it's for... Hey! cover it up!

Re: Need help: Solved

Posted: 28 Oct 2010, 22:53
by dudmaster
I solved it using this code:

Code: Select all

task main() {
string Alphabet[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
"v", "w", "x", "y", "z"};
string Sen = "", Sen2 = "";   // Hax Ngn: Direct NeX Edit
bool S = false;
int N = 0;
while (S == false){
while (0 == ButtonCount(BTNCENTER,true)){
if (ButtonCount(BTNLEFT,true) && N > 0)
{
  N--;
}
if (ButtonCount(BTNRIGHT,true) && N < 26)
{
  N++;
}
Sen2 = Alphabet[N];
TextOut(0, LCD_LINE3, Sen, true);
TextOut(0, LCD_LINE5, Sen2);
Wait(20);
}
Sen = Sen + Sen2;
}
}

Re: Need help: Solved

Posted: 29 Oct 2010, 00:47
by muntoo
THIS is why you should almost always initialize variables as soon as you declare them. Especially pointers (to NULL).

Whenever you need an alphabet letter, instead of using string Alphabet[] why don't you do this:

Code: Select all

string szLetter(int idx)
{
    idx += 'a';
    return(FlattenVar(idx));
}
Now, replace "Alphabet[N]" with "szLetter(N)"

Also, string L1, L2, L3... could be turned into

Code: Select all

string L[];
ArrayInit(L, "", 8);
Also, you can improve your code like this:

Code: Select all

string szLetter(int idx)
{
    idx += 'a';
    return(FlattenVar(idx));
}

task main() {
string L[];
ArrayInit(L, "", 8);
string Sen, Sen2;   // Hax Ngn: Direct NeX Edit  :o (hide the answer!!!!!)
int N;
while(1)
{
if(/*S==0*/)
    break;

while(!(ButtonPressed(BTNCENTER, 0)))
{
    if(ButtonPressed(BTNLEFT, 0))
    {
        N--;
    }
    if(ButtonPressed(BTNRIGHT, 0))
    {
        N++;
    }

    Sen2 = szLetter(N);
    TextOut(0, LCD_LINE3, Sen, true);
    TextOut(0, LCD_LINE5, Sen2);
}
StrCat(Sen, Sen2, Sen);
}
}
}

Re: Need help: Solved

Posted: 29 Oct 2010, 11:28
by dudmaster
Thanks, muntoo- but a little late.

Text Edit program including file read here now!