function

im really lost on this coding . i need to create a function called parameter and i dont know where to even start . please help

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
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 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 [])
{
        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);
             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)
{

}

You have to find the struct where the name is == lookUp in the array f_name_value_pairs and return its value.
Either use std::find_if or write your own linear search.
Hello poonamp6792,

A couple of points that you may find useful in the future.

Line 14. Is fine, but I would use capital letters for the name. This helps to let you know that it is a constant variable that can not be changed. Also it is most common to use the name "MAXSIZZE" for a variable like this.

In lines 42 - 44 you pass "cnt" to the function. This is not necessary because "cnt" is defined as a global variable and thee entire file has access to it.

In the "parse" function you have shown me you can use a for loop and if statement. the same concept can be used for the "param" function. Thomas1965 has told you what needs to be done. Now just make it work.

Hope that helps,

Andy
Topic archived. No new replies allowed.