Question
C++ help building these Create an online survey: The goal is to create a simple online survey and name it - survey.cgi. Allow users to
C++ help building these
Create an online survey: The goal is to create a simple online survey and name it - survey.cgi. Allow users to choose at the very least, between two choices (yes or no) on an issue of your choice (ok to work from html pages you've already created).
The vote will be saved to the server (code provided), and the result of all votes will be displayed on the screen. The result from the survey should show up every time, even before we fill out the survey.
This project will start from retrieve_form_OOP_2.cpp, by making use of the class WebApps, which should already go as far as providing a dynamic array, containing the fields from a form. However, the class will be moved from the main program to a header file.
Another difference from retrieve_form_OOP_2.cpp will be that an HTML form will not call the program. survey.cgi is a 'dynamic cgi program' (or self referential), meaning that the code has to respond from two possibilities:
the script can be called either from a link for the first time, which will cause it to produce only the survey. That state corresponds to the first level 'else' in the code below
or the same script can be called from the 'submit' button of the form it has just published. That state corresponds to the first level 'if' in the code provided.
Here are steps to follow, in order to to convert retrieve_form_OOP_2.cpp into survey.cpp:
Also, here is a video to help explain steps A through D: https://www.youtube.com/watch?v=-m_WCkV21lI&feature=youtu.be
Each capital letter corresponds to a significant stage in the development of this program. This document may seem lengthy, but parts of it also act as a tutorial. It is recommended to save a copy of survey.cpp after completing each capital letter stage in case something goes wrong with the next one.Duplicate retrieve_form_OOP_2.cpp to survey.cpp
Note: Friday this week, a copy of retrieve_form_OOP_2.cpp will be provided for those whose version did not work so well.
Move the struct FIELDS and class WebApps to its own header file, named WebApps.h. We are going to leave the function definitions inline within the class, for simplicity-sake (though not ultimately recommended by industry)
Read more info on Header Files.
WebApps.h needs to live in the same directory as survey.cpp, so that it is available at compile time - we do not compile .h files manually.
WebApps.h should contain the same includes as survey.cpp, the ifndef/define/endif directives, as a regular header file does, the FIELDS struct, and the class, pretty much exactly as already working properly in retrieve_form_OOP_2.cpp. Check out the FileApps.h header file below as an example to follow for WebApps.h.
All the main program needs now is an 'include' to the header file and should compile and run.
Create a temporary web_form_survey.html, based on the older web_form.html to test the program until it runs as before (in retrieve_form_OOP_2.cgi).
At this point, since this program is self referential, its first state is to serve the HTML form for the survey. From here, the current code in main should be replaced completely (might be good to just comment the whole thing and leave at the bottom of the program to reuse some of the code later) and start from the following skeleton (copy from this link to survey_start.txt):
WebApps wo; // make object wo global void build_form(); int main() { const int cnt = wo.get_cnt(); if (cnt != 0) { cout << "debug with cnt != 0" << endl; }else { build_form(); } return 0; } ////////////functions///////////// void build_form () { //simple HTML form to get started //use the one from survey_start.txt if you wish }
This code is pretty self-explanatory, nothing new here, except the HTML form in survey_start.txt demonstrates the tags for inputing from buttons, which may become useful. This code represents the main flow of this program's execution. It does this:
Gets the count from qs, if it's there
If the count is zero, then just a debug as a place holder for all the functions (created next), which will take care of the vote, when received.
Else, the form is presented and several other functions will follow.
The HTML, of course will be your own design, which can follow previous work, but do include an option for 'don't know', just for your instructor who is quite clueless with super heroes :) Note: It is recommended to keep the HTML form minimal while working out the rest of the program's functions. Make this work perfectly and back up!Now that the vote is at hand, time to save it to a text file. Here are the steps:
Given is a new class, FileApps.h, which will be used to save to and read from file: FileApps.h Remember the class needs to be in the same directory as where survey.cpp will be compiled. File I/O operations are not trivial, therefore, the code is provided for this project, in hope that the class can at least be used, if not completely understood. Here is a link to I/O example code if interested. Also check out online book, chapter 18, File I/O. There are other relevant tutorials if anyone is interested
The constructor for class FileApps, expects a file name for the object to work with - use this syntax for creating file object fo: FileApps fo ("survey.txt"). Place it above main, so that it is global, as done with the WebApps object. survey.txt will be created automatically in the same directory as survey.cgi.
Here is an updated if statement, which now contains code to retrieve the vote and save it to survey.txt upon receiving a non-blank qs
if (cnt != 0) { cout << "debug with cnt != 0< br>" << endl; //call function to prepare vote string data_line = prepare_vote(cnt); //to be implemented, //explained next cout << "debug with data_line: " << data_line << "< br>" << endl; fo.save_data_line(data_line);
The prepare_vote(cnt) function needs to be implemented (in the main program, not the class FileApps) - here are the specs:
string prepare_vote(int f_cnt) { //create dynamic array name_value_pairs[] from the wo object //as done earlier statement here //parse qs into name_value_pairs[] array from the wo object //as done earlier statement here //param the vote field value into variable 'vote' from the wo object //as done earlier statement here cout << "debug with vote: " << vote << "< br>" << endl; return vote + "| "; //the new line character will put each vote //on a new line in the file //ex of a concatenated vote: "y| " Normally, this line of data would contain more fields, which would commonly be separated by pipes, like: "y|Fred|Flintstone| " - represents data for three fields. Therefore, even if we use only one field this time, we remain consistent with the protocol. }
Once 'data_line' is sent to object method fo.save_data_line(), the debug statement in FileApps.h should confirm success (of save) and survey.txt should be checked for content manually. Complete this stage before entering the next one and don't forget to back-up!
Now, with the assumption that survey.txt captures the votes properly, survey.cpp is ready to read all the vote data into an array (data_array[]) and pass it back to us. This code is entirely given, so get it here:
WebApps wo; //make the wo object global FileApps fo("survey.txt"); //fo for file object void build_form(); void save_data_line(string); string prepare_vote(int); int main() { const int cnt = wo.get_cnt(); if (cnt != 0) { cout << "debug with cnt != 0 " << endl; //call function to prepare vote string data_line = prepare_vote(cnt); cout << "debug with data_line: " << data_line << " " << endl; //save_data_line() appends the vote to the end of survey.txt fo.save_data_line(data_line); //read_data() opens survey.txt and populates array data_array[] fo.read_data (); cout << "debug past read_data() " << endl; //array data_array[] is now ours (contains all of the votes as strings string* data_array = fo.get_data_array(); cout << "From main() debug with 1th element: " << data_array[0] << " " << endl; // End of the need for FileApps.h under this if statement }else { build_form(); } return 0; }
Again, not necessary to understand how the data is saved and retrieved from the class code - just use it as shown. What is important is that you now have the data and ready to tally and display the votes, the main course of this project. Leave the debug statements from main and FileApps.h in the finish product!
Finally, the last part:Just a few comments, to help with the mechanics of this function:
void display_result (string f_data_array[]) { //function flow: // initialize a dynamic array (vote_tally_array) from function // create_int_array() which returns a pointer to an array with number of // elements equal to the number of possible (different) choices of votes: // For example, Y or N will require a vote_tally_array of 2 elements //initialize vote_tally_array to 0s in a loop //In another loop, increment vote_tally_array from f_data_array[] // vote will need to be extracted from its pipe symbol separator // if vote == "Y" then vote_tally_array[0]++ // else if vote == "N" then vote_tally_array[1]++ //print the results }
Also, at the bottom of the result page, include a link to survey.txt in order to make it available.
To complete the program flow, follow this format:if (vote coming in)
prepare vote save data read data get data_array build form display result
else (no vote)
build form read data get data_array display result
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started