1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
Creating survey using the webapps.h function and survey.cpp but wont compile
#include "WebApps.h"
#include "FileApps.h"
#include <iomanip>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype> //for isalnum()
using namespace std;
WebApps wo;
FileApps fo("survey.txt");
void build_form();
void save_data_line(string);
string prepare_vote(int);
void display_result(string[]);
int* create_int_array(int);
//******************************
//**begining of display_result**
//******************************
void display_result(string f_data_array[])
{
int *vote_tally_array = new int[2];
for (int i = 0; i < 2; i++)
{
vote_tally_array[i] = 0;
}
int start_pos = 0, pos;
string data, vote;
for (int i = 0; i < fo.get_survey_txt_cnt(); i++)
{
data = f_data_array[i];
pos = data.find("|", start_pos);
vote = data.substr(start_pos, pos);
if (vote == "Summer")
{
vote_tally_array[0]++;
}
else if (vote == "Fall")
{
vote_tally_array[1]++;
}
}
cout << "<br><br><br><b>Survey Results:</b><br><br>" << "Summer: " << vote_tally_array[0] << "<br><br>" << "Fall: " << vote_tally_array[1] << "<br><br>"
" <a href=http://toolkit.cs.ohlone.edu/~gen209/survey.txt > Survey.txt file </a><br><br> ";
}
//******************************
//**begining of main************
//******************************
int main()
{
const int cnt = wo.get_cnt();
string* data_array;
if (cnt != 0)
{
cout << "debug with cnt != 0<br>" << endl;
//call function to prepare vote
string data_line = prepare_vote(cnt);
cout << "debug with data_line: " << data_line << "<br>" << 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()<br>" << endl;
//array data_array[] is now ours (contains all of the votes as strings
data_array = fo.get_data_array();
cout << "From main() debug with 1th element: " << data_array[0] <<
"<br>" << endl;
// End of the need for FileApps.h under this if statement
}
else
{
build_form(); //calls the build_form function which shows the survey on the web
}
display_result(data_array);
return 0;
}
//******************************
//**begining of build_form**
//******************************
void build_form() { //this is the html file that users will use to actually take the survey
cout << "<html><head><title>Season Surveying Vote</title></head>" <<
"<body bgcolor=#42f4df>\n" <<
"<form action=\"survey.cgi\" method=\"GET\">\n" <<
" <br><b>Favorite season also let semester end</b><br><br>\n" <<
" <label for=\"vote\">Summer</label>\n" <<
" <input name=\"vote\" value=\"Y\" type=\"radio\"></td><p>\n" <<
" <label for=\"vote\">Fall</label>\n" <<
" <input name=\"vote\" value=\"N\" type=\"radio\"></td><p>\n" <<
" <p><input type=\"submit\" value=\"Finish Survey\"><br><br>" <<
" <a href=http://toolkit.cs.ohlone.edu/~gen209/survey.txt > Survey.txt file </a><br><br> "
"Hit ' Submission ' and see the results!<br>" <<
"</form>\n" <<
"</body>\n" <<
"</html>\n";
}
//******************************
//**begining of prepare_vote**
//******************************
string prepare_vote(int f_cnt) {
//create dynamic array name_value_pairs[] from the wo object
FIELDS * name_value_pairs = wo.create_array(f_cnt);
//parse qs into name_value_pairs[] array from the wo object
wo.parse(wo.get_qs(), name_value_pairs);
//param the vote field value into variable 'vote' from the wo object
string vote = wo.param("vote", name_value_pairs, f_cnt);
cout << "debug with vote: " << vote << "< br>" << endl;
return vote + "|\n";
}
#ifndef WEBAPPS_H
#define WEBAPPS_H
#include <iomanip>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
using namespace std;
struct FIELDS
{
string name;
string value;
};
class WebApps
{
private:
string qs;
int cnt;
int how_many(string f_qs);
public:
void parse(string qs, FIELDS f_name_value_pairs[]);
string param(string lookUp, FIELDS f_name_value_pairs[]);
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;
}
};
/////////////////////////////////////////////////////
// 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;
}
#endif //WEBAPPS_H
|