Why are my output numbers so large and random?
Nov 21, 2012 at 2:06am UTC
Hey everybody. I'm writing a program that will take a text file and output how many votes each candidate received. It's pretty straightforward, except for the fact that I keep getting very unexpected output numbers.
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
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
class countyElectionResults{
// friend ostream &operator<<(ostream &, countyElectionResults);
public :
countyElectionResults();
countyElectionResults(string);
bool loadFile(string);
string getCountyName();
vector<char > getVotes();
void showVotes();
int getNullifierVotes();
int getFielditeVotes();
int getOtherVotes();
int getTotalVotes();
private :
string countyName;
vector<char > votes;
int nullifierVotes;
int fielditeVotes;
int otherVotes;
void countVotes();
};
countyElectionResults::countyElectionResults() {
string countyName = " " ;
int nullifierVotes = 0;
int fielditeVotes = 0;
int otherVotes = 0;
}
bool countyElectionResults::loadFile(string filename){
ifstream countyFile;
char possibilities;
countyFile.open(filename.c_str());
if (!countyFile.good()){
return false ;
}
else getline(countyFile, countyName);
while (countyFile >> possibilities){
votes.push_back(possibilities);
}
return true ;
}
void countyElectionResults::countVotes() {
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++;
break ; }
}
}
}
string countyElectionResults::getCountyName(){
return countyName;
}
vector<char > countyElectionResults::getVotes(){
return votes;
}
void countyElectionResults::showVotes() {
for (int i = 0; i < votes.size(); i++) {
cout << votes[i] << endl;
}
}
int countyElectionResults::getNullifierVotes(){
return nullifierVotes;
}
int countyElectionResults::getFielditeVotes(){
return fielditeVotes;
}
int countyElectionResults::getOtherVotes(){
return otherVotes;
}
int countyElectionResults::getTotalVotes(){
int totalVotes = 0;
totalVotes = nullifierVotes + fielditeVotes + otherVotes;
return totalVotes;
}
int main(){
countyElectionResults countyFile;
string filename;
cout << "Enter file name: " ;
cin >> filename;
countyFile.loadFile(filename);
cout << setw(50) << countyFile.getCountyName() << endl;
cout << setw(30) << "Nullifier: " << setw(20) << countyFile.getNullifierVotes() << endl;
cout << setw(30) << "Fieldite: " << setw(20) << countyFile.getFielditeVotes() << endl;
cout << setw(30) << "Other: " << setw(20) << countyFile.getOtherVotes() << endl;
cout << setw(30) << "Total: " << setw(20) << countyFile.getTotalVotes() << endl;
cout << endl;
countyFile.showVotes();
return 0;
}
There's the code. It compiles but just doesn't give me the expected output. For example, if I enter a textfile that contains this:
Dover
n
f
f
f
n
p
l
f
I would expect this
Enter file name: dover.txt
Dover
Nullifier: 2
Fieldite: 4
Other: 2
Total: 8
n
f
f
f
n
p
l
f
However,I am getting this.
Enter file name: dover.txt
Dover
Nullifier: 0
Fieldite: 0
Other: 4198683
Total: 4198683
n
f
f
f
n
p
l
f
Nov 21, 2012 at 2:45am UTC
One problem is that you define and initialize some local variables inside the constructor that have the same name as the member variables but are different variables so the member variables never get initialized.
Nov 21, 2012 at 2:49am UTC
Second problem is you forgot to call countVotes().
Nov 21, 2012 at 4:38am UTC
I tried reformatting my code but to no avail. Where specifically did I rehash the member variable names?
Topic archived. No new replies allowed.