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

Dani AI

Generated

A few concise notes to make this thread easier to reuse later. The goal is to replace each input character with the key immediately to its left on a QWERTY layout. Common runtime causes seen here are using uninitialized or variable-length C arrays and accidental out-of-bounds indexing (for example when the matched character is the leftmost key on a row). ’s suggestion to simplify the search loop and ’s point about relying on a missing terminator were both on target; ’s final post gets the job done but it can be hardened and made clearer.

A safer, maintainable approach is to model the keyboard as ordered strings (rows) and search those rows with std::string::find. For every input character: temporarily work in lowercase, search each row, and if found and position > 0 map to the character at position - 1. If position == 0 or the character isn’t on the rows, decide to leave it unchanged (or choose an explicit policy). This avoids manual index arithmetic and undefined behavior from uninitialized C arrays. Also avoid non-standard variable-length arrays; prefer std::string, std::vector, or a mapping container.

Edge cases to test: leftmost keys (`, q, a, z), punctuation and number-row characters, uppercase letters (preserve case), and whitespace. If you need full keyboard behavior, include the number/top row and bracket/backslash characters in your rows. Unit-test with inputs containing boundaries (for example the first column keys) so you don’t accidentally read at index -1.

Example implementation (robust, preserves case, leaves unknown chars unchanged):

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

int main() {
    std::vector<std::string> rows = {
        "`1234567890-=",
        "qwertyuiop[]\\",
        "asdfghjkl;'",
        "zxcvbnm,./"
    };

    std::string input;
    std::getline(std::cin, input);

    std::string output;
    output.reserve(input.size());

    for (char c : input) {
        bool mapped = false;
        char original = c;
        bool is_upper = std::isupper(static_cast<unsigned char>(c));
        char lc = std::tolower(static_cast<unsigned char>(c));

        for (const auto &r : rows) {
            std::size_t pos = r.find(lc);
            if (pos != std::string::npos) {
                if (pos > 0) {
                    char mapped_char = r[pos - 1];
                    output.push_back(is_upper ? std::toupper(static_cast<unsigned char>(mapped_char)) : mapped_char);
                } else {
                    output.push_back(original);
                }
                mapped = true;
                break;
            }
        }
        if (!mapped) output.push_back(original);
    }

    std::cout << output << '\n';
    return 0;
}

Recommended Answers

All 6 Replies

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?

post new code.

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