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

Recommended Answers

All 4 Replies

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;
        }
   }
commented: Clever solution. +1

thanks

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

whats that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.