C++ Tutorial/Data Types/char

Материал из C\C++ эксперт
Перейти к: навигация, поиск

char and unsigned char

#include <iostream.h>
int main(){
       int n1 = 100,n2;
       short int s1 = 2,s2;
       char c1 = "A",c2;
       unsigned char u1 = "U",u2;
       n2 = s1;
       s2 = n1 + c1;
       c2 = u1;
       u2 = n1;
       cout << n1 << " " << n2 ;
       cout << s1 << " " << s2 ;
       cout << c1 << " " << c2 ;
       cout << (int)c1 << " " << int(c2);
       cout << u1 << " " ;
       cout << (int)u1 << " " << (int)u2;
    return 0;
}
100 22 165A U65 85U 85 100"

DO while loop: use char type variable to control

#include <iostream>   
using namespace std;   
  
int main(){   
   long dividend, divisor;   
   char ch;   
  
   do{
      cout << "Enter dividend: "; cin >> dividend;   
      cout << "Enter divisor: ";  cin >> divisor;   
      cout << "Quotient is " << dividend / divisor;   
      cout << ", remainder is " << dividend % divisor;   
  
      cout << "\nDo another? (y/n): ";  //do it again?   
      cin >> ch;   
   }while( ch != "n" );
   return 0;   
}

Using a nested if with char type

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main() {
  char letter = "S";                      
  if(letter >= "A") {                   
    if(letter <= "Z") {                 
      cout << "You entered an uppercase letter."
           << endl;
      return 0;
    }
  }
  if(letter >= "a")                     
    if(letter <= "z") {                 
      cout << "You entered a lowercase letter."
           << endl ;
      return 0;
    }
  return 0;
}
You entered an uppercase letter.

Write Charset To File

/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <string>       // for strings
#include <iostream>     // for I/O
#include <fstream>      // for file I/O
#include <iomanip>      // for setw()
#include <cstdlib>      // for exit()
using namespace std;
// forward declarations
void writeCharsetToFile (const string& filename);
void outputFile (const string& filename);
int main ()
{
    writeCharsetToFile("charset.out");
    outputFile("charset.out");
}
void writeCharsetToFile (const string& filename)
{
    // open output file
    ofstream file(filename.c_str());
    // file opened?
    if (! file) {
        // NO, abort program
        cerr << "can"t open output file \"" << filename << "\""
             << endl;
        exit(EXIT_FAILURE);
    }
    // write character set
    for (int i=32; i<256; i++) {
        file << "value: " << setw(3) << i << "   "
             << "char:  " << static_cast<char>(i) << endl;
    }
}   // closes file automatically
void outputFile (const string& filename)
{
    // open input file
    ifstream file(filename.c_str());
    // file opened?
    if (! file) {
        // NO, abort program
        cerr << "can"t open input file \"" << filename << "\""
             << endl;
        exit(EXIT_FAILURE);
    }
    // copy file contents to cout
    char c;
    while (file.get(c)) {
        cout.put(c);
    }
}   // closes file automatically
value:  32   char:
value:  33   char:  !
value:  34   char:  "
value:  35   char:  #
value:  36   char:  $
value:  37   char:  %
value:  38   char:  &
value:  39   char:  "
value:  40   char:  (
value:  41   char:  )
value:  42   char:  *
value:  43   char:  +
value:  44   char:  ,
value:  45   char:  -
value:  46   char:  .
value:  47   char:  /
value:  48   char:  0
value:  49   char:  1
value:  50   char:  2
value:  51   char:  3
value:  52   char:  4
value:  53   char:  5
value:  54   char:  6
value:  55   char:  7
value:  56   char:  8
value:  57   char:  9
value:  58   char:  :
value:  59   char:  ;
value:  60   char:  <
value:  61   char:  =
value:  62   char:  >
value:  63   char:  ?
value:  64   char:  @
value:  65   char:  A
value:  66   char:  B
value:  67   char:  C
value:  68   char:  D
value:  69   char:  E
value:  70   char:  F
value:  71   char:  G
value:  72   char:  H
value:  73   char:  I
value:  74   char:  J
value:  75   char:  K
value:  76   char:  L
value:  77   char:  M
value:  78   char:  N
value:  79   char:  O
value:  80   char:  P
value:  81   char:  Q
value:  82   char:  R
value:  83   char:  S
value:  84   char:  T
value:  85   char:  U
value:  86   char:  V
value:  87   char:  W
value:  88   char:  X
value:  89   char:  Y
value:  90   char:  Z
value:  91   char:  [
value:  92   char:  \
value:  93   char:  ]
value:  94   char:  ^
value:  95   char:  _
value:  96   char:  "
value:  97   char:  a
value:  98   char:  b
value:  99   char:  c
value: 100   char:  d
value: 101   char:  e
value: 102   char:  f
value: 103   char:  g
value: 104   char:  h
value: 105   char:  i
value: 106   char:  j
value: 107   char:  k
value: 108   char:  l
value: 109   char:  m
value: 110   char:  n
value: 111   char:  o
value: 112   char:  p
value: 113   char:  q
value: 114   char:  r
value: 115   char:  s
value: 116   char:  t
value: 117   char:  u
value: 118   char:  v
value: 119   char:  w
value: 120   char:  x
value: 121   char:  y
value: 122   char:  z
value: 123   char:  {
value: 124   char:  |
value: 125   char:  }
value: 126   char:  ~
value: 127   char:  ?
value: 128   char:  �
value: 129   char:
value: 130   char:
value: 131   char:
value: 132   char:
value: 133   char:
value: 134   char:
value: 135   char:
value: 136   char:
value: 137   char:
value: 138   char:
value: 139   char:
value: 140   char:
value: 141   char:
value: 142   char:
value: 143   char:
value: 144   char:
value: 145   char:
value: 146   char:
value: 147   char:
value: 148   char:
value: 149   char:
value: 150   char:
value: 151   char:
value: 152   char:
value: 153   char:
value: 154   char:
value: 155   char:
value: 156   char:
value: 157   char:
value: 158   char:
value: 159   char:
value: 160   char:
value: 161   char:
value: 162   char:
value: 163   char:
value: 164   char:
value: 165   char:
value: 166   char:
value: 167   char:
value: 168   char:
value: 169   char:
value: 170   char:
value: 171   char:
value: 172   char:
value: 173   char:
value: 174   char:
value: 175   char:
value: 176   char:
value: 177   char:
value: 178   char:
value: 179   char:
value: 180   char:
value: 181   char:
value: 182   char:
value: 183   char:
value: 184   char:
value: 185   char:
value: 186   char:
value: 187   char:
value: 188   char:
value: 189   char:
value: 190   char:
value: 191   char:
value: 192   char:
value: 193   char:
value: 194   char:
value: 195   char:
value: 196   char:
value: 197   char:
value: 198   char:
value: 199   char:
value: 200   char:
value: 201   char:
value: 202   char:
value: 203   char:
value: 204   char:
value: 205   char:
value: 206   char:
value: 207   char:
value: 208   char:
value: 209   char:
value: 210   char:
value: 211   char:
value: 212   char:
value: 213   char:
value: 214   char:
value: 215   char:
value: 216   char:
value: 217   char:
value: 218   char:
value: 219   char:
value: 220   char:
value: 221   char:
value: 222   char:
value: 223   char:
value: 224   char:
value: 225   char:
value: 226   char:
value: 227   char:
value: 228   char:
value: 229   char:
value: 230   char:
value: 231   char:
value: 232   char:
value: 233   char:
value: 234   char:
value: 235   char:
value: 236   char:
value: 237   char:
value: 238   char:
value: 239   char:
value: 240   char:
value: 241   char:
value: 242   char:
value: 243   char:
value: 244   char:
value: 245   char:
value: 246   char:
value: 247   char:
value: 248   char:
value: 249   char:
value: 250   char:
value: 251   char:
value: 252   char:
value: 253   char:
value: 254   char:
value: 255   char:  �