E50a Section -- November 23rd, 1998
Characters and ASCII (American Standard Code for Information Exchange)
Basically, each characters gets represented by a code, known as the
ASCII value. There are other standards, like UTF and EBCDIC. The digits are represented by codes 48 through 57, the
upper-case letters by codes 65 through 90. and the lower case letters
are represented by codes 97 through 122.
Why is this useful?
Examples:
- to sort letters in alphabetical order,
- to compare 2 digits that are of type int, not char
- to convert a leter from upper to lower case
- to implement a secret encryption code (you know, one that will
never be figured out)where 'A' is replaced by 0, 'B'
by 1, 'C' by 2 etc...
Randomness
Randomness lets you add some fun to your program without needing the
user's input. This is useful if you want your program to behave
differently every time you run it (for example, for a solitaire game,
you want to deal the cards differently for each game).
How to add randomness to your program:
- make sure you include stdlib.h: #include <stdlib.h>
- to get a random sequence every time you run the program, you must
call the function srand() at the beginning of your program. Call it
only once! It initializes the random number generator. A good way to
initialize it is by using the time() or the clock() functions, since
these will be different ach time you run the program. If your seed is
always the same, then you might as well not use any, and always use
the default seed of 1.
ex: srand((unsigned int)time(NULL)); or
srand(clock());
(note: you must also include <time.h> to use the functions time and clock)
- to get a random integer between 1 and N: rand() % N + 1
File input/output
Reading from a file or writing to a file can be very useful. For
example, suppose that one of your friends has all this data that needs
to be processed. Your friend can just send it to you in a file, and
you will read the data from that file, process it, and give him the
results. You might want to write the results to another file...
How can you do this?
- include fstream: #include <fstream.h>
- find out which file you want to read/write, either by prompting
the user for it and storing its name in a variable, or by directly
writing the name of the file in the code
- declare a variable for the stream and say which file it refers to:
- to do input:
ifstream infile ("myfile.txt");
- to do output:
ofstream
outfile (myfilenamethatIgotfromtheuserandstoredinthisridiculouslylongvariable);
Of course, you can use a variable or put the actual filename for
an input file or an output file, the above is just an example.
- test that the file actually exists. If it doesn't, then the value
of the stream will be false, so you can find this out with a simple if
statement:
if (!infile)
cout<<"no such
file"<<endl;
- read/write to the file:
- read: just as you did cin>>blah; to store in
blah whatever the user typed, you can do infile>>blah;
to store in blah whatever you read from the file. This is when all the
ASCII conversion stuff can be useful, if you read character by
character, and want to convert the characters that represent digits
into actual integers.
- write: just as you did cout<<blah; to print
the value of blah on the screen, you can do outfile<<blah;
to print the value of blah in the file.
Note:
- The end of a file is marked by a special character, called the "end of
file character" or EOF (obviously...). The function streamname.eof()
will return true if the last character read is the EOF character, otherwise it
will return false. Therefore, you can do the following:
while (!infile.eof())
// do something
- You can use the built-in function get() to read a single
character from a stream.
ex: char c;
c = cin.get(); // read 1 character input by the user and store it in c
c = infile.get(); // read 1 character from the input file and store
it in c
Warning: get() will not skip white spaces!