Oct 4, 2017 at 8:12pm Oct 4, 2017 at 8:12pm UTC
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
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace 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++;
}
else if(vote=='N')
{
d1TotalN==1;
totalNV = d1TotalN++;
}
if(district=='2')
{if(vote=='Y')
d2TotalY==1;
totalYV = d2TotalY++;
}
else if(vote=='N')
{
d2TotalN==1;
totalNV = d2TotalN++;
}
if(district=='3')
{if(vote=='Y')
d3TotalY==1;
totalYV = d3TotalY++;
}
else if(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;
}
Oct 4, 2017 at 8:32pm Oct 4, 2017 at 8:32pm UTC
What does the input file contain ?
Oct 5, 2017 at 3:26am Oct 5, 2017 at 3:26am UTC
your code properly indented
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
#include <fstream>
#include <iomanip>
#include <iostream>
using namespace 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
} else if (vote == 'N' ) { //note the indentation
d1TotalN == 1;
totalNV = d1TotalN++;
}
if (district == '2' ) {
if (vote == 'Y' )
d2TotalY == 1;
totalYV = d2TotalY++;
} else if (vote == 'N' ) {
d2TotalN == 1;
totalNV = d2TotalN++;
}
if (district == '3' ) {
if (vote == 'Y' )
d3TotalY == 1;
totalYV = d3TotalY++;
} else if (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;
|| ~~~~~~~~~^~~~
Also, initialise your variables
Last edited on Oct 5, 2017 at 3:27am Oct 5, 2017 at 3:27am UTC