c++ ----using web form html i created this i need help with this code. i am havi
ID: 3705275 • Letter: C
Question
c++ ----using web form html i created this i need help with this code. i am having problems with the if else statements..
this is the web form html
p>The switch statement will execute a block of code based on your input.</p>
<form action="retrieve_form_start.cgi" method="GET" "target="_blank">
First Name:<br>
<input type="text" name="first"><p>
Last Name<br>
<input type="text" name="last"><p>
Favorite Witch<br>
<input type="radio" name="Witch" value="Harry Potter"> Harry Potter<br>
<input type="radio" name="Witch" value="Hermione Granger"> Hermione Granger<br>
<input type="radio" name="Witch" value="Ron Weasley"> Ron Weasley
<p><input type="submit" value="witch?">
</form>
</body>
</html>
<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 ";
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.
if (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 ";
cout<<" <html>";
cout<<" <head>";
cout<<" <h2>Which witch are you?</h2>";
cout<<" <title>Parameter Parser</title>";
cout<<" </head>";
cout<<" <body>";
cout<<" first value="<<first<<"<br>";
cout<<" last value="<<last<<"<br>";
cout<<" Witch value="<<Witch<<"<br>";
cout<<" </body>";
cout<<" </html>";
// code an HTML page, which includes the three fields
// received.
cout<<" ";
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> " << 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
}
Explanation / Answer
in if else part on you mistakenly put all in double qoutes .
so you have to do minor change in that line that i descirbe below. thnx.
else if (Witch=="Hermione Granger"){
cout<<"You are smart"<<endl;
}
else if ("Witch== Ron Weasley") Changed to this else if (Witch=="Ron Weasley")
{
cout<<"You are courageous"<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.