HTML confusion

So far most all the people I've asked about the assignment in my class have been unsure of how to go on with our program. We're trying to create the program that can intake values from a HTML link using C++. The idea is to accept the values inputted by a user, these could be first name, last name, and favorite color. We are supposed to then make sure that the link operates correctly and directs the user to the next page, utilizing the data supplied by the same user.

So far we have all been stuck on the program part of the assignment. There's a structure that we're supposed to work with, and the function calls in the main are rather confusing to everyone. Our first issue is the parse(), where a given retrieve_form_start.cpp was given by the professor, and we're supposed to work with the function to populate the array. I am completely lost as to how we're supposed to populate the said array given what we have. The issue for me primarily is that I can't figure out how to even test run the code in terminal since it's HTML. The program compiles, but it doesn't run at all. This is the error code:
$ ./retrieve_form
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
Aborted

Also, here's the starting point for a structure array that seems to be confusing everyone.

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
#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); 


/*
	// Three fields are retrieved with 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 [])
{	
	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); 
		cout << "value: " << value << "<br>" << endl; 
		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)
{	

}



I'm not sure how anyone could help, the reason seems to be that it's HTML, and I can't figure out how to begin working with it.
Last edited on
HTML is nothing particular. Just another text format.

Your problem is most likely in parse(...) pos/start_pos. If qs.find(...) returns string::npos you cannot use it for substr(...). Actually you need to break the loop.
It seems this program needs to run on a server.
https://www.tutorialspoint.com/cplusplus/cpp_web_programming.htm

If you run it normally the environment var QUERY_STRING is not set and getenv("QUERY_STRING") will return null and that causes the crash.

You can run it normally by commenting line 25 and uncomment line 26.
You need to complete the function param and out put the fields in HTML
I've managed to run it through the school's ssh login, but it seems like a really difficult way to write a program. The issue being that we can't quite use an editor to check our work easily. Also, can someone help with finishing the parse? I'm not sure what sort of array we are going to be creating. Isn't there only a size 3 array? What should be going in and out?
You don't need to create an array. The array is created in main (line 23), filled by the function parse.
finishing the parse

Assign the name and value to the struct in the array.

You need to do is complete the function param and output the fields in HTML.
Until it is fully tested you can run it on your computer. When it is working change lines 25,26 and run on the shell.
How would I populate FIELDS f_name_value_pairs [], and what would I populate it with?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void parse(string 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);
    //cout << "name: " << name << "<br>" << 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 << "<br>" << endl;
    f_name_value_pairs[counter].value = value;
    start_pos = pos + 1;
  }
}

Just needed is line 9 + line 17
@Thomas1965 Is what you did place the three "names" from the html into the array, and then assigned each of the three indexed names now their value according to the html link?

How would I begin with the param() function, it seems similar to the last program we did where we created a function to find out which index in the array had was the highest number, but I'm not quite sure what we're trying to do this time around.

1
2
3
4
5
6
7
8
9
string param(string lookUp, FIELDS f_name_value_pairs[], int f_cnt)
{
        for (int i=0; i<f_cnt; i++) {
                if (lookUp == f_name_value_pairs[i].name) {
                        return f_name_value_pairs[i].value;
                }
        } 
}


That seems to enable me to cout the html "variables" onto a webpage. Now I need to learn how to code an html page. Seems really confusing so far...
Last edited on
The query string consists of 3 name-value pairs. "first=fred&last=flint&color=red" separated by &. Name and value in the pair is separated by =
After parse the array f_name_value_pairs [] contains these values.
1
2
3
4
5
6
f_name_value_pairs [0].name = first
f_name_value_pairs [0].value = fred
f_name_value_pairs [1].name = last
f_name_value_pairs [1].value = flint
f_name_value_pairs [2].name = color
f_name_value_pairs [0].value = red


Your function param looks good.
I am not sure how your html output is supposed to look like. Does the assignment say anything about it?

One simple way:
1
2
3
4
5
6
7
8
9
10
 // Three fields are retrieved with 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.
  cout << "<p>first: " << first << "</p>\n";
  cout << "<p>last: " << last << "</p>\n";
  cout << "<p>color: " << color << "</p>\n";


Ah, thanks. That's what I understood the parse() to do. Having you clearly write that out made it so much easier to clearly know what I was thinking. I'm right now unsure about how to code the second page with the .cgi. Using a second web_form.html, I'm able to do a lot of the code for the initial page that users can interact with.

What I'm stuck on is the .cpp file which is converted into .cgi. I know that it's using cout statements, but that doesn't make it entirely compatible with the html format, since some of the stuff uses code that overlaps. How do I get past this block?

My idea right now is to do something 'creative' with the user input. Right now my html looks like this: http://toolkit.cs.ohlone.edu/~gen258/web_form.html
I want to program something in the .cgi that'll allow me to use the first, second, and last string varibles and place them into some sort of widget that could input the searches into them and produce a result. So with the stocks that I offer, the user can look them up through a widget. Is this doable in C++?

edit: I'm trying to just create a html page on the .cgi version of my program. For some reason I can't create a for loop which will print out a url image. If I could do this much easier step I'd probably be able to finish my assignment. Is there a way to simply print out small images inside of a for loop? This is during a CGI program.
Last edited on
What I'm stuck on is the .cpp file which is converted into .cgi. I know that it's using cout statements, but that doesn't make it entirely compatible with the html format, since some of the stuff uses code that overlaps. How do I get past this block?


I don't understand what the problem is.
The cgi program dumps all the html code onto the console and the server catches this output and sends it as html file to the browser.
Can you show me what you want to output.
I think I'm finished now actually. I got some more help from the class' board online, and my assignment works alright for now. http://toolkit.cs.ohlone.edu/~gen258/web_form.html
I think this is a fine result. Thanks for all the help everybody.
Topic archived. No new replies allowed.