• C

URGENT!!! WHAT'S WRONG WITH THIS PROGRAM...

I'm doing a program to choose a letter from a list by moving the cursor and pressing enter. I don't know what's wrong because the letter where thecursor is, do change the color but once you mive to another letter the color remains. Please help. Thanks. (I'm a begginer : ) ).
This is what I got:

#include <stdio.h>
#include <conio.h>

void keyboard();
void lightOff();
void lightOn();

int x, y=4, j=0;
char *letters[20]={"A","B","C",...,"T"};

 main()
 {
  int i;
  clrscr();
  for(i=0; i<=20;i++)
     {
     gotoxy(40,4+i);
     cprintf("%s",letters[i];
     }
  keyboad();
 }
void keyboard()
{
 char key;
 while(key!='s')
   {
    key=getch();
    switch(key)
    {
     case'H': y--; lightoff(); j--; lighton();
     break;
     case'P':y++; lightoff(); j++; lighton();
     break;
    }
   }
}
void ligtoff()
{
 if(y>23)y=4;
 if(y<4)y=23;
 if(j>19)j=0;
 if(j<0)j=19;
 textbackground(13);
 gotoxy(39,y);
 cprintf("%s",letters[j]);
}
void lighton()
{
 if(y>23)y=4;
 if(y<4)y=23;
 if(j>19)j=0;
 if(j<0)j=19;
 textbackground(1);
 gotoxy(39,y);
 cprintf("%s",letters[j]);
}
gothic130Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bobbit31Commented:
first how did you get it to compile??

cprintf("%s",letters[i];

should be

cprintf("%s",letters[i]);

0
gothic130Author Commented:
Just a finger mistake!!!
0
VB-ExpertCommented:

I have amended errors within the code.  C is case sensitive and must be careful when using it.

C generally uses lowercase unless 'Define' is used, where the name defined may be uppercase.

#include <stdio.h>
#include <conio.h>

void keyboard();
void lightoff();   /* void lightOff();*/
void lightOn();

int x, y=4, j=0;
char *letters[20]={"A","B","C",...,"T"};

int main()            /*main() */
{
  int i;
  clrscr();
  for(i=0; i<=20;i++)
     {
     gotoxy(40,4+i);
     cprintf("%s",letters[i]);  /* cprintf("%s",letters[i]; */
     }
  keyboard();  /* keyboad */
  return 0;   /* New Addition */
 }

void keyboard()
{
 char key;
 while(key!='s')
   {
    key=getch();
    switch(key)
    {
     case'H': y--; lightoff(); j--; lighton();
     break;
     case'P':y++; lightoff(); j++; lighton();
     break;
    }
   }
}
void lightoff()   /* void ligtoff() */
{
 if(y>23)y=4;
 if(y<4)y=23;
 if(j>19)j=0;
 if(j<0)j=19;
 textbackground(13);
 gotoxy(39,y);
 cprintf("%s",letters[j]);
}
void lighton()
{
 if(y>23)y=4;
 if(y<4)y=23;
 if(j>19)j=0;
 if(j<0)j=19;
 textbackground(1);
 gotoxy(39,y);
 cprintf("%s",letters[j]);
}
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

gothic130Author Commented:
Thanks, but the letters remains with the color (lighton)and doesn't change (lightoff). I think I saw the problem: when you move through the letters, they got blue color (lighton) in their right position (A,B,...T) but in the pink color (lightoff) the letters are wrong (B,C,D...,A) and every time the cursor pass, the letter change. You can see this if you write gotoxy(42,y); in lighton function. What can I do?
0
VB-ExpertCommented:

Which Version of 'C' are you using in this case.  Have used Turboc C & Visual C.

VB-Expert
0
Kent OlsenDBACommented:

Ahha.

In function keyboard(), call lighoff() before you change *y*.


void keyboard()
{
 char key;
 while(key!='s')
   {
    key=getch();
    switch(key)
    {
     case'H': lightoff(); y--; j--; lighton();
     break;
     case'P': lightoff(); y++; j++; lighton();
     break;
    }
   }
}



Kent
0
gothic130Author Commented:
Thanks Kent.  Why do you have to write int main and not just main?
And what's the letter for the ENTER key when you use case '?' ? because the last part of the program is to choose the letter you want from the list and the computer have to write "You picked the "L"".

jejejeje The program is almost done :) :) :)


ps. I'm using turbo c 2.01.
0
Kent OlsenDBACommented:

Every routine that you write in C is a function.  Period.  The word "main" has meaning to you and me, but to the computer "main" is just a string of characters that represent a valid function name.  The "whys" are a bit complicated.

When you compile and link a C program, the linking process merges a "base" file from the C library.  This base file performs some initialization for you in the form of making sure that proper access to stdin, stdout, and stderr is available, argc and argv are properly initialized, establishing and initializing the stack, initializing the heap, and performing several other startup items.

The base file also contains what is called a "transfer address".  This is the address where the program will start executing as soon as the operating system finishes loading the program.  main() is NOT the transfer address, it's something more obscure like c0x.

Once the startup code completes it calls your routine by executing main().

The only reason that you MUST have a function called main() is that the startup code can properly call your program.  Since main() is really just another function the declaration for main() must match any function header that exists for main().  In your case, it looks like the function prototype requires that main be typed as an int.  It's strange that you have to explicitly put the *int* keyword in front of main(), because that should be the default.


Kent
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gothic130Author Commented:
Wow, programming is not as easy as I thought :) but I like it!!!.
Do you know which letter should I use for the ENTER key in case ' '  ?
Thanks.
0
Kent OlsenDBACommented:

The enter key is the same as hex value 0x0A (which is the same as decimal 10).  It's also known at ctrl-J.  You can modify the case to look like this:

    switch(key)
    {
     case 'H':
       lightoff(); y--; j--; lighton();
       break;

     case 'P':
       lightoff(); y++; j++; lighton();
       break;

     case 0x0A:  /* Carriage Return (Enter)  */
       break;
    }


Kent
0
gothic130Author Commented:
The program is complete except for one thing: you have to press ctrl-J instead of enter.
This is it:

case 0x0A: textbackgrund(1);

cprintf ("The letter you chose: %s",letters[j];

break;

Any idea?
0
Kent OlsenDBACommented:


Add this case statement:

case 0x0D:  /*  Exit  program  */


Kent
0
gothic130Author Commented:
Well, the program is running quite goodddd!!!

Thank you very much KENT and VB-expert.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.