Caesar Cipher

Hello! I'm making a program that read Caesar Cipher. Everything works fine, but the program stops if it goes to the next line.
Here is my code so far:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
string word;
getline(cin,word);
for (int i = 0; i <= word.length(); i++) {
if (word[i] >= 'M' && word[i] <= 'Z') {
word[i] = word[i] - 12;
}
else if (word[i] >= 'A' && word[i] <= 'L') {
word[i] = word[i] + 14;
}
else if (word[i] >= 'm' && word[i] <= 'z') {
word[i] = word[i] - 12;
}
else if (word[i] >= 'a' && word[i] <= 'l') {
word[i] = word[i] + 14;
}
else if (word[i] >= '0' && word[i] <= '9') {
word[i] = word[i];
}
else if (word[i] == ',' || word[i] == '.' || word[i] == ':') {
word[i] = word[i];
}
}
cout << word;

getchar();
getchar();
return 0;
}

For example, if I enter
Sqadsum
Ngffaz Siuzzqff, Xkymz Tmxx, Sqadsq Imxfaz

It stops at: Georgia
Any suggestions?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
getline(cin,word);
for (int i = 0; i <= word.length(); i++) {
if (word[i] >= 'M' && word[i] <= 'Z') {
word[i] = word[i] - 12;
}
else if (word[i] >= 'A' && word[i] <= 'L') {
word[i] = word[i] + 14;
}
else if (word[i] >= 'm' && word[i] <= 'z') {
word[i] = word[i] - 12;
}
else if (word[i] >= 'a' && word[i] <= 'l') {
word[i] = word[i] + 14;
}
else if (word[i] >= '0' && word[i] <= '9') {
word[i] = word[i];
}
else if (word[i] == ',' || word[i] == '.' || word[i] == ':') {
word[i] = word[i];
}
}
cout << word;


It should be :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
while(getline(cin,word))
{
for (int i = 0; i <= word.length(); i++) {
if (word[i] >= 'M' && word[i] <= 'Z') {
word[i] = word[i] - 12;
}
else if (word[i] >= 'A' && word[i] <= 'L') {
word[i] = word[i] + 14;
}
else if (word[i] >= 'm' && word[i] <= 'z') {
word[i] = word[i] - 12;
}
else if (word[i] >= 'a' && word[i] <= 'l') {
word[i] = word[i] + 14;
}
else if (word[i] >= '0' && word[i] <= '9') {
word[i] = word[i];
}
else if (word[i] == ',' || word[i] == '.' || word[i] == ':') {
word[i] = word[i];
}
}
cout << word;
}


Press Ctrl+Z to end the program.
It works! Thank you.
However, is there anyway that I can output the same format with the input. I've tested the program and I have to press enter when move to another line?
try maybe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
 

#include <string>
#include <cstring>
#include <iostream>

using namespace std;

Caesar :: Caesar()
{
	shift = 0;
}


void Caesar::setShift(int value)
{
	bool stat = false;
	while(!stat)
	{
		try
		{
			if(value<=0)
			{
				string f="Please provide a positive shift value";
				throw f;
			}
			else 
				if(value>94)
				{
					string b="Maximum shift is 94 for ASSCII, please try again";
					throw b;
				}
				else
				{
					shift=value;
					stat=true;
				}
		}
		catch (string e)
		{
			cout<<e<<endl;
			cin >>value;
		}
	}
	
}

 char Caesar::encodeChar(char chara)
{
	setShift(shift);
	for(int i =0; i<shift; i++)
	{
			if(chara == 126)
			{
				chara= 32;
				
			}
			else
			{
				chara++;
				
			}
			
	}
	return chara;
}


 char Caesar::decodeChar(char chara)
{
	
	setShift(shift);
	
	
	for(int i =0; i<shift; i++)
	{
			if(chara == 32)
			{
				chara= 126;
				
			}
			else
			{
				chara--;
				
			}
			
	}
	
	return chara;
}
if you want to take it further
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
 
just make the appropriate header
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;

OneTimePad::OneTimePad(long int ting)
{
	seed = ting;
	srand(seed);
}

void OneTimePad:: setSeed(long int i)
{
	seed =i;
	srand(seed);
}

char OneTimePad::encodeChar(char thing)
{
	setShift( (rand() % (94 - 1 +1))  +1 );
	return Caesar :: encodeChar(thing);
}

char OneTimePad::decodeChar(char text)
{
	setShift( (rand() % (94 - 1 +1)) +1 );
	return Caesar::decodeChar(text);
}

string OneTimePad :: encode(string UGE)
{
	
	int len;
	string word ="";
	len = UGE.length();
	srand(seed);
	for(int i=0; i< len; i++)
	{
		word += encodeChar(UGE[i] );
	}
	return word;
}
string OneTimePad:: decode(string UGE)
{
	
	int len;
	string word ="";
	len = UGE.length();
	srand(seed);
	for(int i=0; i< len; i++)
	{
		
		word +=decodeChar(UGE[i] );
	}
	return word;
}



edited 16/11/16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <string>
#include <cstring>

using namespace std;

string SubstitutionCipher :: encode(string ting)
{
	int length;
	string word ="";
	length = ting.length();
	
	for(int i=0; i< length; i++)
	{
		word+=encodeChar(ting[i] );
	}
	return word;
}


string SubstitutionCipher:: decode(string ting)
{
	int length;
	string word ="";
	length = ting.length();
	
	for(int i=0; i< length; i++)
	{
		word+=decodeChar(ting[i] );
	}
	return word;
}


and a main cipher header to put it all together

1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef  CIPHER_H
#define CIPHER_H
#include <string>
#include<vector>
using namespace std;
class Cipher
{
	public:
		virtual string encode(string) = 0;
		virtual string decode(string) = 0;
};
#endif
		
go another step further
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
#include<string>
#include"RowsColumns.h"
using namespace std;

string RowsColumns::encode(string ting)
{
	int size=calculateSide(ting.length());
	char** matrix=createMatrix(size);
	fillMatrix(matrix,size,ting);
	string that;
	
	for(int i=0;i<size;i++)
	{
		for(int x=0;x<size;x++)
		{

				that+=matrix[x][i];
				
			
		}
	}
	deleteMatrix(matrix,size);
	return that;
	
}
	  string RowsColumns::decode(string ting) 
{
	
		int size=calculateSide(ting.length());
		if(ting.length()<(size*size))
		{
			return "Incompatible text length";
			
		}
		int count=0;
		char** matrix=createMatrix(size);
		fillMatrix(matrix,size,ting);
		string that;
		for(int i=0;i<size;i++)
		{
			for(int x=0;x<size;x++)
			{
				if(count!=ting.length())
				{
					that+=matrix[x][i];
					count ++;
				}
			}
		}
		deleteMatrix(matrix,size);
		return that;


}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

#include<string>
#include"RowsColumns.h"
#include"ZigZag.h"

using namespace std;

string ZigZag::encode(string ting)
{
	int size=calculateSide(ting.length());
	char** matrix=createMatrix(size);
	fillMatrix(matrix,size,ting);
	string that;
	int count =0;
	bool upDown=false;
	for(int i=0;i<size;i++)
	{
		
			if(!upDown)
			{
				for(int x=0;x<size;x++)
				{
					that+=matrix[x][i];
					count++;
				}
				upDown=true;
			}
			else
			{
				for(int x=size-1;x>=0;x--)
				{
					that+=matrix[x][i];
					
				}
				upDown=false;
			}
			
		
		
	}
	deleteMatrix(matrix,size);
	return that;
	
}
	  string ZigZag::decode(string ting) 
{
	int size=calculateSide(ting.length());
	if(ting.length()<(size*size))
	{
		throw "Incompatible text length";
		
	}
	int count =0;
	char** matrix=createMatrix(size);
	fillMatrix(matrix,size,ting);
	string that;
	bool upDown=false;
	/*if(!upDown)
	{
		for(int i=0;i<size;i++)
		{
			for(int x=0;x<size;x++)
			{
				that+=matrix[i][x];
			}
		}
		upDown=true;
	}
	else
	{
		for(int i=0;i<size;i++)
		{
			for(int x=size-1;x>=0;x--)
			{
				that+=matrix[i][x];
			}
		}
		upDown=false;
	}*/
	for(int i=0;i<size;i++)
	{
		if(count!=ting.length())
		{
		if(!upDown)
		{
			for(int x=0;x<size;x++)
			{
				that+=matrix[i][x];
				count++;
			}
			upDown=true;
		}
		else
		{
			for(int x=size-1;x>=0;x--)
			{
				that+=matrix[i][x];
				count++;
			}
			upDown=false;
		}
		
		}
		
		
	}
	deleteMatrix(matrix,size);
	RowsColumns dec;
	string UGE=dec.decode( that);
	return UGE;

}

just dont forget the appropriate headers
Topic archived. No new replies allowed.