| | |
Problem with Decoder
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 26
Reputation:
Solved Threads: 0
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?
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by meistrizy; Feb 20th, 2009 at 5:22 pm.
your loop is doing too much work.
C++ Syntax (Toggle Plain Text)
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; } } }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Dec 2008
Posts: 26
Reputation:
Solved Threads: 0
Sorry. Here it is:
C++ Syntax (Toggle Plain Text)
#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. Last edited by death_oclock; Feb 21st, 2009 at 7:27 pm.
•
•
Join Date: Dec 2008
Posts: 26
Reputation:
Solved Threads: 0
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:
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by meistrizy; Feb 25th, 2009 at 6:46 am.
![]() |
Similar Threads
- DVD Decoder problem, not allowing DVD playback (Windows 95 / 98 / Me)
- Need Help In Decoder Programming (C++)
- Newbie with a decoder question/problem (Windows NT / 2000 / XP)
- Windows 2000... dvd decoder missing??? (Windows NT / 2000 / XP)
- Hamming codes encoder/decoder in C or C++ (C++)
- video capture card problem (Windows 95 / 98 / Me)
Other Threads in the C++ Forum
- Previous Thread: IOStream cin causing problems
- Next Thread: txt reading problem
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






