Cipher program

This is a discussion on Cipher program within the C++ Programming forums, part of the General Programming Boards category; I'm trying to figure out this program, see attch. It is a wonderful code, others I've seen others that are ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    16

    Cipher program

    I'm trying to figure out this program, see attch.
    It is a wonderful code, others I've seen others that are massive bundles of code upon bundles, but this is one is short and it works great.
    I've got three questions, see if any one can explain these things:
    1. Why the bool ? It needs it to run but I dont'
    see why it's there.
    2. This statement:
    reverse (instring.begin (), instring.end ());
    string outstring (instring);
    How does is work? I've tried to run it on it's own and it wont do anything.
    3. The series of code lines that are like these:
    s1[s1size++] = i;
    This is bit manipulation right?
    I've asked a few people here and they don't know,
    shows the level of our knowledge.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Dear Mr. Mule,

    Most intelligent people are not going to download anything from a random source. If you want someone to look at the code, first read the first thread on the forum. It says something like:

    READ THIS BEFORE POSTING

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    16
    My apologies, I didn't know how else to ask without showing the code. Senior Member, you are right.

  4. #4
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    geeze...no line breaks? Anyway, you had a decent question, so I'm going to do this for you...

    Code:
    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main ()
    {
        const int MAXCHAR = 80;
        int k1, k2, k3;
        int s1[MAXCHAR], s2[MAXCHAR], s3[MAXCHAR];
        unsigned s1size, s2size, s3size;
        bool generate = false;
        ifstream infile ("wscipher.in");
    
        if (!infile) 
        {
    	cerr << "Can't open wscipher.in" << endl;
    	exit (2);
        }
    
        ofstream outfile ("wscipher.out");
        if (!outfile) 
        {
    	cerr << "Can't open wscipher.out" << endl;
    	exit (2);
        }
    
        while ((infile >> k1 >> k2 >> k3) && (k1 + k2 + k3 != 0)) 
        {	
    	unsigned i;
    	string instring;
    	int target;
    
    	infile.ignore (80, '\n');
    	infile >> instring;     // ok, no spaces
    	if (generate)
    	    reverse (instring.begin (), instring.end ());
    	string outstring (instring);
    
    	s1size = s2size = s3size = 0;
    	for (i = 0; i < instring.size (); i++) 
    	{
    	    if (instring[i] >= 'a' && instring[i] <= 'i')
    		s1[s1size++] = i;	    
    	    else if (instring[i] >= 'j' && instring[i] <= 'r')
    		s2[s2size++] = i;	    
    	    else
    		s3[s3size++] = i;
    	}
    
    	for (i = 0; i < s1size; i++) 
    	{
    	    target = s1[(i + k1) % s1size];
    	    outstring[target] = instring[s1[i]];
    	}
    	//cerr << "outstring = " << outstring << endl;
    
    	for (i = 0; i < s2size; i++) 
    	{
    	    target = s2[(i + k2) % s2size];
    	    outstring[target] = instring[s2[i]];
    	}
    	//cerr << "outstring = " << outstring << endl;
    
    	for (i = 0; i < s3size; i++) 
    	{
    	    target = s3[(i + k3) % s3size];
    	    outstring[target] = instring[s3[i]];
    	}
    	//cerr << "outstring = " << outstring << endl;
    
    	if (generate) 
    	{
    	    outfile << k1 << " " << k2 << " " << k3 << endl;
    	    reverse (outstring.begin (), outstring.end ());
    	}
    	//cerr << "outstring = " << outstring << endl;
    
    	outfile << outstring << endl;
        }
        return 0;
    }
    >>Why the bool ?
    bool generate = false;
    ...
    if (generate)

    The bool is there for that if statement...if you read through what the if statement does, you can see what would happen if you changed the first line to
    bool generate = true; in the code.

    >>The series of code lines that are like these:
    >>s1[s1size++] = i;
    >>This is bit manipulation right?

    No. If you look a few lines before that, you'll see
    s1size = s2size = s3size = 0;
    s1 is defined up at the top as int s1[MAXCHAR], meaning that it is an array of size MAXCHAR. s1size++ increments s1size. It's the same as s1size=s1size+1. So, s1[s1size++] is accessing the element in the array s1 after the one that was just accessed (in the last repetition of the loop).

    As for your other question...I dunno, I've never done much with strings.

    [edit] Somehow, the code got doublespaced when I copied it.
    Last edited by confuted; 04-26-2003 at 07:59 PM.
    Away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21