make things shorter

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

Join Date: Aug 2007
Posts: 89
Reputation: toko is an unknown quantity at this point 
Solved Threads: 8
toko's Avatar
toko toko is offline Offline
Junior Poster in Training

make things shorter

 
0
  #1
Dec 1st, 2007
i have made a program that gives a defenition of a short form
e.g. ;-) = wink
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,618
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: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: make things shorter

 
0
  #2
Dec 1st, 2007
you could use an array of structures
  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.
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: Aug 2007
Posts: 89
Reputation: toko is an unknown quantity at this point 
Solved Threads: 8
toko's Avatar
toko toko is offline Offline
Junior Poster in Training

Re: make things shorter

 
0
  #3
Dec 1st, 2007
thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,618
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: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: make things shorter

 
0
  #4
Dec 1st, 2007
you could also use c++ std::map class, but may or may not be too advanced for you at this time.
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: Aug 2007
Posts: 89
Reputation: toko is an unknown quantity at this point 
Solved Threads: 8
toko's Avatar
toko toko is offline Offline
Junior Poster in Training

Re: make things shorter

 
0
  #5
Dec 1st, 2007
whats that?
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC