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;

        }

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.

commented: Great feedback. Appreciate the great knowledge. +1

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;
        }

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

char text;

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.