encryption question

Solved!
Last edited on
How would I make a program that does this assuming that I cant use any arrays?


Why can't you use arrays?

I guess a string would be better anyway:

1
2
3
4
5
string word = "cloud";
for(...)
  word[i] += 5;

cout << word;
I could use arrays but I would like to learn other ways to do something like this.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word = "cloud";
cout << "We are going to encrypt the word cloud. Choose an offset value to encryt it with \n";
// Still not sure what to put to make it actually encrypt it
cout << "Cloud is now encrypted into"" << word << endl;
return 0;
}

Okay so at this point I am just missing one more small piece of my program which is to let the user choose a number to encrypt the word cloud in. I appreciate the help Disch with your example although I am still somewhat confused on it. Theirs no cin to let the user type a number in, and when I tried something like you showed me their was errors.
Theirs no cin to let the user type a number in


I figured you already knew how to do that on your own, so I didn't put it in my example. I just used a literal value of 5.

when I tried something like you showed me their was errors.


I didn't give you actual compilable code, it was just psuedo-code to give you the idea. If you tried to copy/paste it it won't work (that was intentional -- I don't like giving away direct answers).

If you have an individual character, you can add a value to it:

1
2
3
4
char example = 'e';
example += 1;

cout << example;  // prints 'f' 


With a string, you can access each individual characters with the [bracket] operator, just as if it were an array. So make a loop and add whatever encryption value to each character in the string.
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

using namespace std;

int main{
    string word;
    int num;
    int temp

    while(true){
        cout << "Please type a word to encrypt: ";
        cin >> word;
        cout << "Please enter a number to encrypt word by";
        cin >> num;

        for(int i = 0; i < word.size(); i++){ //while i is less than the size of the string +1
        temp = static_cast<int>(word[i]); //turn word position i into a number
        temp =+ num; //add to that number
        word[i] = static_cast<char>(temp); //turn new number back into letter
        }
        cout << word << endl; //output word
    }
}


This'll do the job for my pal.
I started making an encrypter at single words and gradually improved it as i learnt. Now my program is password protected, reads a text document and encrypts the whole thing, then re-writes the edited data into the document.
1
2
3
4
5
        for(int i = 0; i < word.size(); i++){ //while i is less than the size of the string +1
        temp = static_cast<int>(word[i]); //turn word position i into a number
        temp =+ num; //add to that number
        word[i] = static_cast<char>(temp); //turn new number back into letter
        }


Close.

The operator you want is +=, not =+. =+ is being treated as this:

1
2
3
temp = +num;  // ie, assignging temp to equal positive num
// which is basically the same as this:
temp = num;


Also, you don't need to have a temp variable there at all. Save a step and just add to word[i] directly.
Temp is an integer whereas word[i] is a character, you need to get the ascii code to change it and then change that code back into a letter.
And yes i notice the error with =+ now, i just quickly typed the code roughly how it should be (with possible few errors)

P.S. Some people have said to me before just to add onto word[i] but this never works for me and i get lot of compile errors so i use the static_casting method which does the same thing but in a more computer friendly manner apparently.
Last edited on
Temp is an integer whereas word[i] is a character, you need to get the ascii code to change it and then change that code back into a letter.


chars and ints are the same. The only difference is that chars are usually 8 bits wide and ints are usually 32 bits wide. The char is the ascii code. There is no conversion going on when you assign your character to the temp variable -- it's simply moving the ascii code into a larger variable, modifying it, then moving it back to the original variable. You can save 2 steps and simplify it by modifying it in its original variable directly.

Really... this works fine. Try it:
1
2
for(int i = 0; i < word.size(); i++)
    word[i] += num;


P.S. Some people have said to me before just to add onto word[i] but this never works for me and i get lot of compile errors


You must have been doing something else wrong. Modifying a char directly is perfectly legal.
Topic archived. No new replies allowed.