Caesar cipher execute something weird

Aug 28, 2015 at 10:47pm
I am writing a caesar cipher program. It can compile but it execute something weird. I have no idea what did i do wrong here, because when I test this part
1
2
3
4
5
6
7
8
9
10
11
12
for(int i=0; i<text.size(); i++)
	{
	  cipher[i] = text[i];
	  if(islower(cipher[i]))
	  {
	    cipher[i] = (cipher[i] - 'a' + rotation)%26 + 'a';
	  }
	  else if(isupper(cipher[i]))
	  {
	    cipher[i] = (cipher[i] - 'A' + rotation)%26 + 'A';
	  }
	}


the result was good.
Please help
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
  #include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include "unistd.h"

using namespace std;

// Global settings
struct global_args_t {
    // TODO
} global_args;

void displayUsage(){
	// Displays how to use this program
	// TODO
	cout << "Instruction: \n use -r to give rotation number \n use -f to give file name" <<endl;
}


int main(int argc, char **argv){
	// defaults
  // TODO
  string text;
  char* input_file_name;
  int rotation;
  bool have_rotation = false;
  bool have_input_file_name = false;
  

	// process command-line arguement
	int opt = 0;
	extern char *optarg;
	static const char* opt_string = "r:f:";
	opt = getopt( argc, argv, opt_string);
	while(opt != -1) // While there are parameters to parse, do so
	{  
		switch(opt)
		{
			case 'r':
				// TODO
				have_rotation = true;
				rotation = atoi(optarg);
				break;
			case 'f': 
				// TODO
				have_input_file_name = true;
				input_file_name = optarg;
				break;
			default:
			  displayUsage();
			  return 1;
		}
		opt = getopt( argc, argv, opt_string);	// Pull the next parameter, or 0 if none.
	}
	
	if(!have_rotation)
	{
	  displayUsage();
	  return 0;
	}
	  
	if(have_rotation)
	{
	  if(have_input_file_name)
	  { 	
	    ifstream file(input_file_name);
	    string text2, temp;
	    while(!file.eof())
	    {
	      getline(file, temp);
	      text2 += temp;
	      text2 += "\n";
	    }
	    text = text2[text2.size()-2];
	  }
	  else
	  {
	  cout <<"Enter text:"<<endl;
	  cin >> text;
	  }
	}
	  
	char cipher[sizeof(text)];
	
	for(int i=0; i<text.size(); i++)
	{
	  cipher[i] = text[i];
	  if(islower(cipher[i]))
	  {
	    cipher[i] = (cipher[i] - 'a' + rotation)%26 + 'a';
	  }
	  else if(isupper(cipher[i]))
	  {
	    cipher[i] = (cipher[i] - 'A' + rotation)%26 + 'A';
	  }
	}
	    
	cout <<cipher<<endl;
	  
  return 0;
}
Aug 29, 2015 at 1:17am
It can compile but it execute something weird.

Could use some more info than that.
Also the smaller you can make the test code to show us the easier it is to help you. Debugger helps for that.

char cipher[sizeof(text)];

Just glancing through this probably isn't what you want. text.size() will give you how long the string is, sizeof will just tell you how much memory the string class takes up. Not really sure why you want to use a char array anyway, just stay with using strings?
Aug 29, 2015 at 6:17pm
Here is what I get when I try to encrypt "This is a test sentence!": "Ymnxp���p�����t�������"

if I use string cipher, cout<<cipher<<endl doesnt work. It doesn't print out anything when I run my program.
Aug 29, 2015 at 7:33pm
as James2250 said, your array is too small to hold the sentence, you end writting out of bounds.

Also, you forgot to add the '\0' terminator.


> if I use string cipher
If you have an issue with another code then post that other code. It's hard to guess your mistakes.

If the only change is char cipher[sizeof(text)]; -> string cipher; then that string is empty and trying to access its elements with [] will be out of bounds.
Aug 29, 2015 at 7:54pm
I did some change to my code, and now it works fine if I type the sentence manually. But if I input a text file in, the output file contains nothing. Did I do something wrong to input file?
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
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include "unistd.h"

using namespace std;


void displayUsage(){
	// Displays how to use this program
	// TODO
	cout << "Instruction: \n use -r to give rotation number \n use -f to give file name" <<endl;
}

char caesar(char c, int r)
{
  if(isalpha(c))
  {
    if(islower(c))
    {
      c = (((c-97)+r)%26)+97; // 97 is a value of 'a'
    }
    else if(isupper(c))
    {
      c = (((c-65)+r)%26)+65; // 65 is a value of 'A'
    }
  }
  return c;
}


int main(int argc, char **argv){

  string text;
  char* input_file_name;
  int rotation;
  bool have_rotation = false;
  bool have_input_file_name = false;
  

	// process command-line arguement
	int opt = 0;
	extern char *optarg;
	static const char* opt_string = "r:f:";
	opt = getopt( argc, argv, opt_string);
	while(opt != -1) // While there are parameters to parse, do so
	{  
		switch(opt)
		{
			case 'r':
				have_rotation = true;
				rotation = atoi(optarg);
				break;
			case 'f':
				have_input_file_name = true;
				input_file_name = optarg;
				break;
			default:
			  displayUsage();
			  return 1;
		}
		opt = getopt( argc, argv, opt_string);	// Pull the next parameter, or 0 if none.
	}
	
	if(!have_rotation)
	{
	  displayUsage();
	  return 0;
	}
	  
	if(have_rotation)
	{
	  if(have_input_file_name)
	  { 	
	    ifstream file(input_file_name);
	    string text2, temp;
	    while(!file.eof())
	    {
	      getline(file, temp);
	      text2 += temp;
	      text2 += "\n";
	    }
	    text = text2[text2.size()-2];
	  }
	  else
	  {
	  cout <<"Enter text:"<<endl;
	  getline(cin, text);
	  }
	}
	
	  string output = "";
	  for(int i = 0; i < text.size(); i++)
	  {
	    output += caesar(text[i],rotation);
	  }
	  cout<<output<<endl;
Aug 29, 2015 at 11:14pm
text = text2[text2.size()-2]; //line 85
¿what's that suppossed to do?
Topic archived. No new replies allowed.