#### Archived

This topic is now archived and is closed to further replies.

# need help

This topic is 5748 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi i'm just learning C++ i need help with some if statements. i'm trying to make a simple calculator-type program and i want the program to recognise user input in the form of words, to choose the function (e.g addition, division) i know how to do this with numbers-if function (my variable name) ==1 then cout << ", etc.), but, as i said before, i want the program to recognise letter input as well as number. any help at all is greatly apppreciated. also, if this is a really simple question, forgive me. p.s if there is a better way of doing this, instead of using endless if....else statements, please let me know. Edited by - 812 on March 22, 2002 4:35:57 PM Edited by - 812 on March 22, 2002 4:38:18 PM

anyone?

##### Share on other sites
you mean the user types in "add" or something along those lines? if so, you must use strcmp() since you can''t compare two strings with the == operator (that compares their pointers, which will never be the same). for example:
 if (strcmp(UserInput, "add") == 0) DoAddition();

as for your "too many ifs" question, check out the switch statement... it is like many ifs all in a row, and easier to read:
switch(SomeVar)  {  case 0:     DoSomething();    break;  case 1:    DoSomethingElse();    break;  case 2:    DoYetAnotherSomething();    break;  };

unfortunately, you can only use switch on integer types, so you won''t be able to use it with strings.

##### Share on other sites

thanks krez, i''ll try that later but for now i''ve got to go.

thanks again!

• ### Forum Statistics

• Total Topics
628730
• Total Posts
2984431

• 25
• 11
• 10
• 16
• 14