0

First let me show what's working then I will show what's not working. This code gives the right result.

unsigned long timeOn;
long d[10];

d[0] = 8;
d[1] = 6;
d[2] = 0;
d[3] = 0;
d[4] = 0;
timeOn = 10000*d[0] + 1000*d[1] + 100*d[2] + 10*d[3] +  d[4] ;
printf("%lu",timeOn);

The output: 86000

If I want the user to input the following values I get a different result and this is the code I have.

unsigned long timeOn;
long d[10];
int i;


 for(i = 0; i < 5 ; i++)
    {
        while (!ConsoleIsGetReady());
        d[i] = ConsoleGet();

    }

timeOn = 10000*d[0] + 1000*d[1] + 100*d[2] + 10*d[3] +  d[4] ;
printf("%lu",timeOn);

BYTE ConsoleGet(void)
{
    char Temp;

    while(IFS1bits.U2RXIF == 0);

    Temp = U2RXREG;
    IFS1bits.U2RXIF = 0;
    return Temp;
}

The output: 619328

Isn't this suppose to work the same way? So how come when the user inputs the values I get a different result? Thank you!

Ammar
  • 1,203
  • 5
  • 27
  • 64

1 Answers1

0

Looks like the console outputs a character (i.e. code in ASCII), not an integer. Just try d[i] = ConsoleGet() - '0'; in your read loop.

nullptr
  • 11,008
  • 1
  • 23
  • 18
  • By the way, you will get the same result if you use `d[i]=getchar();` instead of `scanf("%d", &d[i]);`. That's the difference between an integer value and a character code of a digit (in ASCII, code of '0' is 48, code of '1' is 49, etc.) – nullptr May 07 '13 at 15:23