-
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.
::confused:
-
After the standard errors about "iostream.h", I get an error about this line:
const char plainalpha[max]= "abcdefghijklmnopqrstuvwxyz"; /* plain text */
-
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;