html string processing



so iam creating a code that will need to do some kind of little html coding with c++ . i am trying to make quiz related to harry potter theme . which witch are you? i want each option to give a result of that characters gif that i want to use using url. i dont know how to go about coding . there are two codes that i have in my pico editor connect to a linux server. i dont know how to start about or even do html stringing with c++ . can some one show an example please

first is web form html

<html><head><title>Three Form Fields</title></head>
<body>
<form action="retrieve_form_start.cgi" method="GET">
Use one word per field:<br><br>
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>

and now this is the c++ coding





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
 #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<<"<pre>";
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.

cout<<"\n\n<html>";
cout<<"\n<head>";
cout<<"\n<h2>Using a Full URL File Path</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<img src="http://images5.fanpop.com/image/photos/24600000/Harry-Potter-Gif-harry-potter-24610368-500-409.gif" alt="Ron Weasley" style="width:400px">
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
cout<<"</pre>";
}

This code looks very familar.
I think there are some post with this problem here already.
Where did you find the code?
@Thomas1965,

I believe you are referring to http://www.cplusplus.com/forum/beginner/234506/

Slightly different problem this time, but the same code to start with.

Hello poonamp6792,

You have fixed the "pram" function. That is an improvement.

Using "QUERY_STRING" in the "getenv" function is not something everyone can use. For testing I had to comment this out and use the next line.

line 57 You have a comment on this line. What you are missing is some '\'s before the double quotes and a ; at the end. It works better like this:
cout << "\n<img src=\"http://images5.fanpop.com/image/photos/24600000/Harry-Potter-Gif-harry-potter-24610368-500-409.gif\" alt=\"Ron Weasley\" style=\"width:400px\">";. In order to use a double quote inside something that has a double quote at the beginning and end you need to escapthe other double quotes with a back slash ( \" ).

I have not had the opportunity to to work with a program that creates a html page, so I am not sure if doing a cout for the html code will actually work. You may have to put this code in a file to use it.

Another program I worked on http://www.cplusplus.com/forum/beginner/234623/ may help with your code.

When the program showed some output it looked like two sections. Also it looks to me like the first section should be inside the body tags of the second section. Looking at the output I believe it is missing some necessary tags to be proper html code.

Now that the program works I have something to work on. I am thinking of writing the output to a file for a better idea if what it is doing.

Hope that helps,

Andy
@Handy Andy,
yes that was the one in my mind. I think there are some more - maybe 1-2 years old.

I have not had the opportunity to to work with a program that creates a html page, so I am not sure if doing a cout for the html code will actually work.

Normally it should. The webserver will catch the output and send it to the browser.

BTW The browser also accepts single quotes for the attributes
Last edited on
@Thomas1965,

Thank you.

Normally it should. The webserver will catch the output and send it to the browser.


I have dealt with the concept, but have yet to have the opportunity to actually use it.

BTW The browser also accepts single quotes for the attributes

Up till now I have only seen double quotes around attributes.

Andy
Hello poonamp6792,

I was working with the program and found in line 43 you send the work"Witch" to the function when you should be sending "color". As is it leaves the variable "Witch" blank.

Your cout statements work, but they are in the wrong order. Your cout statements need to output html code that looks like this:


<!Doctype html>
<html lang = "en">
<head>
    <meta charset = "utf-8">
    <meta name = "dcterms.created" content = "Sat, 07 Apr 2018 12:08:22 GMT">
    <meta name="description" content="">
    <meta name="keywords" content="">

    <title>Parameter Parser</title>

    <link href=".. / images / table.png" rel="icon">

    <style type = "text/css">
    </style>

    <!--[if IE]>
    <script src = "http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
</head>
    <body>
        <pre>debug with qs: first=Ron&last=Weasley&color=red</pre>
        <h2>Using a Full URL File Path</h2>
        first value = Ron<br>
        last value = Weasley<br>
        Witch value = red<br>&nbsp;<br />
        <img src="http://images5.fanpop.com/image/photos/24600000/Harry-Potter-Gif-harry-potter-24610368-500-409.gif" alt="Ron Weasley" style="width:400px">
    </body>
</html>

The indentation is not necessary and as Thomas1965 says single quotes around the attributes will work.

Between the head tags there is some extra code that you may not need or want to use. I put it there as an idea and place holder.

Since I put this code in a file I was able to open the file in my browser and it works.

Hope that helps,

Andy

Edit: Changed <br/> to <br />.
Last edited on
Hi guy,
Iam basically converting this into cgi. I just dont how to go about using the html with string processing. there are three witches name im using to do a little personality quiz. so harry , hermione and ron are the the witches which when selected in web from i want it to spit out the personality trait. heres a little javacscript i created.

switch(Witch) {
case "Harry Potter":
text = "You chose Harry Potter! You are loyal to your friends just like how harry was to Ron and Hermione. You love the natural at things without really training for it. Even though you get into mess all the time! It’s because you stand up for the truth. ";
break;
case "Hermione Granger":
text = "You chose Hermione Granger! You are most likely to be perfectionist in everything you do. You are usually the hope of light everyone needs. Everything needs to be done in timely manner Mr/Ms. Perfect shall we say? You are quite resourceful because of the knowledge you have.";

break;
case "Ron Weasley":
text = "You chose Ron Weasley! You are pure blood! You always stand alongside with your friends despite having insecurities sometimes. You have few friends but you choose the ones you know that will be worth it in the long run. You are brave and smart and these are the traits that most likely make you like Ronald Weasley!"

break;
default:
text = "I have never heard of that witch...";


i want this html/js to be used in html section in the c++ code idk how to even start.
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.
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\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
Last edited on
Topic archived. No new replies allowed.