943,709 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 586
  • C++ RSS
Feb 20th, 2009
0

Problem with Decoder

Expand Post »
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)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
meistrizy is offline Offline
33 posts
since Dec 2008
Feb 20th, 2009
0

Re: Problem with Decoder

your loop is doing too much work.

C++ Syntax (Toggle Plain Text)
  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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Feb 21st, 2009
0

Re: Problem with Decoder

For some reason this is causing my program to crash. Am I missing something?
Reputation Points: 10
Solved Threads: 0
Light Poster
meistrizy is offline Offline
33 posts
since Dec 2008
Feb 21st, 2009
0

Re: Problem with Decoder

post new code.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Feb 21st, 2009
0

Re: Problem with Decoder

Sorry. Here it is:

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Light Poster
meistrizy is offline Offline
33 posts
since Dec 2008
Feb 21st, 2009
1

Re: Problem with Decoder

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.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Feb 25th, 2009
0

Re: Problem with Decoder

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)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
meistrizy is offline Offline
33 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: IOStream cin causing problems
Next Thread in C++ Forum Timeline: txt reading problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC