Thanks in advance for your help. I am supposed to create a program that allows user input for a code. The program will then read the array and output the letter on the keyboard one character to the left of the inputted strings. I am first trying to get the top row of the keyboard to output correctly before finishing off the rest of the program, but I don't get the output I am expecting. What am I doing wrong?

#include <iostream>
#include <string>
#include <cctype>
using namespace std;


int main()
{
    //define array1, 2, 3    
    char Code[] = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'};
    char Code2[10];
    string dCode;        
       
    cout << "Enter the code you wish decoded: ";
    cin  >> dCode;
    cout << endl;
         

    int size;
    char Code3[size];
    size = dCode.length();
    char ans[size];        
    
    for(int i = 0; i < size; i++)
    {
      Code2[i] = dCode[i];
      for(int h = 0; h < size; h++)     
      { if (Code2[i] == Code[i])
        cout << dCode[h - 1];
        
      }
    }    
      cout << endl;  
  
    
                       
    system ("pause");
    return 0;
}

your loop is doing too much work.

char codes[] = "qwertyuiop";

string dCode = "wtu";  // hard-code search value

for(int i = 0; i < dCode.size(); i++)
{
    for(int j = 0;  codes[j] != 0; j++)
    {
          if( dCode[i] == codes[j])
          {
              cout << codes[j-1];
              break;
          }
    }
}

For some reason this is causing my program to crash. Am I missing something?

Sorry. Here it is:

#include <iostream>
#include <string>
#include <cctype>
using namespace std;


int main()
{
    //define array1, 2, 3    
    char Code[] = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'};
    char Code2[10];// = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'};
    
    string dCode;        
       
    cout << "Enter the code you wish decoded: ";
    cin  >> dCode;
    cout << endl;
         

    int size;
    char Code3[size];
    size = dCode.length();
    char ans[size];        
    
    for(int i = 0; i < dCode.size(); i++)
    {
      for(int h = 0; Code[h] != 0; h++)     
      { 
        if (dCode[i] == Code[h])
        {
         cout << Code[h - 1];
         break;
        }        
      }
    }    
      cout << endl;   
    
   
    system ("pause");
    return 0;
}

When AncientDragon wrote Code[h] != 0 , his code was correct. Yours isn't. Why? AncientDragon's codes array was a string literal, which automatically has a '\0' or 0 value at the end. Your array does not. Either append a '\0' element or change your codes array to look like AncientDragon's.

Thanks Ancient Dragon and Death Oclock for your help. My code works fine now. Here it is reposted for those who might have similar problems:

#include <iostream>
#include <string>
#include <cctype>
using namespace std;


int main()
{
    //define array    
    char Code[] = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p',
                   'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l',
                   'z', 'x', 'c', 'v', 'b', 'n', 'm'}; 
        
    string dCode;        
       
    cout << "Enter the code you wish decoded: ";
    cout << endl;
    getline (cin, dCode);
            

    int size;
    size = dCode.length();
    char Code3[size];    
         
    
    for(int i = 0; i < size; i++)
    {         
      for(int h = 0; h < 26; h++)     
      {         
        if (dCode[i] == Code[h])
        {            
           dCode[i] = Code[h - 1];
                    
        }
        else 
        {
            dCode[i] = dCode[i];            
        }        
      }
    }    
    
    cout << endl;
    cout << "The translated code from above is: ";    
    cout << endl << dCode;
    cout << endl;
    cout << endl;    
      
                             
    system ("pause");
    return 0;
}
This question has already been answered. Start a new discussion instead.