C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 15th, 2005, 12:46 PM
developer.cool developer.cool is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 3 developer.cool User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 m 5 sec
Reputation Power: 0
Exclamation Binary Number Encryption/ Decryption in C++

I have a program in which there are some bugs. It would be very nice if someone could help me out. I have gicen the details of the program along with the code.


Write a C++ program that can read, compress (encode) and decompress (decode) binary data. The program should display a menu with the following options:
a) Read Data
b) Compress Data
c) Decompress Data
Read Data
Binary data (0/1) should be read from the keyboard and stored in an array. The number of bits entered from the keyboard should be equal to the sum of last 5 digits in your student ID number.
For example:
If your id number is S00453345, the sum of last 5 digits is 5+3+3+4+5=20. So 20 digits (eg. 00110000011111110001) should be entered from the keyboard, read and stored in an array for compression.
Compress Data:
To compress/encode data, you should recognise repeated digits and replace the runs of repetition with a number representing the repetition length.
For example:
000011100011000111100000 can be written as 4332345 because there are 4 zeros, 3 ones, 3 zeros, 2 ones, 3 zeros, 4 ones and 5 zeros.
Decompression Data:
To decompress/decode data, you should use the reverse order. For example: 3425 can be written as 00011110011111.
The whole program should work in a loop to enable the user to read, compress and decompress data until the user chooses to exit.


Code:
 #include <iostream.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <process.h>	// for exit()
 #include <string.h>
 #include<conio.h>
 
 class data
 {
 	private:
 		char sid[20],bin[50];
 		int encr[50],decr[50];
 	
 	public:
 		int sum,ctr,bin_done; /*  Sum stores the number of binary values to get and ctr stores the index of encr[]  */
 		data();
 		void inputsid(); /* To input the Student ID */
 		void computenum(); /* To find the sum of last 5 digits of the Student Id */
 		void menu(); /* To print the menu */
 		void inputbin(); /* To input the Binary data */
 		void encrdata();  /* To encrypt the binary data */
 		void decrdata();  /* To decrypt the binary data */
 
 };
 		// Constructor to reset the counters
 		data::data()
 		{
 			sum=0;
 			ctr=0;
 			bin_done=0;
 		}
 //	  To Get the Student Id
 	   void data::inputsid()
 	   {
 			clrscr();
 			cout<<"Enter your Student ID Number: ";
 			gets(sid);
 			computenum();
 	   }
 
 //	  To compute the num of binary digits to enter
 	   void data::computenum()
 	   {
 			int num,s=0;
 			sum=0;
 			num=strlen(sid);
 			char str[6];
 			for(int i=num-5;i<=num;i++)
 			{
 				str[s]=sid[i];
 				s++;
 			}
 			int tmp=atoi(str);
 			while(tmp!=0)
 			{
 				sum+=tmp%10;
 				tmp/=10;
 			}
 		}
 
 //	  To print the main menu
 	   void data::menu()
 	   {
 //		 clrscr();
 		 cout<<"\n\n\n\n	 MENU\n";
 		 cout<<"1. Enter Data\n";
 		 cout<<"2. Encrypt Data\n";
 		 cout<<"3. Decrypt Data\n";
 		 cout<<"4. Exit\n";
 	   }
 
 	   //	  To get the binary Data
 	   void data::inputbin()
 	   {
 		 cout<<"Enter "<<sum<<" binary digits one character at a time: ";
 		 char tmp;
 		 for(int i=0;i<sum;)
 		 {
 		  cout<<"\nEnter character no "<<i+1<<":";
 		  cin>>tmp;
 		   if(tmp=='0' || tmp=='1')
 		   {
 			   bin[i]=tmp;
 			   i++;
 		   }
 		   else
 		   {
 			   cout<<tmp<<"\nInacceptable Value. Please enter 1 or 0 only";
 		   }
 		 }
 		 bin_done=1;
 		}
 
 //	  To Encrypt the data
 void data::encrdata()
 {
 	ctr=0;
 	int counter=0;
 	for(int i=0;i<sum;i++)
 	{
 		if(i==0)
 		{
 			if(bin[i]=='1')
 			{
 				encr[ctr]=counter;
 				ctr++;
 				counter++;
 			}
 			else
 			{
 				counter++;
 			}
 		}
 		else
 		{
 			if(bin[i]==bin[i-1])
 			{
 				counter++;
 			}
 			else
 			{
 				encr[ctr]=counter;
 				ctr++;
 				counter=1;
 			}
 		}
 	}
 	encr[ctr]=counter;
 	ctr++;
 	cout<<"\nThe Encrypted data is"<<ctr<<" : ";
 	for(i=0;i<ctr;i++)
 	{
 		cout<<encr[i];
 	}
 }
 
 //	  To Decrypt the data
 	   void data::decrdata()
 	   {
 		//int j=0;
 		 int val=0;
 
 		 cout<<"\nDecrypted data is: ";
 		 for(int i=0;i<ctr;i++)
 		 {
 		   for(int k=0;k<encr[i];k++)
 		   {
 		  //	 decr[j]=val;
 			   cout<<val;
 			//  j++;
 		   }
 		   if(val==0)
 		   {
 			   val=1;
 		   }
 		   else
 		   {
 			   val=0;
 		   }
 		 }
 	}
 
 void main()
 {
  data d;	/* Creating an object of the data class */
  d.sum=0;
  d.ctr=0;
  d.bin_done=0;
  char ch;
  clrscr();
  d.inputsid();
   
  do
  {
 	d.menu();
 	cout<<"\nENTER YOUR CHOICE:  ";
 	cin>>ch;
 	switch (ch)
 	{
 		case '1':
 		{
 			d.inputbin();
 			break;
 		}
 		case '2':
 		{
 			if(d.bin_done==0)
 			{
 				cout<<"Please enter the binary data first.";
 				getch();
 				break;
 			}
 			else
 			{			
 				d.encrdata();
 				break;
 			}
 		}
 		case '3':
 		{
 			if(d.ctr==0)
 			{
 				cout<<"Please encrypt the binary data first.";
 				getch();
 				break;
 			}
 			else
 			{
 				d.decrdata();
 				break;
 			}
 		}
 		case '4':
 		{
 			exit(0);
 			break;
 		}
 		default:
 			cout<<"Invalid choice";
 	}
 	getch();
   }while(1);
 }
 

Reply With Quote
  #2  
Old May 15th, 2005, 07:16 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 8 m 6 sec
Reputation Power: 4
what specifically is your problem?
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips.



Reply With Quote
  #3  
Old May 17th, 2005, 11:05 AM
developer.cool developer.cool is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 3 developer.cool User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 m 5 sec
Reputation Power: 0
Exclamation Basic Problem

Quote:
Originally Posted by B-Con
what specifically is your problem?

Write a C++ program that can read, compress (encode) and decompress (decode) binary data. The program should display a menu with the following options:
a) Read Data
b) Compress Data
c) Decompress Data
Read Data
Binary data (0/1) should be read from the keyboard and stored in an array. The number of bits entered from the keyboard should be equal to the sum of last 5 digits in your student ID number.
For example:
If your id number is S00453345, the sum of last 5 digits is 5+3+3+4+5=20. So 20 digits (eg. 00110000011111110001) should be entered from the keyboard, read and stored in an array for compression.
Compress Data:
To compress/encode data, you should recognise repeated digits and replace the runs of repetition with a number representing the repetition length.
For example:
000011100011000111100000 can be written as 4332345 because there are 4 zeros, 3 ones, 3 zeros, 2 ones, 3 zeros, 4 ones and 5 zeros.
Decompression Data:
To decompress/decode data, you should use the reverse order. For example: 3425 can be written as 00011110011111.
The whole program should work in a loop to enable the user to read, compress and decompress data until the user chooses to exit.

Reply With Quote
  #4  
Old May 17th, 2005, 06:05 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
well, it doesn't do any good to repeat your OP. To rephrase B-Con's post:

What are you having trouble with? What, specifically, don't you understand or want our help with?

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Binary Number Encryption/ Decryption in C++


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT