Final Project
A survey app with two classes
Objectives for the final project:
I.
II. Create an online survey
III. Due: Friday, December 13, last day of final’s week
In more details:
I.
II. Create an online survey:
III.
IV. The goal is to create a simple online survey and name it – survey.cgi. Allow users to
V. choose at the very least, between two choices (yes or no) on an issue of your choice
VI. (ok to work from html pages you’ve already created).
VII.
VIII. The vote will be saved to the server (code provided), and the result of all votes will be
IX. displayed on the screen. The result from the survey should show up every time, even
X. before we fill out the survey.
XI.
XII. This project will start from retrieve_form_OOP_2.cpp, by making use of the class
XIII. WebApps, which should already go as far as providing a dynamic array, containing the
XIV. fields from a form. However, the class will be moved from the main program to it’s own
XV. file, called a header file.
XVI.
XVII. Another difference from retrieve_form_OOP_2.cpp will be that an HTML form will not
XVIII. call the program. survey.cgi is a ‘dynamic cgi program’ (or self referential),
XIX. meaning that the code has to respond from two possibilities:
1.
2. the script can be called either from a link for the first time, which will cause it to
3. produce only the survey. That state corresponds to the first level ‘else’ in the code
4. below
5.
6. or the same script can be called from the ‘submit’ button of the form it has just
7. published. That state corresponds to the first level ‘if’ in the code provided.
XX.
XXI.
XXII. Here are steps to follow, in order to to convert retrieve_form_OOP_2.cpp into
XXIII. survey.cpp:
◦
◦ Also, here is a video to help explain steps A through D
XXIV.
XXV. Each capital letter corresponds to a significant stage in the development of this
XXVI. program. This document may seem lengthy, but parts of it also act as a tutorial.
XXVII.
XXVIII. It is recommended to save a copy of survey.cpp after completing each capital letter
XXIX. stage in case something goes wrong with the next one.
A.
B. 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.
C.
D.
E. Move the struct FIELDS and class WebApps to its own header file, named
F. WebApps.h. We are going to leave the function definitions inline within the class, for
G. simplicity-sake (though not ultimately recommended by industry)
1.
2. Read more info on Header Files.
3.
4. WebApps.h needs to live in the same directory as survey.cpp, so that it is available
5. at compile time – we do not compile .h files manually.
6.
7. WebApps.h should contain the same includes as survey.cpp, the ifndef/define/endif
8. directives, as a regular header file does, the FIELDS struct, and the class, pretty much
9. exactly as already working properly in retrieve_form_OOP_2.cpp.
10.
11. Check out the FileApps.h header file below as an example to follow for WebApps.h.
12.
13. All the main program needs now is an ‘include’ to the header file and should
14. compile and run.
15.
16. Create a temporary web_form_survey.html, based on the older web_form.html to
17. test the program until it runs as before (in retrieve_form_OOP_2.cgi).
H.
I. At this point, since this program is self referential, its first state is to serve the
J. HTML form for the survey.
K.
L. From here, the current code in main should be replaced completely (might be good to
M. just comment the whole thing and leave at the bottom of the program to reuse some
N. 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
▪ }
O.
P.
Q. This code is pretty self-explanatory, nothing new here, except the HTML form in
R. survey_start.txt demonstrates the tags for inputing from buttons, which may become
S. useful.
T.
U. This code represents the main flow of this program's execution. It does this:
a.
b. Gets the count from qs, if it's there
c. If the count is zero, then just a debug as a place holder for all the functions
d. (created next), which will take care of the vote, when received.
e. Else, the form is presented and several other functions will follow.
V.
W.
X. The HTML, of course will be your own design, which can follow previous work, but do
Y. include an option for 'don't know', just for your instructor who is quite clueless with
Z. super heroes :)
A.
B. Note: It is recommended to keep the HTML form minimal while working out
C. the rest of the program's functions.
D.
E. Make this work perfectly and back up!
F.
G. Now that the vote is at hand, time to save it to a text file. Here are the steps:
1.
2. Given is a new class, FileApps.h, which will be used to save to and read from
3. file: FileApps.h
4.
5. Remember the class needs to be in the same directory as where survey.cpp will be
6. compiled.
7.
8. File I/O operations are not trivial, therefore, the code is provided for this project, in
9. hope that the class can at least be used, if not completely understood. Here is a
10. link to I/O example code if interested. Also check out online book,
11. chapter 18, File I/O. There are other relevant tutorials if anyone is interested
12.
13. The constructor for class FileApps, expects a file name for the object to work with
14. - use this syntax for creating file object fo: FileApps fo ("survey.txt"). Place it above
15. main, so that it is global, as done with the WebApps object.
16.
17. survey.txt will be created automatically in the same directory as survey.cgi, upon first
18. saving to the file.
19.
20. Here is an updated if statement, which now contains code to retrieve the vote and
21. 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);
22.
23. The prepare_vote(cnt) function needs to be implemented (in the main program, not
24. 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 + "|\n"; //the new line character will put each vote
▪ //on a new line in the file
▪ //ex of a concatenated vote: "y|\n"
▪ Normally, this line of data would contain more fields, which would
▪ commonly be separated by pipes, like:
▪
▪ "y|Fred|Flintstone|\n" - represents data for three fields.
▪
▪ Therefore, even if we use only one
▪ field this time, we remain consistent with the protocol.
▪ }
25.
26. Once 'data_line' is sent to object method fo.save_data_line(), the debug statement
27. in FileApps.h should confirm success (of save) and survey.txt should be checked for
28. content
29. manually.
30.
31. Complete this stage before entering the next one and don't forget to back-up!
H.
I. Now, with the assumption that survey.txt captures the votes properly, survey.cpp is
J. ready to read all the vote data into an array (data_array[]) and pass it back to us.
K.
L. This code is entirely given, so get it here from survey_cpp_state_E.txt
M.
N. Again, not necessary to understand how the data is saved and retrieved from the class
O. code - just use it as shown. What is important is that you now have the data and ready
P. to tally and display the votes, the main course of this project.
Q.
R. Leave the debug statements from main and FileApps.h in the finish product!
S.
T. Finally, the last part:
1.
2. 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() (to be created under main(), not in the
▪ // class), 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 0’s in a loop
▪
▪ // In another loop, increment vote_tally_array from f_data_array[]
▪ // Use something like this pseudocode::
▪ // if vote == "Y|" then vote_tally_array[0]++
▪ // else if vote == "N|" then vote_tally_array[1]++
▪
▪ //print the results
▪ }
▪
3.
4.
5. Also, at the bottom of the result page, include a link to survey.txt in order to make
6. it available.
7.
8. 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
XXX.
XXXI. To turn in:
1.
2. Dropbox survey.cpp and WebApps.h
3.
4. Create a message in the discussion board providing a workable link to your
5. final project survey.cgi, as done before with the HTML and String Processing app.
6.
7. Remember these few steps:
a.
b. Firefox is the only browser, which will allow creating links in a toolkit message.
c.
d. In a new message for topic named 'survey threads', select the
e. check box Enable HTML
f.
g. In the body of the message, type the html 'a' tag (shown below) substituting
h. your own url information (and not using a space as it is done here):
i.
j. < a href="http://toolkit2.ohlone.edu/~username/survey.cgi">Some Descriptive
k. Name for the Project< /a>
8.
9. Take the time to participate in each others’ surveys!
XXXII.
XXXIII. Student Examples: there were some differences, such as the same user could not
XXXIV. vote more than once, otherwise – pretty close to this semester’s format, on the
XXXV. outside.
a.
b. Lawrence Simon
c. Anitha Gopalan
XXXVI.
XXXVII. Thank you for all of your work!
XXXVIII.
XXXIX. In hope this class has met your expectations and that it has prepared you sufficiently
XL. in your continuing education, cheers 🙂
XLI.