views:

49

answers:

3

I am doing my project to implement a typing tutor application in turbo C compiler (this is the limit), this function is actually a look up table for x,y and key character but it is returning a garbage value at first value of that I passed in argument. From second onwards it starts doing right. Note that that x,y are pointers to return two values at time. Please CHECK what is my mistake.

s1 = "ASDF JKL; ADSF JLK; ";
char take_xy(char s1[], int j, int *x, int *y)
{
    char ch;
    switch(s1[j])
    {
        case 'Q' :  *x = 137;   *y = 244;   ch = 'Q';   break;
        case 'W' :  *x = 160;   *y = 244;   ch = 'W';   break;
        case 'E' :  *x = 183;   *y = 244;   ch = 'E';   break;
        case 'R' :  *x = 206;   *y = 244;   ch = 'R';   break;
        case 'T' :  *x = 229;   *y = 244;   ch = 'T';   break;
        case 'Y' :  *x = 252;   *y = 244;   ch = 'Y';   break;  
        case 'U' :  *x = 275;   *y = 244;   ch = 'U';   break;
        case 'I'  : *x = 298;   *y = 244;   ch = 'I';   break;
        case 'O' :  *x = 321;   *y = 244;   ch = 'O';   break;
        case 'P' :  *x = 344;   *y = 244;   ch = 'P';   break;

        case 'A' :  *x = 144;   *y = 268;   ch = 'A';   break;
        case 'S' :  *x = 167;   *y = 268;   ch = 'S';   break;
        case 'D' :  *x = 190;   *y = 268;   ch = 'D';   break;
        case 'F' :  *x = 213;   *y = 268;   ch = 'F';   break;
        case 'G' :  *x = 236;   *y = 268;   ch = 'G';   break;
        case 'H' :  *x = 259;   *y = 268;   ch = 'H';   break;
        case 'J' :  *x = 282;   *y = 268;   ch = 'J';   break;
        case 'K' :  *x = 305;   *y = 268;   ch = 'K';   break;
        case 'L' :  *x = 328;   *y = 268;   ch = 'L';   break;
        case ';' :  *x = 351;   *y = 268;   ch = ';';   break;
//      case ''' :  *x = 374;   *y = 268;   ch = ''';   break;

        case 'Z' :  *x = 162;   *y = 292;   ch = 'Z';   break;
        case 'X' :  *x = 185;   *y = 292;   ch = 'X';   break;
        case 'C' :  *x = 208;   *y = 292;   ch = 'C';   break;
        case 'V' :  *x = 231;   *y = 292;   ch = 'V';   break;
        case 'B' :  *x = 254;   *y = 292;   ch = 'B';   break;
        case 'N' :  *x = 277;   *y = 292;   ch = 'N';   break;
        case 'M' :  *x = 300;   *y = 292;   ch = 'M';   break;
        case '?' :  *x = 369;   *y = 292;   ch = '?';   break;

        case ' ' :  *x = 189;   *y = 316;   ch = ' ';   break;

    }
    return ch;
}    
A: 

There is nothing wrong with the code you have posted, the error must be somewhere else.

Alexander Rafferty
+3  A: 

I would check how you are calling this function. I would guess that the 'j' parameter is less than 0 or greater than 19.

Also run in a debugger and check the value of all your parameters the first time it is called.

As @Matthew Flaschen indicated, let's see the calling code.

EDIT

You might want to also add a default clause to catch anything that doesn't match.

Edward Leno
+3  A: 

It looks like the contents of sl[j] may not covered in that switch, so "ch" has whatever default value it contains when created on the stack.

I would do one of the following:

  1. Normalize the contents of sl[j] before using it in the switch.
  2. Add a "default" tag to the switch.
  3. Initialize "ch" to some default value when you declare it (equivalent to option 2).
John Gaughan
default is not a problem. 2nd I have checked it via debugger but no problem there if I add default value then it returns that default value it doesn't get into the case structure.
Umair Khan
That tells me that the contents of sl[j] aren't covered by the switch. When you debug into that function, what value does it have?
John Gaughan