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;

Recommended Answers

All 9 Replies

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

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

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 to equal in.

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:)

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.

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.

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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.