Loads of input strings

Hey guys. I'm writing a program now that should able user to input alot of different input functions and process them seperately to give different output.
I'm just not sure what's the best way to go about this and I don't really want to write 100 different if else statements which would be very ugly and probably just counter-productive for the project.

I found this:
http://www.codeguru.com/cpp/cpp/cpp_mfc/article.php/c4067/Switch-on-Strings-in-C.htm

quite interesting. Create a map of strings for key's to trigger functions from a switch statement.

Example of what I want to be able to input to my program:
Enter input:
display matrix (display's matrix)
R1 - R2 = subtracts R2 of matrix from R1
R1 * 2 = multiplies row 1 of matrix with 2

anyway this is just a quick example, I really want the input to be able to
be flexible but also be able to give me an error if I do something that makes totally no sense like "ughdjhfdi"

So I'm just looking for hints if any of you have done this sort of stuff before. You don't have to write any code (unless you want to), but links or references would be highly appreciated.
Are you building a calculator (like bc) that can work with matrices?

It's not clear what you're doing. You can't just parse any old thing easily. But it's possible to make a reasonable parser for a specific domain, the smaller the easier.

Stroustrup presents a calculator complete with parser in The C++ Programming Language.
Last edited on
yes kbw I'm making a program that can work with matrices and do gauss elimination on them. I want the program to take linear equations in as input
like
x - 3y = 5
2x + y = 1

the program would then load this into a matrix resulting in

[1 -3 | 5]
[2 1 | 1]

and next the user would be able to start doing some calculations like
R2 - 2*R1

etc

I've got stroustrup's book lying around here somewhere ,will check it out thanks :D
I haven't done the FAQ for it yet, but the basic idea is that you must be able to convert the string to a sufficiently unique number in a consistent way.

The simplest way of doing this is an array of strings, because each string has a unique index into the array.

Finding the string in the array is the same thing as converting the string to a number:

1
2
3
4
5
6
7
8
9
10
11
12
13
string names[] = { "Juan", "Hediberto", "Maria", "Luis", "Nalleli" };

string name_to_find = "Maria";

switch (std::find( names, names + 5, name_to_find ))
  {
  case 0: cout << "Hola Juan\n"; break;
  case 1: cout << "Hola Edi\n"; break;
  case 2: cout << "Bendito sea, Maria\n"; break;
  case 3: cout << "What'Sapo, Luis?\n"; break;
  case 4: cout << "Hey there Na-Na!\n"; break;
  default: cout << "No te conozco (I don't know you)\n";
  }

Using std::find() is very simple, but it also has drawbacks, particularly, if the name_to_find is "Na-Na" then it will not find Nalleli. Nor will it find "luis", because of capitalization. A more robust solution will take care to convert all the proper inputs to the proper number, and where there is ambiguity, properly complain to the user.

[edit] Oh, yeah, as in the example you linked: The poster was demonstrating the same idea, but using a std::map container, where the strings are keys and the integer values are the results. [/edit]

Here are two more advanced examples of converting a string to an integer value:

(This one converts an English month name into its index number -- scroll down to the second code block)
http://www.cplusplus.com/forum/general/11460/#msg54095

(This one converts boolean strings [like "yes" and "true"] into a boolean value)
http://www.cplusplus.com/forum/beginner/18518/#msg94815

Hope this helps.
Last edited on
Thank you Duoas, lot's of very useful information here. This method with array of strings is genius and looks simpler then the one with the map!
The std::find() method is convenient also because you can apply a custom string comparison function as a fourth argument. This allows you to do things like search on substrings and ignore case during comparisons...

The main drawback is when you want two completely different strings to compare equal. For example, I could add "na-na" to the list above, in addition to "Nalleli", but they would convert to different numbers, and the switch would then have to test for both numbers for one person.

This is where the map is useful, since the association is 1:N (instead of 1:1 in an array). Read more about associative containers (and how they differ from indexed sequences like arrays) here: http://cplusplus.com/faq/sequences/#associative-containers

Glad to be of help. :O)
For other people looking for this kind of calculator method I found a great segment about this in Bjarne Stroustrup's book "Programming -- Principles and Practice Using C++", chapter 6. This chapter explains very well how to build a simple parser from keyboard input + source code is available at his webpage: http://www.stroustrup.com/Programming/
Topic archived. No new replies allowed.