Dec 6, 2012 at 9:37am UTC
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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
#include<iostream>
#include<fstream>
#include<iomanip>
#include<vector>
#include<string>
using namespace std;
char getMenuChoice();
void verticalSpace(int );
class countyElectionResults{
friend ostream &operator <<(ostream &, countyElectionResults);
friend istream &operator >>(istream &, countyElectionResults &);
public :
countyElectionResults();
countyElectionResults(string);
bool loadFile(string);
string getCountyName();
vector<char > getVotes();
void showVotes();
int getNullifierVotes();
int getFielditeVotes();
int getOtherVotes();
int getTotalVotes();
countyElectionResults * getNext();
void setNext(countyElectionResults*);
private :
string countyName;
vector<char > votes;
int nullifierVotes;
int fielditeVotes;
int otherVotes;
void countVotes();
countyElectionResults * next;
};
ostream &operator <<(ostream &output, countyElectionResults results){
output << setw(25) << results.countyName
<< setw(15) << results.fielditeVotes
<< setw(15) << results.nullifierVotes
<< setw(15) << results.otherVotes;
return output;
}
istream &operator >>(istream &input, countyElectionResults &results){
char vote;
getline(input, results.countyName);
while (input >> vote){
results.votes.push_back(vote);
}
results.countVotes();
return input;
}
countyElectionResults * countyElectionResults::getNext(){
return next;
}
void countyElectionResults::setNext(countyElectionResults* nextNode){
next = nextNode;
}
countyElectionResults::countyElectionResults(){
nullifierVotes = 0;
fielditeVotes = 0;
otherVotes = 0;
next = NULL;
}
countyElectionResults::countyElectionResults(string filename){
nullifierVotes = 0;
fielditeVotes = 0;
otherVotes = 0;
next = NULL;
loadFile(filename);
}
bool countyElectionResults::loadFile(string filename){
ifstream countyFile;
char vote;
countyFile.open(filename.c_str());
if (!countyFile){
cout << "File not found: " << filename << endl;
return false ;
}
// Read in the first line with the county name
getline(countyFile, countyName);
while (countyFile >> vote){
votes.push_back(vote);
}
countVotes();
return true ;
}
void countyElectionResults::showVotes(){
cout << setw(15) << countyName << endl;
for (int i = 0; i < votes.size(); i++){
cout << setw(15) << votes[i] << " " <<endl;
}
}
string countyElectionResults::getCountyName(){
return countyName;
}
vector<char > countyElectionResults::getVotes(){
return votes;
}
int countyElectionResults::getNullifierVotes(){
return nullifierVotes;
}
int countyElectionResults::getFielditeVotes(){
return fielditeVotes;
}
int countyElectionResults::getOtherVotes(){
return otherVotes;
}
int countyElectionResults::getTotalVotes(){
return votes.size();
}
void countyElectionResults::countVotes(){
nullifierVotes = 0;
fielditeVotes = 0;
otherVotes = 0;
for (int i = 0; i < votes.size(); i ++){
switch (votes[i]){
case 'N' :
case 'n' :
nullifierVotes ++;
break ;
case 'F' :
case 'f' :
fielditeVotes ++;
break ;
default :
otherVotes ++;
}
}
}
class countyElectionList{
public :
countyElectionList();
countyElectionResults at(int );
void push_back(countyElectionResults *);
int size();
bool empty();
private :
countyElectionResults * head;
int elementCount;
};
// A constructor that initializes the private data members
countyElectionList::countyElectionList(){
countyElectionList::head = NULL;
}
// This returns the countyElectionResults result at a particular point
// in the list.
// This is analogous to the at method in the vector class.
countyElectionResults countyElectionList::at(int place){
countyElectionResults * current = head;
while (current != NULL){
if (place==current->getNullifierVotes()){
return *current;
}
current = current->getNext();
}
}
// This method adds a countyElectionResults object to the end of the list.
// It is analogous to the push_back method in the vector class.
void countyElectionList::push_back(countyElectionResults * newCER){
if (head == NULL)
head = newCER;
else
{
countyElectionResults * current = head;
while (current->getNext() != NULL){
current = current->getNext();
}
current->setNext(newCER);
}
}
// This method returns the number of elements on the list.
// It is analogous to the size method in the vector class
int countyElectionList::size(){
int count = 0;
countyElectionResults * current = head;
while (current != NULL){
count ++;
current = current->getNext();
}
return count;
}
//This method returns true if the list has no elements
// It is analogous to the empty method in the vector class.
bool countyElectionList::empty(){
return head == NULL;
}
class stateElectionResults {
public :
void addCountyFile();
void showAllCountyResults();
void showCountyResults(string);
void showCountyVotes(string);
void listCounties();
private :
countyElectionList stateResults;
};
void stateElectionResults::addCountyFile(){
string countyFileName;
int voteCount = 0;
cout << "Please enter the county file name to add:" ;
getline(cin, countyFileName);
countyElectionResults * newResults = new (countyElectionResults);
if (newResults->loadFile(countyFileName)){
stateResults.push_back(newResults);
}
}
void stateElectionResults::showAllCountyResults(){
int nullifierVotes = 0;
int fielditeVotes = 0;
int otherVotes = 0;
verticalSpace(5);
cout << setw(25) << "County Name"
<< setw(15) << "Fieldite Votes"
<< setw(15) << "Nullifier Votes"
<< setw(15) << "Other Votes"
<< endl;
for (int i = 0; i < stateResults.size(); i++){
cout << stateResults.at(i) << endl;
nullifierVotes += stateResults.at(i).getNullifierVotes();
fielditeVotes += stateResults.at(i).getFielditeVotes();
otherVotes += stateResults.at(i).getOtherVotes();
}
cout << setw(25) << "Total:"
<< setw(15) << fielditeVotes
<< setw(15) << nullifierVotes
<< setw(15) << otherVotes
<< endl;
verticalSpace(5);
}
void stateElectionResults::showCountyResults(string countyName){
verticalSpace(5);
for (int i = 0; i < stateResults.size(); i++){
if (stateResults.at(i).getCountyName() == countyName){
cout << stateResults.at(i) << endl;
break ;
}
}
verticalSpace(5);
}
void stateElectionResults::showCountyVotes(string countyName){
for (int i = 0; i < stateResults.size(); i++){
if (stateResults.at(i).getCountyName() == countyName){
stateResults.at(i).showVotes();
break ;
}
}
}
void stateElectionResults::listCounties(){
cout << setw(25) << "COUNTIES" << endl;
for (int i = 0; i < stateResults.size(); i++){
cout << setw(25) << stateResults.at(i).getCountyName() << endl;
}
}
int main(){
stateElectionResults theResults;
bool done = false ;
char choice;
string county;
do {
choice = getMenuChoice();
switch (choice){
case 'Q' :
case 'q' :
done = true ;
break ;
case 'A' :
case 'a' :
theResults.addCountyFile();
break ;
case 'R' :
case 'r' :
theResults.showAllCountyResults();
break ;
case 'L' :
case 'l' :
theResults.listCounties();
break ;
case 'C' :
case 'c' :
cout << "Please enter the county name: " ;
getline(cin,county);
theResults.showCountyResults(county);
break ;
case 'V' :
case 'v' :
cout << "Please enter the county name: " ;
getline(cin,county);
theResults.showCountyVotes(county);
break ;
default :
cout << "Unknown choice. Please try again" << endl;
}
} while (!done);
}
void verticalSpace(int space){
for (int i = 0; i < space; i++){
cout << endl;
}
}
char getMenuChoice(){
char choice;
verticalSpace(5);
cout << setw(45) << "A: Add a county file" << endl;
cout << setw(45) << "L: List counties" << endl;
cout << setw(45) << "R: show all results" << endl;
cout << setw(45) << "C: show single county result" << endl;
cout << setw(45) << "V: verify single county votes" << endl;
cout << setw(45) << "Q: quit" << endl;
cout << "Your choice: " ;
cin >> choice;
string trash;
getline (cin,trash);
verticalSpace(5);
return choice;
}
Last edited on Dec 6, 2012 at 10:06am UTC
Dec 6, 2012 at 9:38am UTC
I am struggling with linked lists and how to fix segmentation faults for the code above.
The assignment is that I need to write a code that reads an indeterminate amount of files full of voting information and outputs certain aspects of these files with respect to the user input. However, I must use linked lists instead of vectors aside from one vector that is full of the votes read.
The text files will come in the form of:
County
n
f
f
n
v
etc.
Where county is the county name and the letters are the votes cast.
I am almost certain the problem lies in one of three methods.
212 countyElectionResults countyElectionList::at(int place){ ((I am having trouble with this method regardless of whether the segmentation fault stems from this. How exactly do I return all of the results rather than just one party?))
246 int countyElectionList::size(){
OR 263 bool countyElectionList::empty(){
I am so sorry for the long post, but I wanted to be as thorough as possible. The segmentation fault occurs whenever the user enters L, R, C, or V. Thank you in advance!
Dec 6, 2012 at 11:18am UTC
Return a pointer?
If the idea is to return the 'nth' element of the list, the code for at should probably be more along the lines of:
1 2 3 4 5 6 7 8 9 10 11
countyElectionResults countyElectionList::at(int place){
unsigned count = 0 ;
countyElectionResults * current = head;
while (count < place && current->getNext() ){
++count ;
current = current->getNext();
}
return *current ;
}
That just returns the last element if place happens to exceed the size of the list.
[Edit: By the way, memory allocated with new should be delete'd at some point. You should probably also look into when it might be appropriate to define a destructor, copy constructor and copy assignment operator.]
Last edited on Dec 6, 2012 at 11:21am UTC
Dec 6, 2012 at 11:41am UTC
Ah that makes so much more sense! Thank you for the explanation!