Question: My code wont compile main.cpp: In function int main(): main.cpp:31:1: error: f_name_value_pairs was not declared in this scope f_name_value_pairs [0].name = first ^ main.cpp:31:31: error:
My code wont compile
main.cpp: In function int main(): main.cpp:31:1: error: f_name_value_pairs was not declared in this scope f_name_value_pairs [0].name = first ^ main.cpp:31:31: error: first was not declared in this scope f_name_value_pairs [0].name = first
#include
#include
#include
#include
#include
using namespace std;
struct FIELDS
{
string name;
string value;
};
const int cnt = 3;
// 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 << "
" << endl;
parse(qs, name_value_pairs);
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
string first = param("first", name_value_pairs, cnt);
string last = param("last", name_value_pairs, cnt);
string color = param("color", name_value_pairs, cnt);
cout << "
first: " << first << "
";cout << "
last: " << last << "
";cout << "
color: " << color << "
";return 0;
}
//*******************************************
//******** Functions begin ******************
//*******************************************
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 << " " << 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 << " " << endl;
f_name_value_pairs[counter].value = value;
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 i=0; i if (lookUp == f_name_value_pairs[i].name) { return f_name_value_pairs[i].value; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
