• 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?
 
Kent OlsenConnect With a Mentor Data Warehouse Architect / DBACommented:

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
 
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
Identify and Prevent Potential Cyber-threats

Become the white hat who helps safeguard our interconnected world. Transform your career future by earning your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

 
VB-ExpertConnect With a Mentor Commented:

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
 
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 OlsenData Warehouse Architect / DBACommented:

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
 
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 OlsenData Warehouse Architect / DBACommented:

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 OlsenData Warehouse Architect / DBACommented:


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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.