Problem with Decoder

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 26
Reputation: meistrizy is an unknown quantity at this point 
Solved Threads: 0
meistrizy meistrizy is offline Offline
Light Poster

Problem with Decoder

 
0
  #1
Feb 20th, 2009
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?

  1. #include <iostream>
  2. #include <string>
  3. #include <cctype>
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8. {
  9. //define array1, 2, 3
  10. char Code[] = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'};
  11. char Code2[10];
  12. string dCode;
  13.  
  14. cout << "Enter the code you wish decoded: ";
  15. cin >> dCode;
  16. cout << endl;
  17.  
  18.  
  19. int size;
  20. char Code3[size];
  21. size = dCode.length();
  22. char ans[size];
  23.  
  24. for(int i = 0; i < size; i++)
  25. {
  26. Code2[i] = dCode[i];
  27. for(int h = 0; h < size; h++)
  28. { if (Code2[i] == Code[i])
  29. cout << dCode[h - 1];
  30.  
  31. }
  32. }
  33. cout << endl;
  34.  
  35.  
  36.  
  37. system ("pause");
  38. return 0;
  39. }
Last edited by meistrizy; Feb 20th, 2009 at 5:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Problem with Decoder

 
0
  #2
Feb 20th, 2009
your loop is doing too much work.

  1. char codes[] = "qwertyuiop";
  2.  
  3. string dCode = "wtu"; // hard-code search value
  4.  
  5. for(int i = 0; i < dCode.size(); i++)
  6. {
  7. for(int j = 0; codes[j] != 0; j++)
  8. {
  9. if( dCode[i] == codes[j])
  10. {
  11. cout << codes[j-1];
  12. break;
  13. }
  14. }
  15. }
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 26
Reputation: meistrizy is an unknown quantity at this point 
Solved Threads: 0
meistrizy meistrizy is offline Offline
Light Poster

Re: Problem with Decoder

 
0
  #3
Feb 21st, 2009
For some reason this is causing my program to crash. Am I missing something?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Problem with Decoder

 
0
  #4
Feb 21st, 2009
post new code.
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 26
Reputation: meistrizy is an unknown quantity at this point 
Solved Threads: 0
meistrizy meistrizy is offline Offline
Light Poster

Re: Problem with Decoder

 
0
  #5
Feb 21st, 2009
Sorry. Here it is:

  1. #include <iostream>
  2. #include <string>
  3. #include <cctype>
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8. {
  9. //define array1, 2, 3
  10. char Code[] = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'};
  11. char Code2[10];// = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'};
  12.  
  13. string dCode;
  14.  
  15. cout << "Enter the code you wish decoded: ";
  16. cin >> dCode;
  17. cout << endl;
  18.  
  19.  
  20. int size;
  21. char Code3[size];
  22. size = dCode.length();
  23. char ans[size];
  24.  
  25. for(int i = 0; i < dCode.size(); i++)
  26. {
  27. for(int h = 0; Code[h] != 0; h++)
  28. {
  29. if (dCode[i] == Code[h])
  30. {
  31. cout << Code[h - 1];
  32. break;
  33. }
  34. }
  35. }
  36. cout << endl;
  37.  
  38.  
  39. system ("pause");
  40. return 0;
  41. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: Problem with Decoder

 
1
  #6
Feb 21st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 26
Reputation: meistrizy is an unknown quantity at this point 
Solved Threads: 0
meistrizy meistrizy is offline Offline
Light Poster

Re: Problem with Decoder

 
0
  #7
Feb 25th, 2009
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:

  1. #include <iostream>
  2. #include <string>
  3. #include <cctype>
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8. {
  9. //define array
  10. char Code[] = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p',
  11. 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l',
  12. 'z', 'x', 'c', 'v', 'b', 'n', 'm'};
  13.  
  14. string dCode;
  15.  
  16. cout << "Enter the code you wish decoded: ";
  17. cout << endl;
  18. getline (cin, dCode);
  19.  
  20.  
  21. int size;
  22. size = dCode.length();
  23. char Code3[size];
  24.  
  25.  
  26. for(int i = 0; i < size; i++)
  27. {
  28. for(int h = 0; h < 26; h++)
  29. {
  30. if (dCode[i] == Code[h])
  31. {
  32. dCode[i] = Code[h - 1];
  33.  
  34. }
  35. else
  36. {
  37. dCode[i] = dCode[i];
  38. }
  39. }
  40. }
  41.  
  42. cout << endl;
  43. cout << "The translated code from above is: ";
  44. cout << endl << dCode;
  45. cout << endl;
  46. cout << endl;
  47.  
  48.  
  49. system ("pause");
  50. return 0;
  51. }
Last edited by meistrizy; Feb 25th, 2009 at 6:46 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC