Advertisement

need help

Started by March 22, 2002 03:29 PM
2 comments, last by 812 22 years, 8 months ago
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?
Advertisement
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.
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
finally a reply!

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

thanks again!

This topic is closed to new replies.

Advertisement