Solved

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

Posted on 2003-10-25
13
213 Views
Last Modified: 2010-04-15
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]);
}
0
Comment
Question by:gothic130
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 9621225
first how did you get it to compile??

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

should be

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

0
 

Author Comment

by:gothic130
ID: 9621376
Just a finger mistake!!!
0
 
LVL 1

Assisted Solution

by:VB-Expert
VB-Expert earned 50 total points
ID: 9621541

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:gothic130
ID: 9623059
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
 
LVL 1

Expert Comment

by:VB-Expert
ID: 9623235

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

VB-Expert
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9627713

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
 

Author Comment

by:gothic130
ID: 9628286
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
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 450 total points
ID: 9628827

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
 

Author Comment

by:gothic130
ID: 9629660
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
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9630002

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
 

Author Comment

by:gothic130
ID: 9630136
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
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9630483


Add this case statement:

case 0x0D:  /*  Exit  program  */


Kent
0
 

Author Comment

by:gothic130
ID: 9630683
Well, the program is running quite goodddd!!!

Thank you very much KENT and VB-expert.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Compile VxWorks Toronado project under Visual Studio 11 242
How to learn Linux? 10 81
Constant string is of type char *   ? 7 48
MySQL recovery 7 28
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question