| | |
make things shorter
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
i have made a program that gives a defenition of a short form
e.g. ;-) = wink
I was thinking if there was a way to write this in a shorter way
e.g. ;-) = wink
C++ Syntax (Toggle Plain Text)
#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
you could use an array of structures
C++ Syntax (Toggle Plain Text)
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; } }
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.
![]() |
Similar Threads
- Collins: Why this scientist believes in God (Geeks' Lounge)
- Hardware Interrupts & 100% CPU usage (Windows NT / 2000 / XP)
- Programming FAQ - Updated 1/March/2005 (Computer Science)
- about classes, give a hand (C)
- PC suddenly died, no post or moitor,only fans. (Troubleshooting Dead Machines)
- Dont get confused how to get top position! follow my experience! (Promotion and Marketing Plans)
- guestbook (ASP.NET)
- extending Horizontal as well as vertical ???? (HTML and CSS)
- bubble sorting in an array (C)
- Barnes and Nobles w/ $50 (C++)
Other Threads in the C++ Forum
- Previous Thread: Arrays and Operators
- Next Thread: I need help with a simple function.
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






