5 Replies - 1092 Views - Last Post: 31 January 2013 - 07:47 AM Rate Topic: -----

#1 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Trying to do an affine cipher in c++

Posted 28 January 2013 - 11:30 PM

I'm in the process of writing an affine cipher code that uses the ax+b form, and I found it is 5x+8. I'm attaching my code. The comments are me commenting out what I was testing and what have you. I thought I was on the right track but then when I compile it, it comes up with three blocks on top of each other, with four numbers in each block? What is going on? I'm attaching a screen shot file of it since I can't copy and paste it. Here's the code too:
#include<iostream>
#include<string>
#include<cctype>

using std::cin;
using std::cout;
using std::endl;
using std::string;

int main()
{
        string answer;
        cout << "Enter a word: ";
        cin >> answer;
        int length = answer.length();
        for (int count = 0; count < length ; count++){
                if (isalpha(answer[count])){
                        answer[count] = tolower(answer[count]);
                        //for (unsigned s = 0; s < answer.length(); s++){
                                answer.at(count)= 5*count+8;
                                cout << answer.at(count) << endl;
                        }
                        //for (int i = 0; i < 25; i++){
                        //      if (answer[count] == 'z')
                        //              answer[count] == 'a';
                        //      else
                        //              answer[count]++;
                        //}
                }

        //cout << answer;

        //for (unsigned i = 0; i < answer.length();++i)
        //      cout << answer.at(i);
        //cout << answer.at(3) << endl; 
        //cin >> a;
        //a = toupper(a);
        //cout << a;

        return 0;

}




I want to know why it's showing the blocks. But also, maybe, if possible, some help, on printing out the letter in cipher code (5x+8) if I'm not doing it right?

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to do an affine cipher in c++

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,290
  • Joined: 25-December 09

Re: Trying to do an affine cipher in c++

Posted 29 January 2013 - 05:35 AM

Quote

I want to know why it's showing the blocks

Probably because the "character" doesn't exist in you character set. Remember a character can only hold the value of 0 - 127, on most machines. Remember each element of a std::string is a char. Also the first 32 characters are non-printing characters. You may want to study the ASCII Table.

By the way, why are you using count instead of the actual character? You also seem to be missing several key parts of the cipher. You may want to revisit your documentation for this cipher.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: Trying to do an affine cipher in c++

Posted 30 January 2013 - 11:29 PM

#include<iostream>
#include<string>
#include<cctype>

using std::cin;
using std::cout;
using std::endl;
using std::string;

int main()
{
        string answer;
        cout << "Enter a word: ";
        cin >> answer;
        int length = answer.length();
        for (int count = 0; count < length ; count++){
                if (isalpha(answer[count])){
                        answer[count] = tolower(answer[count]);
                        for (int i = 0; i < answer.length(); i++){
                                int affine = (5*count+8) % 26;
                                answer.at(count)= affine;
                                if (answer.at(count) == 'z')
                                        answer.at(count) == 'a';
                                else
                                        answer.at(count)++;
                        }
                }
                cout << answer;
        }
        return 0;
}



View PostZacCarlson, on 31 January 2013 - 06:25 AM, said:

#include<iostream>
#include<string>
#include<cctype>

using std::cin;
using std::cout;
using std::endl;
using std::string;

int main()
{
        string answer;
        cout << "Enter a word: ";
        cin >> answer;
        int length = answer.length();
        for (int count = 0; count < length ; count++){
                if (isalpha(answer[count])){
                        answer[count] = tolower(answer[count]);
                        for (int i = 0; i < answer.length(); i++){
                                int affine = (5*count+8) % 26;
                                answer.at(count)= affine;
                                if (answer.at(count) == 'z')
                                        answer.at(count) == 'a';
                                else
                                        answer.at(count)++;
                        }
                }
                cout << answer;
        }
        return 0;
}


I did this first because I just tried to post this with comments on top and it didn't work. But I'm trying to single out the characters then cipher them using 5x+8. If it's in the count portion, please tell me what it is exactly I'm doing wrong. I did change the code so I thin it goes from z to a when it overflows? I also used the correct cipher (mod 26). I put hello and it prints spaces, then the word ello then spaces then llo then spaces then goes to the blocks with numbers in it on top of lo, all the way to o.
Was This Post Helpful? 0
  • +
  • -

#4 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: Trying to do an affine cipher in c++

Posted 31 January 2013 - 12:09 AM

I've changed the == 'a' and z to just one = and that helped kind of. I'm still getting the wrong answer. I type in just h, and it puts in a. I type in just e and it gives me a. So the problem is the calculation......?
Was This Post Helpful? 0
  • +
  • -

#5 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: Trying to do an affine cipher in c++

Posted 31 January 2013 - 12:16 AM

Sorry, but I just figured something more out. Now I'm almost positive it's with my calculation or maybe the 'a' and 'z' loop. Here's the new code: Don't bother with the // as I was testing to see if it helped and it did:

include<iostream>
#include<string>
#include<cctype>

using std::cin;
using std::cout;
using std::endl;
using std::string;

int main()
{
        string answer;
        cout << "Enter a word: ";
        cin >> answer;
        int length = answer.length();
        for (int count = 0; count < length ; count++){
                if (isalpha(answer[count])){
                        answer[count] = tolower(answer[count]);
                        //for (int i = 0; i < answer.length(); i++){
                                int affine = 5*count+8 % 26;
                                answer.at(count)= affine;
                                if (answer.at(count) = 'z')
                                        answer.at(count) = 'a';
                                else
                                        answer.at(count)++;
                        }
                }
                cout << answer;
        return 0;
}




I've changed it again and it does help more. I think the problem is with the calculation and the 'a' 'z' stuff maybe:

include<iostream>
#include<string>
#include<cctype>

using std::cin;
using std::cout;
using std::endl;
using std::string;

int main()
{
        string answer;
        cout << "Enter a word: ";
        cin >> answer;
        int length = answer.length();
        for (int count = 0; count < length ; count++){
                if (isalpha(answer[count])){
                        answer[count] = tolower(answer[count]);
                        //for (int i = 0; i < answer.length(); i++){
                                int affine = 5*count+8 % 26;
                                answer.at(count)= affine;
                                if (answer.at(count) = 'z')
                                        answer.at(count) = 'a';
                                else
                                        answer.at(count)++;
                        }
                }
                cout << answer;
        return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,290
  • Joined: 25-December 09

Re: Trying to do an affine cipher in c++

Posted 31 January 2013 - 07:47 AM

There are several problems with your code.

First remember that the operator= is the assignment operator, operator== is for comparisons. Look closely at your if statement, you are using the wrong operator. Also note that that if/else clause is really not needed at all.

Next for your cipher you want your "alphabet" to be zero based, the ASCII code for 'a' is 97, not zero. So you need to correct for this fact in your calculation.

Next you have an operator precedence issue. The operator% has a higher precedence than the addition operator so your calculation:
  int affine = 5*count+8 % 26;

Actually translates to:
  int affine = 5*count+(8 % 26);

Not what you want, you need to use parentheses to fix this problem.
  int affine = (5*count+8) % 26);


Lastly and probably the biggest problem is that you seem to think that count represents the character, it doesn't. It is being used as the index value, the location within your string, not the value of the character. You need to use the actual character value, not it's location.


Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1