Solved

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

Posted on 2003-10-25
13
209 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
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
first how did you get it to compile??

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

should be

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

0
 

Author Comment

by:gothic130
Comment Utility
Just a finger mistake!!!
0
 
LVL 1

Assisted Solution

by:VB-Expert
VB-Expert earned 50 total points
Comment Utility

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
 

Author Comment

by:gothic130
Comment Utility
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
Comment Utility

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

VB-Expert
0
 
LVL 45

Expert Comment

by:Kdo
Comment Utility

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:gothic130
Comment Utility
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:
Kdo earned 450 total points
Comment Utility

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
Comment Utility
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:Kdo
Comment Utility

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
Comment Utility
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:Kdo
Comment Utility


Add this case statement:

case 0x0D:  /*  Exit  program  */


Kent
0
 

Author Comment

by:gothic130
Comment Utility
Well, the program is running quite goodddd!!!

Thank you very much KENT and VB-expert.

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now