954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

make things shorter

i have made a program that gives a defenition of a short form
e.g. ;-) = wink

#include <iostream>
using namespace std;

int main()
{
    char input[20];
    char correct1 [] = "CU";
    char correct2 [] = ":-)";
    char correct3 [] = ":-(";
    char correct4 [] = ";-)";
    char correct5 [] = ":-P";
    char correct6 [] = "(~.~)";
    char correct7 [] = "TA";
    char correct8 [] = "CCC";
    char correct9 [] = "CUZ";
    char correct10 [] = "TY";
    char correct11 [] = "YW";
    char correct12 [] = "TTYL";
    cout << "Enter phrase>";
    cin >> input;

if (strcmp(input, correct1) == 0)
    {
              cout << "See You\n";
    }

if (strcmp(input, correct2) == 0)
    {
              cout << "I'm happy\n";
    }
    
if (strcmp(input, correct3) == 0)
    {
              cout << "I'm Unhappy\n";
    }
    
if (strcmp(input, correct4) == 0)
    {
              cout << "wink\n";
    }
    
if (strcmp(input, correct5) == 0)
    {
              cout << "stick out my tongue\n";
    }
    
if (strcmp(input, correct6) == 0)
    {
              cout << "sleepy\n";
    }
    
if (strcmp(input, correct7) == 0)
    {
              cout << "totally awesome\n";
    }
    
if (strcmp(input, correct8) == 0)
    {
              cout << "Canadian Computing Competition\n";
    }
    
if (strcmp(input, correct9) == 0)
    {
              cout << "because\n";
    }
    
if (strcmp(input, correct10) == 0)
    {
              cout << "thank-you\n";
    }
    
if (strcmp(input, correct11) == 0)
    {
              cout << "you’re welcome\n";
    }
    
if (strcmp(input, correct12) == 0)
    {
              cout << "talk to you later\n";
    }
    
return main();
}


I was thinking if there was a way to write this in a shorter way

toko
Junior Poster
104 posts since Aug 2007
Reputation Points: 10
Solved Threads: 8
 

you could use an array of structures

struct phrases
{
    char* correct;
    char* response;
};
phrases  ph[] = {
   {"CU","See You\n"},
   {":-)", "I'm happy\n"},
 // etc
};

   cout << "Enter phrase>";
    cin >> input;
  for(int i = 0; i < sizeof(ph)/sizeof(ph[0]); ++i)
  {
        if(stramp(input, ph[i].correct) == 0)
        {
             cout << ph[i].response;
             break;
        }
   }
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

thanks

toko
Junior Poster
104 posts since Aug 2007
Reputation Points: 10
Solved Threads: 8
 

you could also use c++ std::map class, but may or may not be too advanced for you at this time.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

whats that?

toko
Junior Poster
104 posts since Aug 2007
Reputation Points: 10
Solved Threads: 8
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You