i added if else statements instead where the three strings are i bolded it.
<code>
#include <iomanip>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
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\n\n";
cout << "debug with qs: " << qs << "<p>" << 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 << "<br>";
for (int index = 0; index<cnt; index++) {
cout << "name: " << name_value_pairs[index].name << " ";
cout << "value: " << name_value_pairs[index].value << endl << "<br>";
}
// 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 Witch = param("Witch", name_value_pairs, cnt);
// code an HTML page, which includes the three fields
// received.
i
f (Witch=="Harry Potter")
{
cout<<"You are brave"<<endl;
}
else if (Witch=="Hermione Granger"){
cout<<"You are smart"<<endl;
}
else if (Witch== "Ron Weasley")
{
cout<<"You are courageous"<<endl;
}
else
{
return 0;
}
//50
cout << "Content-type:text/html\n\n";
cout<<"\n\n<html>";
cout<<"\n<head>";
cout<<"\n<h2>Which witch are you?</h2>";
cout<<"\n<title>Parameter Parser</title>";
cout<<"\n</head>";
cout<<"\n<body>";
cout<<"\nfirst value="<<first<<"<br>";
cout<<"\nlast value="<<last<<"<br>";
cout<<"\nWitch value="<<Witch<<"<br>";
cout<<"\n</body>";
cout<<"\n</html>";
// code an HTML page, which includes the three fields
// received.
cout<<"\n\n";
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<br>\n" << 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 << "<br>" << endl;
start_pos = pos + 1;
pos = qs.find("&", start_pos);
if (pos == string::npos) {
pos = qs.length();
}
value = qs.substr(start_pos, pos - start_pos);
f_name_value_pairs[counter].name=name; //store the name of current parameter in array
f_name_value_pairs[counter].value=value; //store the value of current parameter in array
start_pos = pos + 1;
}
}
//*******************************************
// param()
// This will find the field value based on the
// field name
//*******************************************
string param(string lookUp, FIELDS f_name_value_pairs[], int f_cnt)
{
for(int counter=0; counter<f_cnt; counter++) //loop through all element in array
{
if(f_name_value_pairs[counter].name.compare(lookUp)==0) //if given parameter matches any name in array
return f_name_value_pairs[counter].value; //return the value
}
return""; //no name patched. return empty string
}
</code>
AND THIS WHAT IT SHOWS IN THE URL/HOST
http://toolkit.cs.ohlone.edu/~gen242/retrieve_form_start.cgi?first=Poonam&last=Patel&Witch=Hermione+Granger
debug with qs: first=Poonam&last=Patel&Witch=Hermione+Granger
debug in parse
name: first
name: last
name: Witch
debug to show content of name_value_pairs array:
name: first value: Poonam
name: last value: Patel
name: Witch value: Hermione+Granger
You are courageous Content-type:text/html
Which witch are you?
first value=Poonam
last value=Patel
Witch value=Hermione+Granger