Thread: Cryptograph problem

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    9

    Exclamation Cryptograph problem

    salutationz to (anyone there who is KIND enuff to help me out)..


    here's the code:

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    #include <conio.h>
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <process.h>
    
    const int max=26; //size of the cipher and plain alphabets arrayz
    
    const char plainalpha[max]= "abcdefghijklmnopqrstuvwxyz"; /* plain text */
    
      /* cipher alphabets used for deciphering */
    
    const  char cipheralpha1[max]="coblfvrtanpsgiymzdwkjehpuq";
    const  char cipheralpha2[max]="zyxwvutsrqponmlkjihgfedcba";
    const  char cipheralpha3[max]="mrfjlbvxaqwekpzndhocstiygu";
    const  char cipheralpha4[max]="qwertyuiopasdfghjklzxcvbnm";
    const  char cipheralpha5[max]="nowisthemfraylgdcqukxjpzv";
    
    class llist{
    
        private:
         llist * link;
         char alpha;
    
        public:
         llist(); /* constructor */
         void getchr(char); /* gets character from string */
         char returnchr(); /* returns character from linked list */
         void insertllist(llist *); /* insert the string into linear linked list */
         void removellist(llist *); /* removes the string from linear linked list */
         void decrypt(char,int); //pass string character by character and the index of the string.
         void encrypt(char,int); //pass string character by character and the index of the string.
          };
    
    //-------------------- /* global variable */
    
         llist * temp=NULL;
         llist * head=NULL;
    
    //-------------------- /* constructor */
    
        llist::llist()
          {
    	link=NULL;
          }
    //------------------- /* get character from string */
       void llist::getchr(char ch){
    	   temp->alpha=ch;
        }
    
        //-------------- /* returns character */
        char llist::returnchr(){
           return temp->alpha;
       }
    
    //-------------------- /* insertion into linear linked list */
    
        void llist::insertllist(llist * temp)
         {
    		if(head==NULL)
    		   {
    			 head=temp;
    		   }
    		    else
    		       {
    			  llist * i=head;
    			   i->link=temp;
    			   i=i->link;
    			}
    
    			cout<<"\n\t one node entered."<<endl;
          }
    
     //------------------ /* removal from linear linked list */
    
        void llist::removellist(llist * temp){
    		      if(temp==head){
    			  head=head->link;
    			  }
    			  else
    			    {
    			      llist * j=head;
    			      if(temp->link==NULL){
    				   j->link=NULL;
    				   j=j->link;
    			       }
    			    }
    			    delete temp;
    
    	 }
    
    
     //----------------/* encryption */
        void llist::encrypt(char chr,int count){
    	 int m=0, j=0, i=0, k=0, n=0;
    
           if(count==0){
    
    	 while(m<26){
    		if( plainalpha[m] == chr){
    		     cout<<cipheralpha1[m];
    		      break;
    		  }
    		  else
    		      m++;
    	      }
          }
    
         if(count==1){
    	 while(j<26){
    		if(plainalpha[j]==chr){
    				cout<<cipheralpha2[j];
    				break;
    	      }
    		else
    		   j++;
    	   }
         }
    
        if(count==2){
    	 while(i<26){
    
    	   if(plainalpha[i]==chr){
    
    		cout<<cipheralpha3[i];
    		break;
    	      }
    		else
    	      i++;
    	    }
        }
    
       if(count==3){
    	 while( k<26){
    	   if(plainalpha[k]==chr){
    		cout<<cipheralpha4[k];
    		break;
    	      }
    		else
    	      k++;
    
    	   }
       }
    
       if(count==4){
    	 while(n<26){
    
    	    if(plainalpha[n]==chr){
    		cout<<cipheralpha5[n];
    		break;
    	      }
    		else
    	      n++;
    	 }
      }
    
    }
    
    //----------------------/* decryption */
       void llist::decrypt(char chr,int count){
    
       int m=0, j=0, i=0, k=0, n=0;
       if(count==0){
    
    	 while(m<26){
    		if( cipheralpha1[m] == chr){
    		     cout<<plainalpha[m];
    		      break;
    		  }
    		  else
    		      m++;
    	      }
     }
    
       if(count==1){
    	 while(j<26){
    		if(cipheralpha2[j]==chr){
    				cout<<plainalpha[j];
    				break;
    	      }
    		else
    		   j++;
    	   }
       }
    
       if(count==2){
    	 while(i<26){
    
    	   if(cipheralpha3[i]==chr){
    
    		cout<<plainalpha[i];
    		break;
    	      }
    		else
    	      i++;
    	    }
       }
    
       if(count==3){
    	 while( k<26){
    	   if(cipheralpha4[k]==chr){
    		cout<<plainalpha[k];
    		break;
    	      }
    		else
    	      k++;
    
    	   }
       }
    
       if(count==4){
    	 while(n<26){
    
    	    if(cipheralpha5[n]==chr){
    		cout<<plainalpha[n];
    		break;
    	      }
    		else
    	      n++;
    	 }
     }
    
    }
    
    			 /* main menu */
    
    	char mainmenu( ){
    	  char choice;
    	    cout<<"\n\t------------------------------------------"<<endl;
    	    cout<<"\n\t          MENU        "<<endl;
    	    cout<<"\n\t------------------------------------------"<<endl;
    	    cout<<"\n\n\t 1. Encrypt. "<<endl;
    	    cout<<"\n\n\t 2. Decrypt. "<<endl;
    	    cout<<"\n\n\t\t (E)xit from program. "<<endl;
    	    cout<<"\n\n\t Enter your choice : ";
    	   cin>>choice;
    	   return choice;
    	}
    
    				/* main driver */
    
    	 void main(){
    	     clrscr();
    
    	       char choice;
    	       llist l;
    			  char str[100];
    			  char chr;
    			  int check=0;
    			  int count=0;
    
    	       do
    		{
    		  choice=mainmenu();
    		    switch(choice){
    
    		      case '1':
    			  clrscr();
    
    
    			  cout<<"\n\t Enter the plain text :" ;
    			  gets(str);
    
    			  int length=strlen(str);
    
    			  cout<<"\n\t Encrypted text : ";
    
    			  for(int r=0; str[r]!='\0' ; r++){
    
    			     temp=new llist; //creates a new node each time.
    
    			     l.getchr(str[r]);
    
    			     check=isalpha(str[r]);
    							  
                                     if(count==5)
    				     count=0;
    
    				if(check!=0 && count < 5 )
    				   {
    					chr=l.returnchr();
    					l.encrypt(chr,count);
    					count++;
    					
    				   }
    				   else
    				     if(check==0)
    					 cout<<str[r];
    				  }
    				    
    				 if(r==(length-1)){
    				     count=0;
    			      }
    
    
    			    l.insertllist(temp);
    			    l.removellist(temp);
    
    			    break;
    
    		     case '2':
    			     clrscr();
    
    	cout<<"\n\t Enter the encrypted text :" ;
    	  gets(str);
    
    	  cout<<"\n\t Decrypted text : ";
    
    	 for(int s=0; str[s]!=NULL ; s++){
    	      temp=new llist;
    	      l.getchr(str[s]);
    
    	      check=isalpha(str[s]);
    
    		   if(s==(length-1))
    		       count=0;
    
    		  if(count==5)
    		     count=0;
    
    		if(check!=0 && count < 5 )
    		   {
    			chr=l.returnchr();
    			l.decrypt(chr,count);
    			count++;
    		   }
    	   else
    		     if(check==0)
    			 cout<<str[s];
    	      }
    			 if(s==length)
    			   count=0;
    
    		   l.insertllist(temp);
    
    		   l.removellist(temp);
    
    		   break;
    
    
    		      case 'E':
    		      case 'e':
    			    exit(0);
    
    		      default:
    			cout<<"\n\t Invalid characters entered. "<<endl;
    		 }
    		}while(choice!='e' && choice!='E');
    
    			  getch();
    	}
    An idea of how this program is supposed to work:
    A user enters any string and the first letter of the message is deciphered using the first cipher alphabet and the second from the second cipher and so on, until the sixth letter of the message appears which starts over again using the first cipher alphabet. All non-alphabetic characters go as it is.

    Now my problem is........before i go on to that...im outta practice in C++ soooo bear with me...newayz,
    1) when a new string is entered why doesnt it go back to the first cipher alphabet? It just continues on where it left it.... what am i DOIng wrong?
    2) what is stdin and stdout? and how is it used?

    Appreciate it!

    F.k.
    :
    Last edited by Lollipop; 07-25-2003 at 01:25 PM.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    After the standard errors about "iostream.h", I get an error about this line:

    const char plainalpha[max]= "abcdefghijklmnopqrstuvwxyz"; /* plain text */
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    jus to capitolize on major's post, the errors on iostream.h are because your using a depreciated header, try this code:

    Code:
    #include <iostream>
    
    using namespace std;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM