Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top