Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The method you've chosen is a very poor fit for the problem, especially if you later want to add more command forms to the program.</p> <p>The proper way to handle this kind of input is to write a parser using a parser generator such as GNU Bison, but in your case that might be overkill, would take too much space to explain, and requires some knowledge about languages and grammars (in the computer science sense). Search for "GNU Bison" or "parser generator" on Google if you want to learn more about that.</p> <p>A more limited but easier to understand solution is to use C++11 regular expressions. First match the entire line to find the function, entity and parameter list, and then match the parameter list depending on the entity or function type. The code would look something like that:</p> <pre><code>// This regular expression matches one line of input (one command). // A line has some text starting with a letter (the function name), // then one or more spaces, another piece of text, // and then a string of arbitrary characters inside parentheses. // Parentheses not preceded by \\ denote capture groups. // Any characters matched by them are later accessible through // the std::smatch object. std::regex rx_line("([A-Za-z][A-Za-z0-9]*)\\s+" "([A-Za-z][A-Za-z0-9]*)\\s*" "\\((.*)\\)\\s*"); // Retrieve one non-empty line of input, skipping whitespace. std::string line; std::getline(std::cin &gt;&gt; std::ws, line); // Match the line regex to the line, capturing the parts of text which we want. std::smatch line_match; if (!std::regex_match(line, line_match, rx_line)) { // Report error: the command has incorrect format. } // Retrieve parts of the input matched by capture groups. std::string function = line_match[1]; std::string entity = line_match[2]; std::string params = line_match[3]; // Depending on which entity was mentioned in the command, // match the parameters to a suitable regex. if (entity == "student") { // number, then comma, then text in quotes, then comma, // and finally a number. std::regex rx_student("([1-9][0-9]*)\\s*," "\\s*\"([^\"]*)\"\\s*," "\\s*([1-9][0-9]*)"); std::smatch student_match; if (!std::regex_match(params, student_match, rx_student)) { // report error } // Retrieve parameters from capture groups. int int1 = std::atoi(student_match[1].str().c_str()); std::string text = student_match[2]; int int2 = std::atoi(student_match[3].str().c_str()); // ... // do something with the data // ... } else if (entity == "account") { // and so on } else { // error - unknown entity } </code></pre> <p>See e.g. <a href="http://www.cplusplus.com/reference/regex/ECMAScript/" rel="nofollow">http://www.cplusplus.com/reference/regex/ECMAScript/</a> for a description of regular expression syntax.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload