Files, Encryption and Assignment 9


See pages 255-267 in A Computer Science Tapestry for more information

We have been using the command "cin>>variable" to get information from a user via the keyboard.  You may have wondered why we need to use both "cin" and the extraction operator (>>) to accomplish this, when it seems like either one would do.  The reason is that the extraction operator (>>) is a general operator for getting input, whereas the "cin" command tells the program that the source of the input, which defaults to the keyboard.  This means we can use the extraction operator with other input sources other than the keyboard.

The most logical alternative source of input is from external files.  When you use a word processing program you don't expect to have to start a paper from scratch every time you turn on the computer.  Often, you will load a file from disk first.  You can perform disk operations like this by using the extraction operator in conjunction with a command that routes input from a file on disk.

This is most easily illustrated in a short program, that we can then analyze line by line.  The following program reads text from a file "games80.txt" and prints out all of the words in the file on the screen.

#include <iostream.h>

#include <fstream.h>

#include <apstring.h>

int main(void)


	apstring word;

	ifstream myfile;"games80.txt");






	return 0; 


In order for the program to read the file you must store the file games80.txt in the same folder as your program.  Now let's take a look at the files that we haven't seen before. 

This use of files isn't all that productive.  If, however, we change the loop part of the above program between and myfile.close to read as follows, the program seems a bit more useful:

apstring game;

int found;


cout<<"Enter a game to look for: ";





		cout<<"Found your game";




	cout<<"Didn't find your game";

Now, the program will search for a user input game and tell us if it is in the list.  At times it may be useful to search through the same file multiple times in one program.  You can do this simply by closing the file and opening it again. Every time you open a file it starts at the beginning.

You can also use this input technique to input numbers as well as strings, in the same way that you can use cin to input numbers and strings.  You will need to know the pattern of your file because if you try to input words into an "int" variable type you will get strange results.  For example if you know that your file has a name and number in it for 5 people you could use the following loop to input the names and numbers:

apstring name;

int number,i;







The idea of secretly encoding messages is quite old, and doesn't require much technology.  However, computers have advanced the process of message coding to a well studied science.  The idea of using computers to make and break codes is receiving a lot of attention in the popular media today with many books and movies on the subject, and it essential to the security of many businesses and commerce on the Internet.  The bulk of the math and programming on encryption (encoding of messages) and decrytpion (decoding of messages) is beyond the scope of this course, but is something that will likely be covered in future semesters.  However, a very brief introduction to encryption is relevant at this time.

The basic idea in encryption is to alter a number or word so that it is unreadable by someone intercepting the information.  You may have made codes like this at some point where one letter in the alphabet replaces another letter.  We used this idea in Visual Basic class.  For the purposes of the program here we are interested in encrypting numbers.  One very simple method of encrypting a number is to multiply it by some fixed value.  This number can then be decrypted at the other end by dividing by this same number.  Only if someone knows what number you multiplied by will they be able to get at the original number.  You can make this more complicated by combining the basic math functions in some sequence to encode the number and then doing them in reverse to decode the number. 

Encryption is quite important in the storage of passwords.  It is dangerous to leave a file of passwords on a computer (such as our network server), but this is essential for checking of passwords of people logging in.  So, what is usually done is that the file containing the passwords is encrypted so that if it is hacked into, no one can read the passwords.  Then one someone types in their password, it is encrypted in the same way as the passwords in the file, and the two encrytped passwords are checked against each other.

As an example suppose that my encryption technique was to multiply the number by 2.  If someone's password was 1234, then the number 2468 would be stored in the password file.  Then when that person typed in their password when they log in, it will be doubled and then checked against the number 2468.  This way the actual password is never stored on a disk, and never transmitted to the password checker (only the encrypted number is passed).

In the example here, it is fairly easy to crack the code, and once this is done, it is possible to recreate the original password from the encoded one.   However, this is not always possible.  If, for example your encryption included squaring a number it might not be possible to recreate the original number.   This is because squaring a number loses information (i.e. if the encrypted number is 4, the original number could be 2 or it could be -2, both of which equal 4 when squared).

Those are the basics of encryption.  Which are enough for our purposes.

Assignment 9

You have all used the "gradechecker" program to look at your grades in this class.  This program was, in fact, written by a student in this school.  Your assignment is to write a C++ version of this program (the original one is written in a language called "Perl" which runs on many web servers).   You will make a name and grades file by hand which will contain at least a  person's last name and 5 grades for at least 5 people.  You should make this file in Notepad and save it to your program folder.  You will have a second file that contains a list of last names and (already encrypted) passwords.  This file should also be made in Notepad and saved in your folder.  Make sure that you separate names and grades with spaces. 

Your program should prompt a user for their last name and their password.  It should then check to see whether the password is correct.  If it is incorrect it should give them another try before quitting.  If it is correct, it should display their grades.  Even though the grades are numbers you can treat them as strings.

For those of you having trouble getting started, you may forget about all of the password stuff and simply have person enter their name and check their grades.  For those of you that cruise through the project, I suggest you search the Internet for information on encryption techniques that you could use in your program.   You may only do this once you have a working version of the program with a simple encryption technique that you have demonstrated. 

Click Here to Download a DOS Version of the Program comptr9.gif (546 bytes)

(Note: You must download grades.txt and password.txt and put them in the same folder as assign9.exe for this program to work.  The encryption tecnique used in this example is doubling the number.)

This assignment is due Tuesday, May 5.  It should be put in Assignment 9 folder by that time.  It will be graded on the following factors.

  1. Does it work?
  2. How well is the program commented and how well variables are named?
  3. Did you  use external files and is  the format of these files easy to understand?
  4. DId you use encryption?
  5. Is your porgram broken up into small but meaningful functions?
  6. Creativity

Back to Home Page House3.wmf (25540 bytes)