Hi
I am writing a toy encryption method objecy oriented program in which user enters a statement, and each character in the statement is replaced by another character which are stored in a 2D array.

I have created a static array in which each alphabet has a value in 2D array something like this and the program also.... I am having problem in the code with red area...
Actually i am going worng in compairng..Could anyone please help me..

#include<iostream.h>
#include<iomanip.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
const int row =26;
const int col= 2;
const char tarray[row][col]=
{
{'A', '?'},
{'B', '!'},
{'C', '.'},
{'D', ';'},
{'E', ':'},
{'F', '|'},
{'G', '^'},
{'H', ','},
{'I', '%'},
{'J', '$'},
{'K', '>'},
{'L', '<'},
{'M', '='},
{'N', '['},
{'O', ']'},
{'P', '+'},
{'Q', '-'},
{'R', '*'},
{'S', '/'},
{'T', '#'},
{'U', '@'},
{'V', '&'},
{'W', '('},
{'X', ')'},
{'Y', '{'},
{'Z', '}'}
};
class toymethod
{
char string[30];
public:
void encrypt();
void decrypt();
};


void toymethod::encrypt()
{
int k;
cout<<"Enter the string \n";
cin.getline(string,20);do
{
if(strcmp(string,tarray)==0)
cout<<tarray[j];
else
cout<<"Character not found \n";
}while(i<26);
}void toymethod::decrypt()
{
}


void main()
{


toymethod t1;
t1.encrypt();


}

Please help me...THanks in advance...Bye

strcmp takes two char*'s as input, but tarray is a 2 charactor wide array. It seems like you want to compare each char of 'string' with each row in tarray, as in:

if (string == tarray[j][0])

i.e., the ith char of string with the jth row of tarray, and column 0 which is the column with upper case alphas.

This means you need TWO loops, one that iterates over every char in string that was entered, and another that iterates over the 26 rows of tarray, looking for that char.

Another issue here is that you only translate UPPERCASE chars. Does that mean that you don't want to convert lower-case letters?

after playing with your code

#include<iostream>
using namespace std;
  
const int n_row =26;
const int n_col= 2;
const char tarray[n_row][n_col]= {
    {'A', '?'},
    {'B', '!'},
    {'C', '.'},
    {'D', ';'},
    {'E', ':'},
    {'F', '|'},
    {'G', '^'},
    {'H', ','},
    {'I', '%'},
    {'J', '$'},
    {'K', '>'},
    {'L', '<'},
    {'M', '='},
    {'N', '['},
    {'O', ']'},
    {'P', '+'},
    {'Q', '-'},
    {'R', '*'},
    {'S', '/'},
    {'T', '#'},
    {'U', '@'},
    {'V', '&'},
    {'W', '('},
    {'X', ')'},
    {'Y', '{'},
    {'Z', '}'} };

class toyclass {  // didn't like the name
  char m_string[30];
public:
  void encrypt();
  void decrypt();
private:
  char lookup( char encoded );
};

void toyclass::encrypt() {
  char inp[30];
  int idx;
  cout<<"Enter the string \n";
  cin.getline(inp,30);
  for( idx = 0; inp[idx] != 0; idx++ ) {
     int cidx = inp[idx] - 'A';
     if ( cidx >= 0 && cidx < 26 ) 
       m_string[idx] = tarray[cidx][1];
     else 
       cout<<"Character '" << inp[idx] << "' not found \n";
  }
  m_string[idx] = '\0';

  cout << "encrypted '" << m_string << "'\n";
}

char toyclass::lookup( char encoded ) {
  char decoded = '*';
  int idx = 0;
  while ( idx < n_row ) {
     if ( tarray[idx][1] == encoded ) {
        decoded = tarray[idx][0];
        break;
     }
     idx++;
  }
  return decoded;
}

void toyclass::decrypt(){
  int idx;
  char outp[30];
  for( idx = 0; m_string[idx] != 0; idx++ )
     outp[idx] = lookup(m_string[idx]);
  outp[idx] = '\0';
  cout << "decrypted '" << outp << "'\n";
}

int main() {

  toyclass t1;
  t1.encrypt();
  t1.decrypt();
  return 0;
}

K.

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.