Solved

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

Posted on 2003-10-25
13
211 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
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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

776 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