Write An Individual Line From An Input File

I have been working on this assignment for over a week and I have gotten to a point where I am completely stuck. I am trying to read line-by-line when asking the user to enter the amount of votes. I have figured out how to write only the first line of the input file, now I'm trying to do that with the rest of the lines. Any help is greatly appreciated, I've searched the internet and am completely stuck.

My input file that is used, has the following information:
Spencer Breland
Josh Newcomb
Justin Main
Kyle English
Jamie Thompson


The code for the program is below:


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
/* Name:  Spencer
   Class:  CSC 102
   Teacher:  Tom Rishel
   Date: 01/24/2013
   Assignment:  Election
   Description:  Write a program that reads the names of all candidates in a given election from a file. The input file
   should contain the name of each candidate on a separate line. Prompt the user to enter the number
   of votes received by each candidate. */

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

/* declares variables */
double oneVote;
double twoVote;
double threeVote;
double fourVote;
double fiveVote;
double totalVotes;
double onePercent;
double twoPercent;
double threePercent;
double fourPercent;
double fivePercent;
ifstream infile;
ofstream outfile;

int main()
	{
		/* greeting for program */
		cout << "Welcome to the Presidential Election!" << endl;
		cout <<"===================================================="<<endl;
		cout << "The list of candidates are below: " << endl;
		cout << endl;

		/* Retrieves text from input file */
		{
  string line;
  ifstream myfile ("CandidateList.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 
}
		
		cout <<"===================================================="<<endl;

  string str;
  ifstream ifs("CandidateList.txt");
  getline (ifs,str);

		/* asks the user to input the number of votes for each candidate */
		cout << endl;
		cout << "How many votes did " << str <<" receive? " << endl;
		cin >> oneVote;
		cout << endl;
		cout << "How many votes did Josh Newcomb receive? " << endl;
		cin >> twoVote;
		cout << endl;
		cout << "How many votes did Justin Main receive? " << endl;
		cin >> threeVote;
		cout << endl;
		cout << "How many votes did Kyle English receive? " << endl;
		cin >> fourVote;
		cout << endl;
		cout << "How many votes did Jamie Thompson receive? " << endl;
		cin >> fiveVote;
		cout << endl;
		cout << endl;


		/* formulas below */
		totalVotes = oneVote + twoVote + threeVote + fourVote + fiveVote;
		onePercent = oneVote/totalVotes * 100;
		twoPercent = twoVote/totalVotes * 100;
		threePercent = threeVote/totalVotes * 100;
		fourPercent = fourVote/totalVotes * 100;
		fivePercent = fiveVote/totalVotes * 100;

		/* output on screen below */
		cout << "The results are below:" << endl;
		cout <<"----------------------------------------------------"<<endl;
		cout << "Candidate" << "          " << "Votes Received" << "   " << "Percentage Votes." << endl;
		cout <<"===================================================="<<endl;
		cout << setprecision(2) << fixed << showpoint << endl;
		cout << "1. " << str  << "      " << oneVote << "            " << onePercent << "%" << endl;
		cout << "2. Josh Newcomb   " << "      " << twoVote << "            " << twoPercent << "%" << endl;
		cout << "3. Justin Main    " << "      " << threeVote << "            " << threePercent << "%" << endl;
		cout << "4. Kyle English   " << "      " << fourVote << "            " << fourPercent << "%" << endl;
		cout << "5. Jamie Thompson " << "      " << fiveVote << "            " << fivePercent << "%" << endl;
		cout << endl;
		
		/* determines winner */
		if (oneVote >= twoVote && oneVote >= threeVote && oneVote >= fourVote && oneVote >= fiveVote)
                cout << "And the winner is "<< str << "!" << endl;
        else if (twoVote >= oneVote && twoVote >= threeVote && twoVote >= fourVote && twoVote >= fiveVote)
                cout << "And the winner is Josh Newcomb!" << endl;
        else if (threeVote >= oneVote && threeVote >= twoVote && threeVote >= fourVote && threeVote >= fiveVote)
                cout << "And the winner is Justin Main!" << endl;
        else if (fourVote >= oneVote && fourVote >= twoVote && fourVote >= threeVote && fourVote >= fiveVote)
                cout << "And the winner is Kyle English!" << endl;
        else if (fiveVote >= oneVote && fiveVote >= twoVote && fiveVote >= fiveVote && fiveVote >= fourVote)
                cout << "And the winner is Jamie Thompson!" << endl;

		/* outputs to an output file */
		ofstream myfile;
		myfile.open ("ElectionResults.txt");
		myfile << "The results are below:" << endl;
		myfile <<"----------------------------------------------------"<<endl;
		myfile << "Candidate" << "          " << "Votes Received" << "   " << "Percentage Votes." << endl;
		myfile <<"===================================================="<<endl;
		myfile << setprecision(2) << fixed << showpoint << endl;
		myfile << "1. " << str << "      " << oneVote << "            " << onePercent << "%" << endl;
		myfile << "2. Josh Newcomb   " << "      " << twoVote << "            " << twoPercent << "%" << endl;
		myfile << "3. Justin Main    " << "      " << threeVote << "            " << threePercent << "%" << endl;
		myfile << "4. Kyle English   " << "      " << fourVote << "            " << fourPercent << "%" << endl;
		myfile << "5. Jamie Thompson " << "      " << fiveVote << "            " << fivePercent << "%" << endl;
		myfile << endl;
		
		if (oneVote >= twoVote && oneVote >= threeVote && oneVote >= fourVote && oneVote >= fiveVote)
                myfile << "And the winner is " << str << "!" << endl;
        else if (twoVote >= oneVote && twoVote >= threeVote && twoVote >= fourVote && twoVote >= fiveVote)
                myfile << "And the winner is Josh Newcomb!" << endl;
        else if (threeVote >= oneVote && threeVote >= twoVote && threeVote >= fourVote && threeVote >= fiveVote)
                myfile << "And the winner is Justin Main!" << endl;
        else if (fourVote >= oneVote && fourVote >= twoVote && fourVote >= threeVote && fourVote >= fiveVote)
                myfile << "And the winner is Kyle English!" << endl;
        else if (fiveVote >= oneVote && fiveVote >= twoVote && fiveVote >= fiveVote && fiveVote >= fourVote)
                myfile << "And the winner is Jamie Thompson!" << endl;

		myfile.close();



    system("pause");
    
    
    return 0;
}
The candidates names are contained within the text file. I assume you need to use that data to populate a list of names.

Instead, the candidates names have actually been typed into the program code, multiple times. Even if you didn't have a data file, that would still be an undesirable approach. It is heavy on maintenance, if a candidate withdraws or is replaced by a different one, the program would need to be changed in multiple different places.

As a starting point, define string variables for the names of the list of candidates. That way, you would need to type the candidates names just once, not five times.

When you've done that, modify the code which reads the file, so that it stores the names in your variables.

The one question I have is this: how do we know how many candidates there are? The program assumes five. Perhaps there is some rule which says there must always be exactly five candidates, but in real elections, the number of candidates may vary, therefore the program should be driven by the data file, the number of lines in the file will control how many times the user is prompted to enter the votes.
Last edited on
Topic archived. No new replies allowed.