In desperate need of help with looping and reading files

closed account (LNAM4iN6)
I
Last edited on
You need to make use of a struct or a class to store info. Then make a vector or array of these.

This will be much better - what if you 1000 votes? Are you going to have 1000 variables with 5000 lines of code? That is why this is in the assignment:

Following those six lines are an indeterminate number of lines,


HTH Good luck. Google the things you don't understand.
OP for reference:

I am given a text file where the first six lines will always be in the format <Party affiliation> <Position sought> <Full name> where <Party affiliation> is either Fieldite or Nullifier, <Position sought> is one of Auditor, Coroner, or Recorder, and <Full name> is the candidates full name as they want it listed. Votes may be cast for fringe candidates.
Following those six lines are an indeterminate number of lines, each of which represents a vote for a particular candidate for office. The votes, as they come out of the voting machine, are in the format:
<Position code> <Party voted for>
where
Position code is one of the valid codes A,C, or R (in upper or lower case) or some other letter that represents a mistake.
Party voted for is one of the major parties coded as a N or F, or some other minor party represented by some other code.

Here is an example of a text file:
Fieldite Recorder Clayton R. Raschke
Nullifier Recorder Roslyn Blumberg
Fieldite Coroner L. N. Cataldo
Nullifier Auditor Kelly Corrao
Nullifier Coroner Melisa Kinkade
Fieldite Auditor Julianne Winzer
C F
A F
C F
A F
R F
A F
R F
R N
A F
A F
C F
C G
C F
A G

The output should be a table that shows, for each position, the name and party of the winning candidate first; the name and party of the losing candidate second; and the sum of votes cast for other candidates. It should also show the count of miscast votes that were for a non-existant position code.

I am at a complete halt here and I feel like I am going down the wrong path completely. I am especially having problems with looping and sorting the first six lines of the file so that it can be sorted easily. Any help would be much appreciated! I am very new to C++. This is all the code I have managed to get but I am in need of some serious guidance.



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
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
using namespace std;

int main(){

ifstream electionfile;
string electionfilename;

cout << "Please enter a file name: ";
cin >> electionfilename;

electionfile.open(electionfilename.c_str());

if (!electionfile) {
        cout << "File not found" << endl;
}

string pa3, ps3, fn3;
string pa4, ps4, fn4;
string pa5, ps5, fn5;
string pa6, ps6, fn6;
string vote1;
string vote2;
string vote3;
string vote4;
string vote5;
string vote6;
int cf = 0;
int cn = 0;
int af = 0;
int an = 0;
int rf = 0;

electionfile >> pa1 >> ps1;
getline(electionfile,fn1);
electionfile >> pa2 >> ps2;
getline(electionfile,fn2);
electionfile >> pa3 >> ps3;
getline(electionfile,fn3);
electionfile >> pa4 >> ps4;
getline(electionfile,fn4);
electionfile >> pa5 >> ps5;
getline (electionfile,fn5);
electionfile >> pa6 >> ps6;
getline (electionfile,fn6);
electionfile >> vote1;
electionfile >> vote2;
electionfile >> vote3;
electionfile >> vote4;
electionfile >> vote5;
electionfile >> vote6;


while (electionfile >> vote1 >> vote2 >> vote3 >> vote4 >> vote5 >> vote6)
{
        if(("A F") = vote1){
           af++;
        }
}

cout << "Number of auditor fieldite votes: " << af  << endl;

return 0;
}




Topic archived. No new replies allowed.