CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

getc

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
homfray



Joined: 19 Nov 2003
Posts: 45
Location: Oxford

View user's profile Send private message Visit poster's website

getc
PostPosted: Thu Jun 22, 2006 5:14 am     Reply with quote

Ok I am having on of those days and this could be the fastest ever answer

Code:
   
   while(1)
   {
      answer=0;
      if(kbhit())    //if keyboard is hit
      {
         c = getc() & 0x5F;
         if(c == 'D')   
         {
            printf("\n\rPress 1-6\n\r");
            answer=getc();
            answer=answer+200;
            printf("%u",answer);
         }
      }
     } 
 


In this short code when I press D and then 1 why does't the answer come out as 201.

What I would like to do is some calculations based on the number typed in
any help would be grand

nice
_________________
Nice!!!
rberek



Joined: 10 Jan 2005
Posts: 207
Location: Ottawa, Canada

View user's profile Send private message

PostPosted: Thu Jun 22, 2006 6:15 am     Reply with quote

Answer will be the ASCII value of 1 , which is 49 decimal. Subtract 48 from answer to get the number you want.
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Thu Jun 22, 2006 6:23 am     Reply with quote

The problem is that you are adding "apples" + "oranges", then you make a decimal convertion and surelly your result is �249�

answer=getc();

Here, after you pressed �1�, answer = 0x31. // �1�= 0x31 (see ASCII table)

answer=(answer & 0x0F)+200; // ignore the high nibble of �answer� to get the �1�.

printf("%u",answer); // now you will get the expected �201�


Humberto
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group