Question
can someone step by step comment and explain the logic of the code. specailly when its return -1 ? #include #include #include using namespace std;
can someone step by step comment and explain the logic of the code.
specailly when its return -1 ?
#include
#include
#include
using namespace std;
struct FIELDS
{
string name;
string value;
};
const int cnt = 4;
class WebApps {
private:
string qs; // holds the QUERY_STRING
int cnt; // holds the number of fields found from the form
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)
{
qs = f_getenv;
}
void set_cnt(int f_how_many)
{
cnt=f_how_many;
}
string get_qs()
{
return qs;
}
int get_cnt()
{
return cnt;
}
//////////////////////////////////////////////////// // how_many() // This will count how many = signs in the QUERYSTRING ////////////////////////////////////////////////////
int how_many(string f_qs)
{
int start_pos = 0, pos, count = 0;
do
{
pos = f_qs.find ("=", start_pos);
count++;
start_pos = pos + 1;
}
while (pos != string::npos);
return count - 1;
}
////////////////////////////////////////////////////
//create_array
// Creates a dynamic array
////////////////////////////////////////////////////
FIELDS *create_array (int f_cnt)
{
FIELDS *array= new FIELDS [f_cnt]; //creating the dynamic array
return array;
}
/////////////////////////////////////////////
// parse()
// This will separate the name/value pairs found after the ? in the URL
/////////////////////////////////////////////
void parse (string f_qs, FIELDS f_name_value_pairs [])
{
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);
start_pos = pos + 1;
pos = qs.find("&", start_pos);
if (pos == string::npos) {
pos = qs.length();
}
value = qs.substr(start_pos, pos - start_pos);
start_pos = pos + 1;
FIELDS field = {name, value};
f_name_value_pairs[counter] = field;
if (pos == qs.length()){
return;
}
}
}
/////////////////////////////////////////////
// param()
// Will receive the value for any given form field
/////////////////////////////////////////////
string param(string lookUp, FIELDS f_name_value_pairs[], int f_cnt)
{
for (int counter=0; counter < f_cnt; counter++) {
if (f_name_value_pairs[counter].name == lookUp){
return f_name_value_pairs[counter].value;
}
}
return "";
}
};
int main()
{
WebApps wo; // name the class object
FIELDS name_value_pairs [cnt];
string qs;//(getenv("QUERY_STRING"));
cout << "Content-type:text/html ";
wo.parse(qs, name_value_pairs);
string first = wo.param("first", name_value_pairs, cnt);
string last = wo.param("last", name_value_pairs, cnt);
string color =wo.param("color", name_value_pairs, cnt);
string witch =wo.param("witch", name_value_pairs, cnt);
string about =wo.param("about", name_value_pairs, cnt);
cout<<"";
cout<<"";
cout<<"
cout<<"";
cout<<"
FirstName: "<
LastName :"<
cout<<"
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started