RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 1051 | Replies: 9 | Solved | Thread Tools  Display Modes
Reply
Join Date: Apr 2008
Posts: 12
Reputation: KenTheFurry is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
KenTheFurry's Avatar
KenTheFurry KenTheFurry is offline Offline
Newbie Poster

Cesar Cipher in c++

  #1  
Jul 20th, 2008
Well I am writing a little message encryption program (simple) and Well it works but it will not change anything after a space. just hoping someone here could help.

	cout << "Enter in the message you want to encrypt..." << endl << "		";
	cin >> in;
	count=strlen(in);
	for(i = 0; i <= count; i++){
		if(in[i]=='a'){
			out[i]='z';
		}if(in[i]=='b'){
			out[i]='y';
		}if(in[i]=='c'){
			out[i]='x';
		}if(in[i]=='d'){
			out[i]='w';
		}if(in[i]=='e'){
			out[i]='v';
		}if(in[i]=='f'){
			out[i]='u';
		}if(in[i]=='g'){
			out[i]='t';
		}if(in[i]=='h'){
			out[i]='s';
		}if(in[i]=='i'){
			out[i]='r';
		}if(in[i]=='j'){
			out[i]='q';
		}if(in[i]=='k'){
			out[i]='p';
		}if(in[i]=='l'){
			out[i]='o';
		}if(in[i]=='m'){
			out[i]='n';
		}if(in[i]=='n'){
			out[i]='m';
		}if(in[i]=='o'){
			out[i]='l';
		}if(in[i]=='p'){
			out[i]='k';
		}if(in[i]=='q'){
			out[i]='j';
		}if(in[i]=='r'){
			out[i]='i';
		}if(in[i]=='s'){
			out[i]='h';
		}if(in[i]=='t'){
			out[i]='g';
		}if(in[i]=='u'){
			out[i]='f';
		}if(in[i]=='v'){
			out[i]='e';
		}if(in[i]=='w'){
			out[i]='d';
		}if(in[i]=='x'){
			out[i]='c';
		}if(in[i]=='y'){
			out[i]='b';
		}if(in[i]=='z'){
			out[i]='a';
		}
	}
	cout << "The encoded message is..." << endl << out << endl;
	cin.get();
	pause();
	goto start;
-KenTheFurry
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Posts: 1,395
Reputation: Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice 
Rep Power: 10
Solved Threads: 196
Lerner Lerner is offline Offline
Nearly a Posting Virtuoso

Re: Cesar Cipher in c++

  #2  
Jul 20th, 2008
use getline() rather than >> to accept input into in.
Last edited by Lerner : Jul 20th, 2008 at 11:35 pm.
Reply With Quote  
Join Date: Apr 2008
Posts: 12
Reputation: KenTheFurry is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
KenTheFurry's Avatar
KenTheFurry KenTheFurry is offline Offline
Newbie Poster

Re: Cesar Cipher in c++

  #3  
Jul 21st, 2008
Yeah I tried that and I get the same result wont encrypt anything after a space.
-KenTheFurry
Reply With Quote  
Join Date: Jan 2008
Posts: 2,134
Reputation: VernonDozier is a glorious beacon of light VernonDozier is a glorious beacon of light VernonDozier is a glorious beacon of light VernonDozier is a glorious beacon of light VernonDozier is a glorious beacon of light VernonDozier is a glorious beacon of light 
Rep Power: 11
Solved Threads: 258
VernonDozier VernonDozier is offline Offline
Postaholic

Re: Cesar Cipher in c++

  #4  
Jul 21st, 2008
Originally Posted by KenTheFurry View Post
Yeah I tried that and I get the same result wont encrypt anything after a space.


I would guess then that the problem is that the out array is not initialized correctly. I see nowhere where you strcpy the in array to the out array before going through the loop. If you initialize out beforehand as all NULL, the first character that is not 'a' through 'z' is going to end the out string. I think you should either do a deep copy of the in array to the out array after getting a value for in and before the loop starts, or better yet, change all of those if statements to "else if" statements and then add an "else" branch for when characters (like spaces) aren't 'a' through 'z'. In that case you should assign out[i] to equal in[i].
Last edited by VernonDozier : Jul 21st, 2008 at 12:24 am.
Reply With Quote  
Join Date: May 2008
Location: Infinite Castle, below Beltline
Posts: 307
Reputation: Prabakar is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 27
Prabakar's Avatar
Prabakar Prabakar is offline Offline
Posting Whiz

Re: Cesar Cipher in c++

  #5  
Jul 21st, 2008
1)cin reads a array of char until a delimitter like <space> or <new line> is encountered. Thats why the program might have stoped after chaging the first letter.

2)The converstion you've made could be done by math exprestions rather than that many if struct.

Hope this helps
Reply With Quote  
Join Date: Apr 2008
Posts: 12
Reputation: KenTheFurry is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
KenTheFurry's Avatar
KenTheFurry KenTheFurry is offline Offline
Newbie Poster

Re: Cesar Cipher in c++

  #6  
Jul 21st, 2008
Originally Posted by Prabakar View Post
1)cin reads a array of char until a delimitter like <space> or <new line> is encountered. Thats why the program might have stoped after chaging the first letter.

2)The converstion you've made could be done by math exprestions rather than that many if struct.

Hope this helps

Oh no it will encrypt anything as long as their is not a space in it so it could goto 99 chars until it will stop encrypting.
-KenTheFurry
Reply With Quote  
Join Date: May 2008
Location: Infinite Castle, below Beltline
Posts: 307
Reputation: Prabakar is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 27
Prabakar's Avatar
Prabakar Prabakar is offline Offline
Posting Whiz

Re: Cesar Cipher in c++

  #7  
Jul 21st, 2008
I don't understand why getline() did not work.
Here is a code that uses getline as lener suggested.
  1. #include <iostream>
  2.  
  3. #define isupper(a) ((a)>= 'A' && (a)<= 'Z')
  4. #define islower(a) ((a)>= 'a' && (a)<= 'z')
  5.  
  6. using namespace std ;
  7.  
  8. int main()
  9. {
  10. char PlainText[100], CipherText[100] ;
  11. cin.getline(PlainText, 100) ;
  12. for ( int i = 0 ; PlainText[i] ; i++ )
  13. {
  14. if ( isupper(PlainText[i]) )
  15. CipherText[i] = 25 - (PlainText[i] - 'A') + 'A' ;
  16. else if ( islower(PlainText[i]) )
  17. CipherText[i] = 25 - (PlainText[i] - 'a') + 'a' ;
  18. else CipherText[i] = PlainText[i] ;
  19.  
  20. }
  21. cout << CipherText ;
  22. cin.get();
  23. return 0 ;
  24. }

EDIT:

string class could be used rather than array of char.
Last edited by Prabakar : Jul 21st, 2008 at 12:06 pm.
Reply With Quote  
Join Date: Jul 2005
Posts: 1,395
Reputation: Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice 
Rep Power: 10
Solved Threads: 196
Lerner Lerner is offline Offline
Nearly a Posting Virtuoso

Re: Cesar Cipher in c++

  #8  
Jul 21st, 2008
Place this line:

cout << in << endl;

between these two lines:

cin >> in;
count = strlen(in);

and this line:

cout << count << endl;

after the second line above. If the appropriate results are printed to the screen then add this to the end of the if statements in your code to see if that helps.
else
  out[i] = in[i];

If that doesn't work then post declaration for in and out and provide more code for us to look at.
Reply With Quote  
Join Date: May 2008
Location: Infinite Castle, below Beltline
Posts: 307
Reputation: Prabakar is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 27
Prabakar's Avatar
Prabakar Prabakar is offline Offline
Posting Whiz

Re: Cesar Cipher in c++

  #9  
Jul 21st, 2008
I still believe your first suggestion of getline() is better
Reply With Quote  
Join Date: Apr 2008
Posts: 12
Reputation: KenTheFurry is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
KenTheFurry's Avatar
KenTheFurry KenTheFurry is offline Offline
Newbie Poster

Re: Cesar Cipher in c++

  #10  
Jul 21st, 2008
Originally Posted by Prabakar View Post
I don't understand why getline() did not work.
Here is a code that uses getline as lener suggested.
  1. #include <iostream>
  2.  
  3. #define isupper(a) ((a)>= 'A' && (a)<= 'Z')
  4. #define islower(a) ((a)>= 'a' && (a)<= 'z')
  5.  
  6. using namespace std ;
  7.  
  8. int main()
  9. {
  10. char PlainText[100], CipherText[100] ;
  11. cin.getline(PlainText, 100) ;
  12. for ( int i = 0 ; PlainText[i] ; i++ )
  13. {
  14. if ( isupper(PlainText[i]) )
  15. CipherText[i] = 25 - (PlainText[i] - 'A') + 'A' ;
  16. else if ( islower(PlainText[i]) )
  17. CipherText[i] = 25 - (PlainText[i] - 'a') + 'a' ;
  18. else CipherText[i] = PlainText[i] ;
  19.  
  20. }
  21. cout << CipherText ;
  22. cin.get();
  23. return 0 ;
  24. }

EDIT:

string class could be used rather than array of char.

Hmm I guess I will use something like that.
-KenTheFurry
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the C++ Forum
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 8:48 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC