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.

If you intended to correct an error in the post then please contact us.

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

Share this post


Link to post
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 this post


Link to post
Share on other sites