Question

I am currently stuck with this portion of my assignment: Pointers review: to help determine how...

I am currently stuck with this portion of my assignment:

Pointers review: to help determine how many fields are presented from the form at run time, the program will count the '=' signs from the QUERY_STRING and dynamically create a name_value_pairs array of cnt elements to be used by parse() and param(). Here is what to do, in order:

  1. As directed above, make a back up of the work so far and work with the new version named retrieve_form_OOP_2.cpp.
  2. These additions to the class will require a 'private:' area to hold the cnt and qs variables, a 'constructor' function, as well as additional functions to access the variables in the 'private' area, as explained in videos at 1b and 1c.
  3. Cheat sheet for the 'class' modification - adopt this structure to help modify your existing class:
    • class_start.txt
  4. The way to approach these changes is as always - one step at a time:
    1. First create the constructor by adding following lines (from class_start.txt) into your existing class :
      • cout << "Content-type:text/html\n\n"; set_qs(getenv("QUERY_STRING")); cout << "debug with get_qs: " << get_qs();
    2. Next create the two functions set_qs() and get_qs().
    3. Now compile until it does and runs giving the proper debug message.
    4. Use similar approach with 'cnt' (as get_qs() and set_qs() above) - will need function 'how_many()', which can be adapted from parse() fairly easily. Note: while working on set_cnt() and get_cnt(), code how_many() to simply return a 3 (and comment everything else). Once set_cnt() and get_cnt() are working (based on the debug statements), code how_many() to find the 'real' number of = signs in qs. Makes sense?
    5. Once cnt works, then parse and param will need modifying to replace the variable by its call get_cnt() to produce the value from 'private:'
  5. The next step would be to create the dynamic array, of which function will also reside in the class. But the calls will come from main() from this point. Note: no need to use pointer notation with this dynamic array - treat it as a regular array (which it is, of course, since arrays are pointers).
  6. Beware of the wo.get_cnt() and other object substitutions in main as well

This is the code for class WebApps that I have so far:

//Libraries

   #include <iomanip>

   #include <iostream>

   #include <cstring>

   #include <cstdlib>

   #include <string>

   #include <cctype> //for isalnum()

   using namespace std;

struct FIELDS {

   string name;

   string value;

};

const int cnt = 3; //should be set to the number of fields in form

class WebApps{

   public:

WebApps()

{

   cout << "Content-type:text/html\n\n";   

   set_qs(getenv("QUERY_STRING"));

//cout << "debug with get_qs: " << get_qs();

   set_cnt(how_many(get_qs()));

//cout << "<br>debug with get_cnt: " << get_cnt();  

}

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;

   }

/////////////////////////////////////////////////////

// how_many()

// This will count and return how many = signs in the QUERYSTRING

////////////////////////////////////////////////////

int how_many (string f_qs) {

//initialize variables

int i = 0;

int count = 0;

while (i<strlen(f_qs)) {

if(f_qs.at(i) == "=") count++;

   }

   returns count; //returns the count or 0 if f_qs is empty

   }

////////////////////////////////////////////////////

//create_array

// Creates a dynamic array  

FIELDS * create_array (int f_cnt)

{

   FIELDS fields[f_cnt];

   return 0;

}

/////////////////////////////////////////////

// parse()

// This will separate the name/value pairs found after the ? in the URL

/////////////////////////////////////////////   

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

f_name_value_pairs[counter].name = name;

cout << "name: " << name << " " << 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].value = value; //store the name of parameter in array

cout << "value: " << value << "<br>" << endl;

start_pos = pos + 1;

   }

   }

   //value = qs.substr(startPos, qs.length() - startPos);

   //cout << "value: " << value << endl;

string param(string lookUp, FIELDS f_name_value_pairs[], int f_cnt) {

string f_value;

bool found = false;

for (int index=0; index < f_cnt; index++) {

if(f_name_value_pairs[index].name != lookUp);

   //do nothing

else

   {

found = true;

f_value = f_name_value_pairs[index].value;

   }

   }  

   if (found)

return f_value;

   else

return "";

   }

   private: // private access specifier

string qs; // holds the QUERY_STRING

int cnt; // holds the number of fields found from the form

};

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOLUTION:

class WebApps {

private: // private access specifier

string qs; // holds the QUERY_STRING

int cnt; // holds the number of fields found from the form

public: //public access specifier.   

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

}

  

//setter method for qs

void set_qs(string f_getenv) {

qs = f_getenv;

}

//setter method for cnt

void set_cnt(int f_how_many) {

cnt = f_how_many;

}

string get_qs() {

return qs;

}

int get_cnt() {

return cnt;

}

/////////////////////////////////////////////////////

// how_many()

// This will count and return how many = signs in the QUERYSTRING

////////////////////////////////////////////////////

int how_many (string f_qs)

{

int i = 0;

int count =0;

while (i < strlen(f_qs)){

if(f_qs.at(i) == '=') count++;

}

returns count;//returns count or 0 if f_qs is empty

}

////////////////////////////////////////////////////

//create_array

// Creates a dynamic array

////////////////////////////////////////////////////

FIELDS * 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 parse (string f_qs, FIELDS f_name_value_pairs [])

{

int pos = 0;

string token = "";

int count =0;

while ((pos = s.find(",")) != std::string::npos) {

token = f_qs.substr(0, pos);

f_qs.erase(0, pos + 1);

int pos2 = 0;

string token2 = token;

while ((pos2 = token.find("=")) != std::string::npos) {

token2 = token.substr(0, pos2);

token.erase(0, pos+1);

FIELDS field;

field.key = token2;

field.value = token;

f_name_value_pairs[count++] = field;

}

}

}

  

/////////////////////////////////////////////

// param()

// Will receive the value for any given form field

/////////////////////////////////////////////

string param(string lookUp, FIELDS f_name_value_pairs [], int f_cnt)

{

for(int i=0;i<f_cnt;i++)

if(strcmp(loopup, f_name_value_pairs[i].key) == 0)

return f_name_value_pairs[i].value;

}

  

  

};

class FIELDS{

public:

string key;

string value;

};

Add a comment
Know the answer?
Add Answer to:
I am currently stuck with this portion of my assignment: Pointers review: to help determine how...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help of how to include the merge sort code into this counter code found...

    I need help of how to include the merge sort code into this counter code found below: #include<iostream> using namespace std; int bubble_counter=0,selection_counter=0; // variables global void bubble_sort(int [], int); void show_array(int [],int); void selectionsort(int [], int ); int main() { int a[7]={4,1,7,2,9,0,3}; show_array(a,7); //bubble_sort(a,7); selectionsort(a,7); show_array(a,7); cout<<"Bubble counter = "<<bubble_counter<<endl; cout<<"Selection Counter = "<<selection_counter<<endl; return 0; } void show_array(int array[],int size) { for( int i=0 ; i<size; i++) { cout<<array[i]<< " "; } cout<<endl; } void bubble_sort( int array[],...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • C++ problem with dynamic arrays is that once the array is created using the new operator...

    C++ problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This project asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have: A private member variable called dynamicArray that references a...

  • I have to type and explain in class each code in every detail filled with //...

    I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() {    int var1 = 20 ;    int var2 = 30 ;    int* ptr1 ;    int* ptr2 ;    int* temp ;    ptr1 = &var1 ;    ptr2 = &var2 ;    cout << *ptr1 << endl ;...

  • Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will...

    Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a copy constructor, (6) overload the assignment operator, (7) overload the insertion...

  • May i ask for help with this c++ problem? this is the code i have for assignment 4 question 2: #...

    may i ask for help with this c++ problem? this is the code i have for assignment 4 question 2: #include<iostream> #include<string> #include<sstream> #include<stack> using namespace std; int main() { string inputStr; stack <int> numberStack; cout<<"Enter your expression::"; getline(cin,inputStr); int len=inputStr.length(); stringstream inputStream(inputStr); string word; int val,num1,num2; while (inputStream >> word) { //cout << word << endl; if(word[0] != '+'&& word[0] != '-' && word[0] != '*') { val=stoi(word); numberStack.push(val); // cout<<"Val:"<<val<<endl; } else if(word[0]=='+') { num1=numberStack.top(); numberStack.pop(); num2=numberStack.top(); numberStack.pop();...

  • How can I write this code (posted below) using vectors instead of arrays? This is the...

    How can I write this code (posted below) using vectors instead of arrays? This is the task I have and the code below is for Task 1.3: Generate twenty random permutations of the number 0, 1, 2, ..., 9 using of the algorithms you designed for Task 1.3. Store these permutations into a vector and print them to the screen with the additional information of unchanged positions and number of calls torand(). Calculate the total numbers of unchanged positions. Compare...

  • My main() file does not call to the class created in a .hpp file. It comes...

    My main() file does not call to the class created in a .hpp file. It comes out with the error "undefined reference to "___" const. I want to solve this by only changing the main() .cpp file, not the .hpp file. .cpp file .hpp file Thank you! include <iostream> include <string> tinclude "CourseMember.hpp using namespace std int main0 CourseMember CourseMember(90, "Jeff", "Goldblum"): // initializing the constructor cout<< "Member ID is <<CourseMember.getID) << endl; 1/ calling getID) accessor cout <<"First Name...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT