Hi, ihsir
Hope that u all can help me, i really don't have any ideals to solve these problems. Pls see my incomplete assigment and give some advices. Thanks!!!
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
class String
{
private:
char *string;
int len;
public:
String(char * = "");
~String(void){delete[] string;}
void getString(void){cout << "The input is: "; fflush(stdin); gets(string);}
void display(void){cout << "You have entered as follow : "; cout << string << endl << endl;}
int code(void);
int decode(void);
};
String::String(char *s)
{
len=strlen(s)+1;
string=new char[len];
strcpy(string,s);
}
int String::code()
{
char *temp=string;
char sample[96]={'0','1','2','3','4','5','6','7','8','9','A','B', 'C','D',
'E','F','G','H','I','J','K','L','M','N','O','P','Q ','R',
'S','T','U','V','W','X','Y','Z','a','b','c','d','e ','f',
'g','h','i','j','k','l','m','n','o','p','q','r','s ','t',
'u','v','w','x','y','z','!','"','#','$','%','&',', ',' ',
'(',')','*','+','-','.','/',':',';','<','=','>','?','@',
'[','\\',']','^','_','`','{','|','}','~'};
char code[96] ={'E','S','c','r','#','/','[','H','R','h','5','D','Q','b',
'p','"','.','^','0','K','W','k','x',')','1','A','P ','d',
'v','%','-',']','9','J','U','m','o','!',',','4','G','T',
'i','u','&',';','{','7','C','V','f','w','$','_',': ','8',
'M','X','g','s','(','\\',',','+','|','6','B','O',' a','n',
'*','=','3','F','Y','Z','j','t','q','@','`','2','N ','e',
'z',' ','~','I','l','+','?','}','L','y','<','>'};
int count=0;
for(*temp;*temp!=NULL;*temp++)
{
while(*temp!=sample[count])
count++;
cout << code[count];
count=0;
}
cout << endl;
return 0;
}
int String::decode()
{
char *temp=string;
char sample[96]={'0','1','2','3','4','5','6','7','8','9','A','B', 'C','D',
'E','F','G','H','I','J','K','L','M','N','O','P','Q ','R',
'S','T','U','V','W','X','Y','Z','a','b','c','d','e ','f',
'g','h','i','j','k','l','m','n','o','p','q','r','s ','t',
'u','v','w','x','y','z','!','"','#','$','%','&',', ',' ',
'(',')','*','+','-','.','/',':',';','<','=','>','?','@',
'[','\\',']','^','_','`','{','|','}','~'};
char code[96] ={'E','S','c','r','#','/','[','H','R','h','5','D','Q','b',
'p','"','.','^','0','K','W','k','x',')','1','A','P ','d',
'v','%','-',']','9','J','U','m','o','!',',','4','G','T',
'i','u','&',';','{','7','C','V','f','w','$','_',': ','8',
'M','X','g','s','(','\\',',','+','|','6','B','O',' a','n',
'*','=','3','F','Y','Z','j','t','q','@','`','2','N ','e',
'z',' ','~','I','l','+','?','}','L','y','<','>'};
int count=0;
for(*temp;*temp!=NULL;*temp++)
{
while(*temp!=code[count])
count++;
cout<< sample[count];
count=0;
}
cout << endl;
return 0;
}
void main(void)
{
char again,choice;
int i;
int len;
char filename[30];
char filename2 [1000];
cout << endl;
cout << "Welcome To Encryption/Decryption Machine" << endl << endl << endl;
cout << "This program is brought to you by : " << endl << endl;
cout << "Student Name : Cha Woo Chuan" << endl;
cout << "Student ID : 020191011348" << endl;
cout << "Class Code : IT355A " << endl;
cout << "Subject : Object-Oriented Design and Programming " << endl;
cout << " C++ Assignment For Term 1 Year 2002 " << endl;
cout << "Programming Lever : Beginner " << endl;
cout << "Lecturer's Name : Gurunthiah " << endl << endl << endl << endl;
cout << "Please press any key to continue...........";
getche();
String sentence;
do
{
do
{
clrscr();
cout << endl;
cout << "Encryption/Decryption Program Menu" << endl;
cout << endl << "**********************************" << endl;
cout << endl << endl;
cout << "Please select the following options: " << endl << endl;
cout << "1. Encrypt" << endl << "2. Decrypt" << endl ;
cout << endl << endl;
cout << "**********************************" << endl;
cin >> choice;
}
while((choice!='1')&&(choice!='2'));
cout << endl << endl;
sentence.getString();
sentence.display();
switch(choice)
{
case '1' : cout << "The encrypted message is : ";
sentence.code();
cout << endl;
cout << "Enter a name for the file with extension: " ;
cin >> filename;
cout << endl;
cout << "*** Note: Pls remember this filename for decryption purpose. ***" << endl << endl ;
break;
case '2' : cout << "Please enter the filename that consists the encrypted text : ";
cin >> filename2
if((fp=fopen(filename2,"r"))==NULL)
{
cout << "** Error opening filename **" << endl <<endl;
goto start;
}
while (!feof(fp))
{
}
cout << endl << "The encrypted text in the file \'"<<filename2<<"\' is : "<< str<<endl<<endl;
for (i=0; i<=len; ++i){
}
cout<<endl<<endl "The original text is: " <<endl <<endl;
goto start;
default;
exit;
}
return (0);
}
default: cout << "Wrong input!\n";
}
cout << "You can press any key to continue or press 'q' to quit";
again = getche();
}while(again!='q');
clrscr();
cout << "Have a nice day!" << endl;
getche();
}
Thanks!