home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16356 < prev    next >
Encoding:
Text File  |  1992-11-16  |  1.6 KB  |  49 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: unsigned char?
  5. Message-ID: <1992Nov14.221510.13655@taumet.com>
  6. Keywords: unsigned char?
  7. Organization: TauMetric Corporation
  8. References: <1992Nov13.195635.19707@imada.ou.dk>
  9. Date: Sat, 14 Nov 1992 22:15:10 GMT
  10. Lines: 37
  11.  
  12. adelt@imada.ou.dk (Adel Shavandi) writes:
  13.  
  14.  
  15. >I have the following code segment:
  16.  
  17. >    unsigned char    _number;
  18. >    ifstream istrm;
  19.  
  20. >    istrm>>_number;
  21.  
  22. >in the input file to be read, i am supposed to read 15, but by
  23. >inspection i find out that _number is 49!
  24.  
  25. The conversion for char or unsigned char reads one character from
  26. the input, possibly first skipping leading whitespace.  If the next
  27. input character was '1', then _number would would contain '1'.  The
  28. ASCII code set uses the value 49 to represent the character '1'.
  29. If you look at the value of _number as an integer, it will be 49,
  30. although as a character it will be '1'.  Example:
  31.     cout << "char  = " << _number << ", value = " << (int)_number << endl;
  32. This should print
  33.     char = 1, value = 49
  34. (Assuming your system uses ASCII, which is usually the case.)
  35.  
  36. If the input contains a sequence of characters you want to treat as an
  37. integer, read the value into a variable of some integer, not char, type:
  38.     int i;
  39.     unsigned long j;
  40.     istrm >> i >> j;
  41.  
  42. By the way, I suggest not using leading underscores on indentifiers in
  43. your own code.  It is a dangerous habit, as they may conflict with things
  44. in system headers.
  45. -- 
  46.  
  47. Steve Clamage, TauMetric Corp, steve@taumet.com
  48. Vice Chair, ANSI C++ Committee, X3J16
  49.