954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Cesar Cipher in c++

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
Newbie Poster
13 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

use getline() rather than >> to accept input into in.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

Yeah I tried that and I get the same result wont encrypt anything after a space.

KenTheFurry
Newbie Poster
13 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 
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].

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

1)cin reads a array of char until a delimitter like or 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:)

Prabakar
Posting Whiz
342 posts since May 2008
Reputation Points: 94
Solved Threads: 33
 

1)cin reads a array of char until a delimitter like or 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
Newbie Poster
13 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

I don't understand why getline() did not work.
Here is a code that uses getline as lener suggested.

#include <iostream>

#define isupper(a) ((a)>= 'A' && (a)<= 'Z') 
#define islower(a) ((a)>= 'a' && (a)<= 'z') 

using namespace std ;

int main()
{
    char PlainText[100], CipherText[100] ;
    cin.getline(PlainText, 100) ; 
    for ( int i = 0 ; PlainText[i] ; i++ )
    {
        if ( isupper(PlainText[i]) )
            CipherText[i] = 25 - (PlainText[i] - 'A') + 'A' ;
        else if ( islower(PlainText[i]) )
            CipherText[i] = 25 - (PlainText[i] - 'a') + 'a' ;
        else CipherText[i] = PlainText[i] ;
    
    }
    cout << CipherText ;
    cin.get();
    return 0 ;
}


EDIT:

string class could be used rather than array of char.

Prabakar
Posting Whiz
342 posts since May 2008
Reputation Points: 94
Solved Threads: 33
 

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.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

I still believe your first suggestion of getline() is better

Prabakar
Posting Whiz
342 posts since May 2008
Reputation Points: 94
Solved Threads: 33
 

I don't understand why getline() did not work. Here is a code that uses getline as lener suggested.

#include <iostream>

#define isupper(a) ((a)>= 'A' && (a)<= 'Z') 
#define islower(a) ((a)>= 'a' && (a)<= 'z') 

using namespace std ;

int main()
{
    char PlainText[100], CipherText[100] ;
    cin.getline(PlainText, 100) ; 
    for ( int i = 0 ; PlainText[i] ; i++ )
    {
        if ( isupper(PlainText[i]) )
            CipherText[i] = 25 - (PlainText[i] - 'A') + 'A' ;
        else if ( islower(PlainText[i]) )
            CipherText[i] = 25 - (PlainText[i] - 'a') + 'a' ;
        else CipherText[i] = PlainText[i] ;
    
    }
    cout << CipherText ;
    cin.get();
    return 0 ;
}

EDIT:

string class could be used rather than array of char.


Hmm I guess I will use something like that.

KenTheFurry
Newbie Poster
13 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You