This code was supposed to read and write to a file the total votes, either Y or N, for each individual district and for the overall total of each vote.
Problem is that its giving me crazy numbers and idk why
char vote;
char district;
int totalYV;
int totalNV;
int d1TotalY; //do this for all districts
int d2TotalY;
int d3TotalY;
int d1TotalN;
int d2TotalN;
int d3TotalN;
#include <fstream>
#include <iomanip>
#include <iostream>
usingnamespace std;
int main() {
// declare variable and get input
ifstream inFile;
ofstream outFile;
char vote;
char district;
int totalYV;
int totalNV;
int d1TotalY; // do this for all districts
int d2TotalY;
int d3TotalY;
int d1TotalN;
int d2TotalN;
int d3TotalN;
inFile.open("vote.txt");
outFile.open("votersTotal.txt");
inFile >> district >> vote;
while(inFile) {
inFile >> district >> vote;
// add in overall total for both
if(district == '1') {
if(vote == 'Y')
d1TotalY == 1;
totalYV = d1TotalY++; //note the indentation
} elseif(vote == 'N') { //note the indentation
d1TotalN == 1;
totalNV = d1TotalN++;
}
if(district == '2') {
if(vote == 'Y')
d2TotalY == 1;
totalYV = d2TotalY++;
} elseif(vote == 'N') {
d2TotalN == 1;
totalNV = d2TotalN++;
}
if(district == '3') {
if(vote == 'Y')
d3TotalY == 1;
totalYV = d3TotalY++;
} elseif(vote == 'N') {
d3TotalN == 1;
totalNV = d3TotalN++;
}
totalYV++;
totalNV++;
};
outFile << "District 1 Y votes: " << d1TotalY << endl;
outFile << "District 1 N votes: " << d1TotalN << endl;
outFile << "District 2 Y votes: " << d2TotalY << endl;
outFile << "District 2 N votes: " << d2TotalN << endl;
outFile << "District 3 Y votes: " << d3TotalY << endl;
outFile << "District 3 N votes: " << d3TotalN << endl;
outFile << "Total Y Votes: " << totalYV << endl;
outFile << "Total N Votes: " << totalNV << endl;
inFile.close();
outFile.close();
return 0;
}
compiling with warnings
|| foo.cpp: In function ‘int main()’:
foo.cpp|37 col 14| warning: statement has no effect [-Wunused-value]
|| d1TotalY == 1;
|| ~~~~~~~~~^~~~
foo.cpp|41 col 13| warning: statement has no effect [-Wunused-value]
|| d1TotalN == 1;
|| ~~~~~~~~~^~~~
foo.cpp|47 col 14| warning: statement has no effect [-Wunused-value]
|| d2TotalY == 1;
|| ~~~~~~~~~^~~~
foo.cpp|50 col 13| warning: statement has no effect [-Wunused-value]
|| d2TotalN == 1;
|| ~~~~~~~~~^~~~
foo.cpp|55 col 14| warning: statement has no effect [-Wunused-value]
|| d3TotalY == 1;
|| ~~~~~~~~~^~~~
foo.cpp|58 col 13| warning: statement has no effect [-Wunused-value]
|| d3TotalN == 1;
|| ~~~~~~~~~^~~~