Need help: Solved

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
dudmaster
Posts: 171
Joined: 06 Oct 2010, 02:38
Location: Texas, Santa Fe
Contact:

Need help: Solved

Post 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!
2Labz.com, My Website
dudmaster
Posts: 171
Joined: 06 Oct 2010, 02:38
Location: Texas, Santa Fe
Contact:

Re: Need help: Solved

Post 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;
}
}
2Labz.com, My Website
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Need help: Solved

Post 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);
}
}
}
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
dudmaster
Posts: 171
Joined: 06 Oct 2010, 02:38
Location: Texas, Santa Fe
Contact:

Re: Need help: Solved

Post by dudmaster »

Thanks, muntoo- but a little late.

Text Edit program including file read here now!
2Labz.com, My Website
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests