// File: retrieve_form_OOP_2.cpp
// Author: Komal Dhillon
// cs102 Online
// Date: 04/09/18
// Description: This program retrieves three form fields and sends the result
// back to the browser
wo.parse(wo.get_qs(), name_value_pairs); //calling parsefunction of object wo
string first = wo.param("first", name_value_pairs, wo.get_cnt());//calling param function of object wo and storing the returned value in first
string last = wo.param("last", name_value_pairs, wo.get_cnt());//calling param function of object wo and storing the returned value in last
string season = wo.param("season", name_value_pairs, wo.get_cnt());//calling param function of object wo and storing the returned value in color
int summer,winter,fall;
cout<<"\n\n<html>";
cout<<"\n<head>";
cout<<"\n<h2>Which Season is your favorite?</h2>";
cout<<"\n<title>Parameter Parser</title>";
cout<<"\n</head>";
cout<<"\n<body>";
cout<<"\nfirst value="<<first<<"<br>";
cout<<"\nlast value="<<last<<"<br>";
cout<<"\nseason value="<<season<<"<br>";
if (season=="summer")
{
cout<< "Hello " <<first << " " << last << " You enjoy hot weather"<<endl;
}
else if (season=="winter"){
cout<< "Hello " << first << " " << last << " You like snow or cold weather"<<endl;
}
else if (season=="fall")
{
cout<< "Hello " << first << " " << last << " You enjoy Sweater Weather (so do I)"<<endl;
}
/////////////////////////////////////////////////////
// how_many()
// This will count and return how many = signs in the QUERYSTRING
////////////////////////////////////////////////////
int WebApps::how_many(string f_qs) {
int f_cnt = 0;
for (int i=0; i<f_qs.length(); i++) {
if (f_qs[i] == '=') {
f_cnt++;
}
}
return f_cnt;
}
/////////////////////////////////////////////
// parse()
// This will separate the name/value pairs found after the ? in the URL
/////////////////////////////////////////////
/////////////////////////////////////////////
// param()
// Will receive the value for any given form field
/////////////////////////////////////////////
string WebApps::param(string lookUp, FIELDS f_name_value_pairs[], int wo.get_cnt()){
string value;
int i = 0;
for(i = 0; i< wo.get_cnt(); i++)
{
if ( lookUp == f_name_value_pairs[i].name) {
value = f_name_value_pairs[i].value;
}
Here is your code with the syntax errors fixed. Be sure to enable warnings when compiling this. There are still some problems that the compiler will warn you about.
A few notes:
- When you're a beginner, write one or two functions at a time. Then compile the code and fix syntax errors. If you wait until you've created a long program (like this), you'll have so many errors that it's really hard to fix them.
- Pretty much the only thing you do with f_name_value_pairs is pass it to methods of the class WebApps. That's a pretty good hint that f_name_value_pairs should itself be a member of WebApps.
- There's no need to pass the count to parse(). I've removed it. After all, the onlyreasonable value to pass is the number of fields,which you get with get_cnt()
// File: retrieve_form_OOP_2.cpp
// Author: Komal Dhillon
// cs102 Online
// Date: 04/09/18
// Description: This program retrieves three form fields and sends the result
// back to the browser
#include <iomanip>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
usingnamespace std;
struct FIELDS
{
string name;
string value;
};
class WebApps
{
private:
string qs;
int cnt;
int how_many(string f_qs);
public: //public access specifier.
void parse(string qs, FIELDS f_name_value_pairs[]); //declared public member function parse for the class WebApps
string param(string lookUp, FIELDS f_name_value_pairs[]); //declared public member function param for the class WebApps
FIELDS *create_array(int f_cnt);
WebApps()
{ //constructor
cout << "Content-type:text/html\n\n"; //get ready to print on browser
set_qs(getenv("QUERY_STRING")); //save string to private qs
//cout << "debug with get_qs: " << get_qs(); //testing functions
set_cnt(how_many(get_qs()));
//cout << "<br>debug with get_cnt: " << get_cnt();//testing functions
}
void set_qs(string f_getenv)
{
qs = f_getenv;
}
void set_cnt(int f_how_many)
{
cnt = f_how_many;
}
string get_qs()
{
return qs;
}
int get_cnt()
{
return cnt;
}
};
//main begins
int
main()
{
WebApps wo; //created wo object of instance WebApps
FIELDS *name_value_pairs = wo.create_array(wo.get_cnt());
wo.parse(wo.get_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 < wo.get_cnt(); index++) {
cout << "name: " << name_value_pairs[index].name << " ";
cout << "value: " << name_value_pairs[index].value << endl << "<br>";
}
cout << "Content-type:text/html\n\n";
cout << "debug with qs: " << wo.get_qs() << " " << endl;
wo.parse(wo.get_qs(), name_value_pairs); //calling parsefunction of object wo
string first = wo.param("first", name_value_pairs); //calling param function of object wo and storing the returned value in first
string last = wo.param("last", name_value_pairs); //calling param function of object wo and storing the returned value in last
string season = wo.param("season", name_value_pairs); //calling param function of object wo and storing the returned value in color
int summer, winter, fall;
cout << "\n\n<html>";
cout << "\n<head>";
cout << "\n<h2>Which Season is your favorite?</h2>";
cout << "\n<title>Parameter Parser</title>";
cout << "\n</head>";
cout << "\n<body>";
cout << "\nfirst value=" << first << "<br>";
cout << "\nlast value=" << last << "<br>";
cout << "\nseason value=" << season << "<br>";
if (season == "summer") {
cout << "Hello " << first << " " << last << " You enjoy hot weather" << endl;
} elseif (season == "winter") {
cout << "Hello " << first << " " << last << " You like snow or cold weather"
<< endl;
} elseif (season == "fall") {
cout << "Hello " << first << " " << last <<
" You enjoy Sweater Weather (so do I)" << endl;
}
cout << "\n</body>";
cout << "\n</html>";
cout << "\n\n";
return 0;
}
/////////////////////////////////////////////////////
// how_many()
// This will count and return how many = signs in the QUERYSTRING
////////////////////////////////////////////////////
int
WebApps::how_many(string f_qs)
{
int f_cnt = 0;
for (int i = 0; i < f_qs.length(); i++) {
if (f_qs[i] == '=') {
f_cnt++;
}
}
return f_cnt;
}
////////////////////////////////////////////////////
//create_array
// Creates a dynamic array
////////////////////////////////////////////////////
FIELDS *
WebApps::create_array(int f_cnt)
{
FIELDS fields[f_cnt];
return fields;
}
/////////////////////////////////////////////
// parse()
// This will separate the name/value pairs found after the ? in the URL
/////////////////////////////////////////////
void
WebApps::parse(string f_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 < get_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);
f_name_value_pairs[counter].name = name;
if (pos == string::npos) {
pos = qs.length();
}
value = qs.substr(start_pos, pos - start_pos);
cout << "value: " << value << "<br>" << endl;
start_pos = pos + 1;
f_name_value_pairs[counter].value = value;
}
}
/////////////////////////////////////////////
// param()
// Will receive the value for any given form field
/////////////////////////////////////////////
string
WebApps::param(string lookUp, FIELDS f_name_value_pairs[])
{
string value;
int i = 0;
for (i = 0; i < get_cnt(); i++) {
if (lookUp == f_name_value_pairs[i].name) {
value = f_name_value_pairs[i].value;
}
}
return value;
}