944,160 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 977
  • C++ RSS
Dec 1st, 2007
0

make things shorter

Expand Post »
i have made a program that gives a defenition of a short form
e.g. ;-) = wink
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. char input[20];
  7. char correct1 [] = "CU";
  8. char correct2 [] = ":-)";
  9. char correct3 [] = ":-(";
  10. char correct4 [] = ";-)";
  11. char correct5 [] = ":-P";
  12. char correct6 [] = "(~.~)";
  13. char correct7 [] = "TA";
  14. char correct8 [] = "CCC";
  15. char correct9 [] = "CUZ";
  16. char correct10 [] = "TY";
  17. char correct11 [] = "YW";
  18. char correct12 [] = "TTYL";
  19. cout << "Enter phrase>";
  20. cin >> input;
  21.  
  22. if (strcmp(input, correct1) == 0)
  23. {
  24. cout << "See You\n";
  25. }
  26.  
  27. if (strcmp(input, correct2) == 0)
  28. {
  29. cout << "I'm happy\n";
  30. }
  31.  
  32. if (strcmp(input, correct3) == 0)
  33. {
  34. cout << "I'm Unhappy\n";
  35. }
  36.  
  37. if (strcmp(input, correct4) == 0)
  38. {
  39. cout << "wink\n";
  40. }
  41.  
  42. if (strcmp(input, correct5) == 0)
  43. {
  44. cout << "stick out my tongue\n";
  45. }
  46.  
  47. if (strcmp(input, correct6) == 0)
  48. {
  49. cout << "sleepy\n";
  50. }
  51.  
  52. if (strcmp(input, correct7) == 0)
  53. {
  54. cout << "totally awesome\n";
  55. }
  56.  
  57. if (strcmp(input, correct8) == 0)
  58. {
  59. cout << "Canadian Computing Competition\n";
  60. }
  61.  
  62. if (strcmp(input, correct9) == 0)
  63. {
  64. cout << "because\n";
  65. }
  66.  
  67. if (strcmp(input, correct10) == 0)
  68. {
  69. cout << "thank-you\n";
  70. }
  71.  
  72. if (strcmp(input, correct11) == 0)
  73. {
  74. cout << "you’re welcome\n";
  75. }
  76.  
  77. if (strcmp(input, correct12) == 0)
  78. {
  79. cout << "talk to you later\n";
  80. }
  81.  
  82. return main();
  83. }

I was thinking if there was a way to write this in a shorter way
Similar Threads
Reputation Points: 10
Solved Threads: 8
Junior Poster
toko is offline Offline
104 posts
since Aug 2007
Dec 1st, 2007
0

Re: make things shorter

you could use an array of structures
C++ Syntax (Toggle Plain Text)
  1. struct phrases
  2. {
  3. char* correct;
  4. char* response;
  5. };
  6. phrases ph[] = {
  7. {"CU","See You\n"},
  8. {":-)", "I'm happy\n"},
  9. // etc
  10. };
  11.  
  12. cout << "Enter phrase>";
  13. cin >> input;
  14. for(int i = 0; i < sizeof(ph)/sizeof(ph[0]); ++i)
  15. {
  16. if(stramp(input, ph[i].correct) == 0)
  17. {
  18. cout << ph[i].response;
  19. break;
  20. }
  21. }
Last edited by Ancient Dragon; Dec 1st, 2007 at 12:55 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Dec 1st, 2007
0

Re: make things shorter

thanks
Reputation Points: 10
Solved Threads: 8
Junior Poster
toko is offline Offline
104 posts
since Aug 2007
Dec 1st, 2007
0

Re: make things shorter

you could also use c++ std::map class, but may or may not be too advanced for you at this time.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Dec 1st, 2007
0

Re: make things shorter

whats that?
Reputation Points: 10
Solved Threads: 8
Junior Poster
toko is offline Offline
104 posts
since Aug 2007

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: Arrays and Operators
Next Thread in C++ Forum Timeline: Someone Plz Help!!





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


Follow us on Twitter


© 2011 DaniWeb® LLC