1,105,288 Community Members

Encrypt/Decrypt a string

Member Avatar
Koldsoul
Light Poster
39 posts since Oct 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Summary of task: I have to write a program that adds 3 places to each char of a user input string to encrypt and vice versa for decrypting. Doesn't seem to hard, but as a beginner I am having an issue. I am having problems getting the user input string to convert so that I can add or subtract three places from it. So if the letter is A it should come out a D if encrypted. The error I am getting when compiled is

Encryption2.cpp:54: no match for `string & - char'

Here is my source code for this program.

//Encryption/decryption program that encrypts or decrypts
//user input as a string.

#include <iostream>
#include <string>

string encrypt(string text, string message);
string decrypt(string text, string message);

using namespace std;


char choice;

int main()


    {


//Ask user to encrypt or decrypt

        cout << "Would you like to encrypt or decrypt?" << endl; 
        cout << "Enter E for encrypt or D for decrypt." << endl;
              cin >> choice;


  if (choice == 'E')
  {

        encrypt;

  }     
 else if (choice == 'D')
  {
        decrypt;
  }   

              return 0;
}

//Encrypt the text
string encrypt(string text, string message)

        {
        int i;
    string temp;


        cout << "Enter the text you wish to encrypt." << endl;
        cin >> text;


        for (i = 0; i < message.length(); ++i)
      temp = 3 + (text - '0') << endl;

        cout << "Encrypted text is " << temp << endl;

        }

//Decrypt the text
string decrypt(string text, string message)

        {
        int i;
    string temp;


        cout << "Enter the text you wish to decrypt." << endl;
        cin >> text;


        for (i = 0; i < message.length(); ++i)
      temp = 3 + (text - '0') << endl;

        cout << "Decrypted text is " << temp << endl;

        }
Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 80 [?]
Team Colleague
 
1
 

The problem you're asking about is (text - '0') , which doesn't work because text is a string and not a character. You probably meant (text[i] - '0') . However, that's not going to be of much help because the rest of your program is horribly broken. Here are some examples:

>string encrypt(string text, string message);
>string decrypt(string text, string message);
This won't compile because you don't say using namespace std; until after the prototypes, yet the prototypes use an unqualified name that's in the std namespace.

>encrypt;
When calling a function, you must provide the argument list.

>temp = 3 + (text - '0') << endl;
You really don't want to use << here, and endl is nonsensical.

Member Avatar
Koldsoul
Light Poster
39 posts since Oct 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks Narue, like I said, new to this C++ language and still figuring it all out. I appreciate you pointing out my errors other than what I already knew. I have fixed all of it, the program compiles now fine, runs but doesn't output any text where it should. The function is now how I have it below. Not sure why it is not outputting anything. It does output the "Encrypted text is " and "Decrypted text is " lines but nothing afterwards and is compiling with no errors.

//Encrypt the text
string encrypt(string text, string message)

        {
        int i;
    string temp;


        cout << "Enter the text you wish to encrypt." << endl;
        cin >> text;


        for (i = 0; i < message.length(); ++i)
      temp = 3 + (text[i] - '0') ;

        cout << "Encrypted text is " << temp << endl;

        }

//Decrypt the text
string decrypt(string text, string message)

        {
        int i;
    string temp;


        cout << "Enter the text you wish to decrypt." << endl;
        cin >> text;


        for (i = 0; i < message.length(); ++i)
      temp = 3 + (text[i] - '0');

        cout << "Decrypted text is " << temp << endl;
        }
Member Avatar
papanyquiL
Junior Poster
168 posts since May 2009
Reputation Points: 45 [?]
Q&As Helped to Solve: 19 [?]
Skill Endorsements: 0 [?]
 
0
 

You need to declare what 'text' is. As in

char text;
Member Avatar
NathanOliver
Posting Virtuoso
1,657 posts since Apr 2009
Reputation Points: 284 [?]
Q&As Helped to Solve: 310 [?]
Skill Endorsements: 4 [?]
 
0
 

the problem is that when you say temp = 3 + (text[i] - '0'); in your for loop you are constantly setting the string to just one character. to build the string i would either use temp += 3 + (text[i] - '0'); or temp.append(3 + (text[i] - '0')); that will at least get the entire string

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article