Project issues

Hi there I am having some trouble with a class project. this program is a survey that uses html to ask a question, supply 2 radio buttons for yes and no, then record the answer on a text file on our class server along with the voters IP address to prevent repeat votes. and then finally display the amount of votes as well as a link to the raw data as a final output. It is a .cgi file and has a function to create its own web page, so no extra html files needed.

Heres what I have so far:
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
  #include <iomanip>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>	//for isalnum()
using namespace std;

struct FIELDS 
{
	string name;
	string value;
};

void parse(string, FIELDS []);		//will go through qs and find the "=" to seperate fields
string param(string, FIELDS []);	//will check to make sure that the feilds match
int how_many(string);				//counts how many feilds there are 
FIELDS* create_FIELDS_array(int);	//creates a dynamic array
void create_form();					//creates html form
bool save_data();					//will save data to file named survey.txt
bool read_data();					
void display_result();				//will tally the votes and display the result in a format like this
									//Y|70.36.224.178 - with a \n at the end to create a new line for the next record
bool IP_not_duplicated(string);		//will make sure that there is no matching REMOTE_ADDR on the file

string qs(getenv("QUERY_STRING"));	//query string
const int cnt = how_many(qs); 		//how many feilds there are

int main()
{
		//string qs("wish=kjh&number=kh&emotion=kjh");
	cout << "Content-type:text/html\n\n";	//start of web page
	FIELDS* name_value_pairs;				//pointer of type FIELDS
	//fstream myfile;
	name_value_pairs= create_FIELDS_array(cnt);	//makes FIELDS pntr a dynamic array
	 
	cout << "Stage 2\n" << endl;				
	cout << "debug for cnt" << cnt << endl;		//cnt shows up fine
	parse (qs, name_value_pairs);				//this runs fine as well
	string vote = param("vote", name_value_pairs);	//will not return anything so far
	
	
	if (qs.length() != 0) {					
		cout << "debug with qs: " << qs << "<p>" << endl;
		cout << "debug for array" << name_value_pairs[0].value << endl;
		cout << "debug for vote" << vote << endl; 
		create_form();
		
		
	}
	else {

		create_form();
	}
	
    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 [])
{

	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;
		cout << "value: " << value << "" << endl;
		start_pos = pos + 1;
	}
	value = qs.substr(start_pos, qs.length() - start_pos);
	cout << "value: " << value << endl;
}
//*******************************************
// param()
// This will put the info into a new field
// on the web form
//*******************************************
string param(string lookUp, FIELDS f_name_value_pairs[])
{	
	string f_value;
	bool found = false;
	for (int index=0; index < cnt; index++)
	{
		if(f_name_value_pairs[index].name != lookUp);

		else {
			found = true;
			f_value = f_name_value_pairs[index].value;
		}
	} 
	if (found) 
		return f_value;
	else 
		return "nope";
}


int how_many (string qs)
{
	int pos=0;
	int count=0;
	int start_pos=0;

	do {
		pos = qs.find("=", start_pos);
		if (pos != string::npos){
		count++;
		start_pos= pos+1;
			}
		} while (pos != string::npos);
	return count;
}

FIELDS * create_FIELDS_array (int f_cnt)
{
	FIELDS * address;
	address = new FIELDS[f_cnt];
	return (address);
}

void create_form() {
	cout << "<html><head><title>Exercise Survey</title></head>" <<
			"<body>\n" <<
			"<form action=\"survey.cgi\" method=\"GET\">\n" <<
			"	Do You Prefer Running Over Biking?<br>\n" <<
			"		<label for=\"vote\">Yes</label>\n" <<
			"		<input name=\"vote\" value=\"Y\" type=\"radio\"></td><br>\n" <<
			"		<label for=\"vote\">No</label>\n" <<
			"		<input name=\"vote\" value=\"N\" type=\"radio\"></td><p>\n" <<		
			"	<p><input type=\"submit\" value=\"Submit\">\n" <<
			"</form>\n" <<
			"</body>\n" << 
			"</html>\n";

}



I know it not the cleanest (relatively new at programming) but any help will be extremely appreciated.

If youre wondering what the output looks like here is a link, just click on survey.cgi :http://toolkit.cs.ohlone.edu/~gen206/

Thanks for looking!
Last edited on
There should be no ; if(f_name_value_pairs[index].name != lookUp); at the end.
I would code it like this:
1
2
3
4
5
6
7
8
9
10
string param (string lookUp, FIELDS f_name_value_pairs[])
{
  for (int index = 0; index < cnt; index++)
  {
    if (f_name_value_pairs[index].name == lookUp);
      return f_name_value_pairs[index].value;
  }  
  
  return "nope";
}

Haven't you learned about vectors and maps yet ? Would be much easier.
Sadly no I have not, I will do some research on that and learn them. It only looks like that because we were given that code to start with. Made some changes and everything works smoothly past the param call.
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
#include <iomanip>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>	//for isalnum()
using namespace std;

struct FIELDS 
{
	string name;
	string value;
};

void parse(string, FIELDS []);		//will go through qs and find the "=" to seperate fields
string param(string, FIELDS []);	//will check to make sure that the feilds match
int how_many(string);				//counts how many feilds there are 
FIELDS* create_FIELDS_array(int);	//creates a dynamic array
void create_form();					//creates html form
bool save_data(ofstream &, const char *,string, string);					//will save data to file named survey.txt
bool read_data();					
void display_result();				//will tally the votes and display the result in a format like this
									//Y|70.36.224.178 - with a \n at the end to create a new line for the next record
bool IP_not_duplicated(string);		//will make sure that there is no matching REMOTE_ADDR on the file

string qs(getenv("QUERY_STRING"));

const int cnt = how_many(qs);

int main(){

	
	cout << "Content-type:text/html\n\n";
	create_form();
	ofstream surveyfile;
	string ra(getenv("REMOTE_ADDR"));
	FIELDS* name_value_pairs = create_FIELDS_array(cnt);
	cout << "Stage 2" << "<p>" << endl;
	parse (qs, name_value_pairs);
	string vote = param("vote", name_value_pairs);
	if (qs.length() != 0) {
		cout << "debug with qs: " << qs << "<p>" << endl;
		cout << "<p>" << endl;
		cout << "debug for .name after parse:" << name_value_pairs[0].name << "<p>" << endl;
		cout << "debug for .value after parse:" << name_value_pairs[0].value << "<p>" << endl;
		cout << "debug for vote after param:" << vote << "<p>" << endl;
		cout << "debug for IP:" << ra << "<p>" << endl;
		//save_data(surveyfile,"survey.txt", ra, vote);
	}
	else {

	}

	

	

	//

	//string IP(getenv("REMOTE_ADDR"));

	






	return 0;
}

void parse (string qs, FIELDS f_name_value_pairs [])
{

	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: " << f_name_value_pairs[counter].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;
		cout << "value: " << value << "" << endl;
		start_pos = pos + 1;
	}
	
}
//*******************************************
// param()
// This will put the info into a new field
// on the web form
//*******************************************
string param(string lookUp, FIELDS f_name_value_pairs[])
{	
	string f_value;
	bool found = false;
	
		if(f_name_value_pairs[0].name != lookUp);

		else {
			found = true;
			f_value = f_name_value_pairs[0].value;
		}
	 
	if (found) 
		return f_value;
	else 
		return "";
}


int how_many (string qs)
{
	int pos=0;
	int count=0;
	int start_pos=0;

	do {
		pos = qs.find("=", start_pos);
		if (pos != string::npos){
		count++;
		start_pos= pos+1;
			}
		} while (pos != string::npos);
	return count;
}

FIELDS * create_FIELDS_array (int f_cnt)
{
	FIELDS * address;
	address = new FIELDS[f_cnt];
	return (address);
}

void create_form() {
	cout << "<html><head><title>Exercise Survey</title></head>" <<
			"<body>\n" <<
			"<form action=\"survey.cgi\" method=\"GET\">\n" <<
			"	Do You Prefer Running Over Biking?<br>\n" <<
			"		<label for=\"vote\">Yes</label>\n" <<
			"		<input name=\"vote\" value=\"Y\" type=\"radio\"></td><br>\n" <<
			"		<label for=\"vote\">No</label>\n" <<
			"		<input name=\"vote\" value=\"N\" type=\"radio\"></td><p>\n" <<		
			"	<p><input type=\"submit\" value=\"Submit\">\n" <<
			"</form>\n" <<
			"</body>\n" << 
			"</html>\n";

}

bool save_data(ofstream &surveyfile, char const *file_name, string f_ra, string f_vote)
{
	surveyfile.open (file_name, ios::app);
	string new_line;
		if (surveyfile.fail())
			return false;
		else
			new_line= f_vote + "|" + f_ra + "\n";
			return true;

}


I have moved on to the next part of the code which should save the vote, and remote address to the text file on the server. Testing for errors now but I get these errors upon compiling:
survey.cpp: In function `int main()':
survey.cpp:35: error: aggregate `std::ofstream surveyfile' has incomplete type
and cannot be defined
survey.cpp: In function `bool save_data(std::ofstream&, const char*,
std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
survey.cpp:159: error: `open' undeclared (first use this function)
survey.cpp:159: error: (Each undeclared identifier is reported only once for
each function it appears in.)
survey.cpp:161: error: `fail' undeclared (first use this function)


looks like it has to do with my ofstream variable, and something in the save_data function?
To use ofstream you need to include <fstream>
Yah I thought it was there, guess I overlooked it haha. Put it in and it is working good, but the reading the data in the text file, counting the votes and putting the votes in a dynamic array part is becoming tedious for me.

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
#include <iomanip>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <fstream>	//for isalnum()
using namespace std;

struct FIELDS 
{
	string name;
	string value;
};

void parse(string, FIELDS []);		//will go through qs and find the "=" to seperate fields
string param(string, FIELDS []);	//will check to make sure that the feilds match
int how_many(string);				//counts how many feilds there are 
FIELDS* create_FIELDS_array(int);	//creates a dynamic array
void create_form();					//creates html form
void save_data(ofstream &, const char *,string, string);					//will save data to file named survey.txt
void read_data(ifstream &, const char *);					
void display_result();	
int count_records(ifstream &);			//will tally the votes and display the result in a format like this
void populate_survey_data_array(ifstream &, FIELDS [],int);		//Y|70.36.224.178 - with a \n at the end to create a new line for the next record
bool IP_not_duplicated(string);		//will make sure that there is no matching REMOTE_ADDR on the file

string qs(getenv("QUERY_STRING"));
const int cnt = how_many(qs);

int main(
{	
	cout << "Content-type:text/html\n\n";
	create_form();
	ofstream surveyfile;
	ifstream input_file;
	string ra(getenv("REMOTE_ADDR"));
	FIELDS* name_value_pairs = create_FIELDS_array(cnt);
	cout << "Stage 3" << "<p>" << endl;
	parse (qs, name_value_pairs);
	string vote = param("vote", name_value_pairs);
	if (qs.length() != 0) {
		cout << "debug with qs: " << qs << "<p>" << endl;
		cout << "<p>" << endl;
		cout << "debug for .name after parse:" << name_value_pairs[0].name << "<p>" << endl;
		cout << "debug for .value after parse:" << name_value_pairs[0].value << "<p>" << endl;
		cout << "debug for vote after param:" << vote << "<p>" << endl;
		cout << "debug for IP:" << ra << "<p>" << endl;
		save_data(surveyfile,"survey.txt", ra, vote);
		read_data(input_file, "survey.txt");
	}
	else {
			create_form();
	}
	return 0;
}

void parse (string qs, FIELDS f_name_value_pairs [])
{
	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: " << f_name_value_pairs[counter].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;
		cout << "value: " << value << "" << endl;
		start_pos = pos + 1;
	}
	
}

string param(string lookUp, FIELDS f_name_value_pairs[])
{	
	string f_value;
	bool found = false;
		if(f_name_value_pairs[0].name != lookUp);
		else {
			found = true;
			f_value = f_name_value_pairs[0].value;
		}
	if (found) 
		return f_value;
	else 
		return "";
}

int how_many (string qs)
{
	int pos=0;
	int count=0;
	int start_pos=0;
	do {
		pos = qs.find("=", start_pos);
		if (pos != string::npos){
		count++;
		start_pos= pos+1;
			}
		} while (pos != string::npos);
	return count;
}

FIELDS * create_FIELDS_array (int f_cnt)
{
	FIELDS * address;
	address = new FIELDS[f_cnt];
	return (address);
}

void create_form() {
	cout << "<html><head><title>Exercise Survey</title></head>" <<
			"<body>\n" <<
			"<form action=\"survey.cgi\" method=\"GET\">\n" <<
			"	Do You Prefer Running Over Biking?<br>\n" <<
			"		<label for=\"vote\">Yes</label>\n" <<
			"		<input name=\"vote\" value=\"Y\" type=\"radio\"></td><br>\n" <<
			"		<label for=\"vote\">No</label>\n" <<
			"		<input name=\"vote\" value=\"N\" type=\"radio\"></td><p>\n" <<		
			"	<p><input type=\"submit\" value=\"Submit\">\n" <<
			"</form>\n" <<
			"</body>\n" << 
			"</html>\n";
}

void save_data(ofstream &surveyfile, char const *file_name, string f_ra, string f_vote)
{
	surveyfile.open (file_name, ios::app);
	string new_line;
		if (surveyfile.fail());
			
		else{
			new_line= f_vote + "|" + f_ra + "\n";
			surveyfile << new_line;
			}
	surveyfile.close();
}

void read_data(ifstream &input_file, char const *file_name)
{
	input_file.open (file_name);
	if (input_file.fail());
	else{
		int vote_count=count_records(input_file);
		FIELDS* data_array = create_FIELDS_array(cnt);
		populate_survey_data_array(input_file, data_array, vote_count);
	}
}
int count_records(ifstream &surveyfile) 		//responsible for counting the votes
{
	cout << "Counting how many records in input_file_open ... " << endl;	
	int survey_text_count = 0;	//Extra record gets counted at the end of the file
	while (!surveyfile.fail()) 
	{
		string data;
		getline(surveyfile, data); 
		survey_text_count++;
		cout << "data is :" << data << endl;
	}
	return survey_text_count -1;
}

void populate_survey_data_array (ifstream &f_input_file, FIELDS f_data_pairs, int vote_count)
{
	f_input_file.clear();
	f_input_file.seekg(0L, ios::beg);
	string data;
	for (int i=0; i<vote_count;i++)
	{
		getline(f_input_file, data);
		f_data_pairs[i]=data;
	}
}


only lines 145 to 179 are new, just figured I should put the whole thing on there. Getting an error that says: no match for 'operator[]' in 'f_data_pairs[i]'
Shouldn't FIELDS f_data_pairs be a pointer or array?
void populate_survey_data_array (ifstream &f_input_file, FIELDS f_data_pairs, int vote_count)
What should change in order to make that happen, because I know data_array that gets defined on line 151 is a dynamic array. So in order to use it in that way should I change the parameters of the function? Or something inside of the function its self?

Thanks for all the help so far, really appreciated!
got everything down in the code except for the IP check to prevent duplicates.

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
#include <iomanip>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <fstream>	//for isalnum()
using namespace std;

struct FIELDS 
{
	string name;
	string value;
};

void parse(string, FIELDS []);		//will go through qs and find the "=" to seperate fields
string param(string, FIELDS []);	//will check to make sure that the feilds match
int how_many(string);				//counts how many feilds there are 
FIELDS* create_FIELDS_array(int);	//creates a dynamic array
void create_form();					//creates html form
void save_data(ofstream &, const char *,string, string);					//will save data to file named survey.txt
void read_data(ifstream &, const char *);					
void display_result(string);	
int count_records(ifstream &, const char *);			//will tally the votes and display the result in a format like this
void populate_survey_data_array(ifstream &,int);		//Y|70.36.224.178 - with a \n at the end to create a new line for the next record
bool IP_not_duplicated(string);
string * create_array();		//will make sure that there is no matching REMOTE_ADDR 
string qs(getenv("QUERY_STRING"));
const int cnt = how_many(qs);
FIELDS* name_value_pairs = create_FIELDS_array(cnt);
ifstream input_file;
int vote_count=count_records(input_file, "survey.txt");
string* data_array= create_array();

int main(){
	cout << "Content-type:text/html\n\n";
	create_form();
	ofstream surveyfile;
	string ra(getenv("REMOTE_ADDR"));
	cout << "Stage 4" << "<p>" << endl;
	parse (qs, name_value_pairs);
	string vote = param("vote", name_value_pairs);
	if (qs.length() != 0) {
		cout << vote_count << endl;
		cout << data_array[0];
		cout << "<p>" << endl;
		save_data(surveyfile,"survey.txt", ra, vote);
		read_data(input_file, "survey.txt");
		display_result(vote);
	}
	else {
			create_form();
	}
	return 0;
}
void parse (string qs, FIELDS f_name_value_pairs [])
{
	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;
		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;
		start_pos = pos + 1;
	}
}
string param(string lookUp, FIELDS f_name_value_pairs[])
{	
	string f_value;
	bool found = false;
		if(f_name_value_pairs[0].name != lookUp);
		else {
			found = true;
			f_value = f_name_value_pairs[0].value;
		}
	if (found) 
		return f_value;
	else 
		return "";
}
int how_many (string qs)
{
	int pos=0;
	int count=0;
	int start_pos=0;

	do {
		pos = qs.find("=", start_pos);
		if (pos != string::npos){
		count++;
		start_pos= pos+1;
			}
		} while (pos != string::npos);
	return count;
}
FIELDS * create_FIELDS_array (int f_cnt)
{
	FIELDS * address;
	address = new FIELDS[f_cnt];
	return (address);
}
void create_form() {
	cout << "<html><head><title>Exercise Survey</title></head>" <<
			"<body>\n" <<
			"<form action=\"survey.cgi\" method=\"GET\">\n" <<
			"	Do You Prefer Running Over Biking?<br>\n" <<
			"		<label for=\"vote\">Yes</label>\n" <<
			"		<input name=\"vote\" value=\"Y\" type=\"radio\"></td><br>\n" <<
			"		<label for=\"vote\">No</label>\n" <<
			"		<input name=\"vote\" value=\"N\" type=\"radio\"></td><p>\n" <<		
			"	<p><input type=\"submit\" value=\"Submit\">\n" <<
			"</form>\n" <<
			"</body>\n" << 
			"</html>\n";
}
void save_data(ofstream &surveyfile, char const *file_name, string f_ra, string f_vote)
{
	surveyfile.open (file_name, ios::app);
	string new_line;
		if (surveyfile.fail());
			
		else{
			new_line= f_vote + "|" + f_ra + "\n";
			surveyfile << new_line;
		}
	surveyfile.close();
}
void read_data(ifstream &input_file, char const *file_name)
{
	input_file.open (file_name);
	if (input_file.fail());
	else{
		count_records(input_file, "survey.txt");
		populate_survey_data_array(input_file, vote_count);
	}
}
int count_records(ifstream &input_file, char const *file_name) 		//responsible for counting the votes
{	
	int survey_text_count = 0;
	input_file.open(file_name);	//Extra record gets counted at the end of the file
	while (!input_file.fail()) 
	{
		string data;
		getline(input_file, data); 
		survey_text_count++;
	}
	input_file.close();
	return survey_text_count;
		//there is an extra line in text file
}
void populate_survey_data_array (ifstream &f_input_file, int vote_count)
{
	//The following two lines bring the file cursor back to beginning of file - after
	//	having been left at the end after count_records function
	f_input_file.clear();
	f_input_file.seekg(0L, ios::beg);
	string data;
	//for loop code here
	for (int i=0; i<vote_count;i++)
	{
		getline(f_input_file, data);
		data_array[i] = data;	
	}
}
string * create_array ()
{
	string * address;
	address = new string[vote_count];
	return (address);
}
void display_result(string f_vote)
{
	int tally_array[1];

	for (int i =0; i<vote_count; i++){
		if (f_vote=="Y")
			tally_array[0]++;
		else (f_vote=="N");
			tally_array[1]++;
	}
	cout << "Yes| " << tally_array[0] << "<p>" << endl;
	cout << " No| " << tally_array[1] << "<p>" << endl;
}


Tally_array is not giving a good value, gives a garbage value. Not too sure where the problem is, count_records is doing fine at counting the votes, could be data_array that isnt being initialized. I know this is a very crude way to run this code, but trying to get good at the basics in order to understand how the more complex forms of these functions work.

Also I know that a for loop can be used if I can some how get vote to be globally declared, but every time I call param before the html header the file wont run.
Last edited on
cout << " No| " << tally_array[1] << "<p>" << endl;
The array index is out of bounds.

If you need two entries you need to declare it like this:
int tally_array[2];

You also need to initialize your array.

..\WebForm.cpp: In function 'void display_result(std::string)':
..\WebForm.cpp:197:7: warning: 'tally_array[1]' may be used uninitialized in this function [-Wmaybe-uninitialized]
int tally_array[1];
^
..\WebForm.cpp:207:35: warning: 'tally_array[0]' may be used uninitialized in this function [-Wmaybe-uninitialized]
cout << "Yes| " << tally_array[0] << "<p>" << endl;
Last edited on
Okay, so I got the program to stop displaying garbage values for the tally_array, but now it just gives me the total count for both, not just the yes votes, and not just the no votes:

45

Yes| 45

No| 45

first number is total votes, and then it just repeats that number.

set tally_array[0] and [1] to 0.
One problem is your display_result function.
for (int i =0; i<vote_count; i++){
You count the same vote over and over again.

1
2
3
4
if (f_vote=="Y")
			tally_array[0]++;
		else (f_vote=="N");
			tally_array[1]++;


The ; at the end of else lead always to tally_array[1]++
It doesn't matter if f_vote == "Y" or "N" bith counter's will be incremented.
How can I fix that? I got rid of the ";" next to the else statement and now its incrementing yes or no:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void display_result(string f_vote)
{
	int tally_array[2];
	tally_array[0]=0;
	tally_array[1]=0;

	for (int i =0; i<vote_count; i++){
		if (f_vote=="Y")
			tally_array[0]++;
		
		
		else if(f_vote=="N")
			tally_array[1]++;
		
		
	}
	cout << "Yes| " << tally_array[0] << "<p>" << endl;
	cout << " No| " << tally_array[1] << "<p>" << endl;

}


display result is called as display_result(vote);

and vote is: string vote= param("vote", name_value_pairs);

I know that vote is being defined correctly due to the debug statements I had.

Is there a way to check and see if the array string *data_array is being populated?
Just print the data in populate_survey_data_array() to see what you have read.

In display_result you need to iterate through the array where the data istored and count each item.

What is this project? Is it a real app or homework ?

I guess it could be called non graded homework. It was a "see how far you could get in a week". We were allowed to use any resources we could get our hands on. Class Actually ended yesterday, the only problem this app had was the vote counting. Thanks for the help with this, it really did help me alot.
Topic archived. No new replies allowed.