Question: Pointers review: to help determine how many fields are presented from the form at run time, the program will count the '=' signs from the

Pointers review: to help determine how many fields are presented from the form at run time, the program will count the '=' signs from the QUERY_STRING and dynamically create a name_value_pairs array of cnt elements to be used by parse() and param(). Here is what to do, in order:

1)As directed above, make a back up of the work so far and work with the new version named retrieve_form_OOP_2.cpp.

2)These additions to the class will require a 'private:' area to hold the cnt and qs variables, a 'constructor' function, as well as additional functions to access the variables in the 'private' area.

3)Cheat sheet for the 'class' modification - adopt this structure to help modify your existing class:

class WebApps { public: //public access specifier. WebApps (){ //constructor cout << "Content-type:text/html "; //get ready to print on browser set_qs(getenv("QUERY_STRING")); //save string to private qs //cout << "debug with get_qs: " << get_qs(); //testing functions set_cnt(how_many(get_qs())); //cout << " debug with get_cnt: " << get_cnt();//testing functions } void set_qs(string f_getenv) { ...} void set_cnt(int f_how_many) { ... } string get_qs() { ... } int get_cnt() { ... } ///////////////////////////////////////////////////// // how_many() // This will count and return how many = signs in the QUERYSTRING //////////////////////////////////////////////////// int how_many (string f_qs) { //initialize variables do { ... } while (pos != string::npos); returns the count or 0 if f_qs is empty } //////////////////////////////////////////////////// //create_array // Creates a dynamic array //////////////////////////////////////////////////// FIELDS * create_array (int f_cnt) { modify function from last assignment } ///////////////////////////////////////////// // parse() // This will separate the name/value pairs found after the ? in the URL ///////////////////////////////////////////// void parse (string f_qs, FIELDS f_name_value_pairs []) { existing code } ///////////////////////////////////////////// // param() // Will receive the value for any given form field ///////////////////////////////////////////// string param(string lookUp, FIELDS f_name_value_pairs [], int f_cnt) { existing code } private: // private access specifier string qs; // holds the QUERY_STRING int cnt; // holds the number of fields found from the form }; 

The way to approach these changes is as always - one step at a time:First create the constructor by adding following lines (from class_start.txt) into your existing class :

cout << "Content-type:text/html "; set_qs(getenv("QUERY_STRING")); cout << "debug with get_qs: " << get_qs();

Next create the two functions set_qs() and get_qs().

Now compile until it does and runs giving the proper debug message.

Use similar approach with 'cnt' - will need function 'how_many()', which can be adapted from parse() fairly easily.

Once cnt works, then parse and param will need modifying to replace the variable by its call get_cnt() to produce the value from 'private:'

The next step would be to create the dynamic array, of which function will also reside in the class. But the calls will come from main() from this point. Note: no need to use pointer notation with this dynamic array - treat it as a regular array (which it is, of course, since arrays are pointers).

Beware of the wo.get_cnt() and other object substitutions in main as well

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!