need help
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
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:
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:
unfortunately, you can only use switch on integer types, so you won''t be able to use it with strings.
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])
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement