need help! trying to create basic encrypting program

Hi there! i'm trying to create a basic encrypting program that will just change letters. example: a to c and so on. I have written the code on to my visual studio 2010 no errors or warning. But when i start the program and type in something, nothing happens heres the code:

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
#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <string>
#include "windows.h"
using namespace std;
void encrypt(string words);
bool tester(int x, int y);
bool testerfor(int x, int y);
int main()
{
	string Emptywords;
	cout << "What shall i encrypt?" << endl;
	cin >> Emptywords;
	encrypt(Emptywords); // calling encryption funtion and passing  user input

}



void encrypt(string words)
{

	char A [100]; //char array for letters that user typed in
	char B [100]; //char array for encrypted letters
string encrypt; //string that will contain in the end the encrypted message

	for(int n = 0; testerfor(n,words.size()); ) // this for loop is going to encrypt the letters 
	{
	    int b = 1;
		A[n] = words[b]; // words[n] = checking for what letters user entered
				switch(A[n]) // switch statement to encrypt the letter that A array has
		{
		case 'a':
			B[n] = 'c'; // putting the encrypted letter inside B array
			break;
		case 'b':
			B[n] = 'd';
			break;
		case 'c':
			B[n] = 'e';
			break;
		case 'd':
			B[n] = 'g';
			break;
		case 'e':
			B[n] = 'j';
			break;
        case 'f':
			B[n] = 'l';
			break;
		case 'g':
			B[n] = 'รถ';
			break;
		case 'h':
			B[n] = 'a';
			break;
		case 'i':
			B[n] = 'k';
			break;
		case 'j':
			B[n] = 'y';
			break;
		}



        stringstream ss; // the part that will but encrypted message inside ecrypt

		ss << B[n];  // be should contain encrypted letter
		if(tester(n,words.size()))
		{
			ss >> encrypt;

		}
        n++; // increasing n by 1
		b++; // increasing b by 1
	}



cout << encrypt << endl; // showing the result to the user
Sleep(1000);


}

bool tester(int x, int y)
{
if (x == y)
{
    return true;
}
return false;
}


bool testerfor(int x, int y)
{
    if (x < y)
{
    return true;
}
return false;
}

after typing in all my letters and pressing enter it should print on the screen the encrypted message but it dosen't.
what am i doing wrong any ideas?
Last edited on
The encrypt function does not return the encrypted string. So the encrpted string isn't returned to the caller, and so it can't display it.
If you're only increasing the character by 2 wouldnt something like this make more sense?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

std::string encrypt(std::string);
int main()
{
	std::string test = "this is a test string";
	std::cout << encrypt(test);
	return 0;
}

std::string encrypt(std::string old)
{
	std::string crypted;
	crypted.resize(old.length());

	for(int i = 0; i < old.length(); ++i)
	{
		//if you want to keep spaces uncomment next line
		//if(old[i] >64 && old[i] < 123)
			crypted[i] = old[i]+2;
	}
	return crypted;
}
Last edited on
thanks for helping me out!
naraku9333 and else I have question:
on naraku's code above he is using std:: why is he not using

using namespace std;
is there a good reason not to use using namespace std; on your programs?
is there a good reason not to use using namespace std; on your programs?


Yes. Try compiling this.

1
2
3
4
5
6
7
8
9
#include <algorithm>
using namespace std;

int count = 0;

int main()
{
    return ++count;
}
thank you all for answering my questions!
naraku9333. Please do not do people's homework. It doesn't really help.
Homework or not, the OP has at least shown some effort. All I did was show him/her a simpler way, whether the OP learns anything from it is up to him/her.
All you did was provide an implementation rather than provide guided help. This isn't a homework service.
kbw. its not an homework...
as soon as I ask a question everybody goes like "its his homework do not help him!"
im only trying to get better at c++ because after learning c++ i will start to learn SDL and opengl.
what the point of this forum if you cant ask a question and only get answers like
"its hes homework! do it yourself!" .. and so on!
I did offer some help, I just didn't write the code for you.
kbw.
sorry...
I didn't notice that...
need to get classes and double check before writing a comment...
sorry...
No problem :)
Topic archived. No new replies allowed.