Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete the function param() based on prototype, calls, and definition. Complete the function 'param()' based on the prototype, calls, and definition, which are already in

Complete the function param() based on prototype, calls, and definition.

Complete the function 'param()' based on the prototype, calls, and definition, which are already in place. The function 'param()'s job is to match the field name passed in its parameter to the corresponding array struct element. Once the appropriate structure is matched, the value for that field can be returned.

Return "" (an empty string) if the element for that name is not found in the array - (no match from the html form).

Use your own fields by modifying the html code appropriately (first, last, and color are not required, just examples) .

As a final touch, return the data received back to the browser in some creative way. For example the color chosen by the user could be used to change the background color of the page. Again, this is only an example - be creative.

#include

#include

#include

#include

#include

using namespace std;

struct FIELDS

{

string name;

string value;

};

const int cnt = 3; //cnt should be set to the number of fields the

html form contains

// Prototypes

void parse(string, FIELDS []);

string param(string, FIELDS [], int);

//main begins

int main()

{

FIELDS name_value_pairs [cnt];

string qs(getenv("QUERY_STRING"));

//string qs("first=fred&last=flint&color=red");

cout << "Content-type:text/html ";

cout << "debug with qs: " << qs << "

" << endl;

parse(qs, name_value_pairs);

// debug to show content of name_value_pairs

cout << "debug to show content of name_value_pairs array: " <<

endl << " ";

for (int index = 0; index

cout << "name: " << name_value_pairs[index].name << " ";

cout << "value: " << name_value_pairs[index].value << endl <<

" ";

}

/*

// Three fields data are retrieved from the param function

string first = param("first", name_value_pairs, cnt);

string last = param("last", name_value_pairs, cnt);

string color = param("color", name_value_pairs, cnt);

*/

// code an HTML page, which includes the three fields

// received.

return 0;

}

//*******************************************

//******** Functions begin ******************

//*******************************************

//*******************************************

// parse()

// This will separate the name/value pairs found after the ? in

// the URL and populate the name_value_pairs array of structures

//*******************************************

void parse (string qs, FIELDS f_name_value_pairs [])

{

cout << "debug in parse " << endl;

string name, value;

int start_pos = 0, pos;

for (int counter=0; counter < cnt; counter++) {

pos = qs.find("=", start_pos);

name = qs.substr(start_pos, pos - start_pos);

cout << "name: " << name << " " << endl;

f_name_value_pairs[counter].name = name;

start_pos = pos + 1;

pos = qs.find("&", start_pos);

if (pos == string::npos) {

pos = qs.length();

}

value = qs.substr(start_pos, pos - start_pos);

cout << "value: " << value << " " << endl;

start_pos = pos + 1;

f_name_value_pairs[counter].value = value;

}

}

//*******************************************

// param()

// This will find the field value based on the

// field name

//*******************************************

string param(string lookUp, FIELDS f_name_value_pairs[], int f_cnt)

{

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PostgreSQL Up And Running A Practical Guide To The Advanced Open Source Database

Authors: Regina Obe, Leo Hsu

3rd Edition

1491963417, 978-1491963418

More Books

Students also viewed these Databases questions

Question

Understand human resource planning in an academic setting.

Answered: 1 week ago