creating outfile name, comparing array values

Hello again! In a battle against my own insanity, I decided to do some extra stuff for my final program in my C++ class. Probably got a bit carried away. Here's the code:

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
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
	string candidates[5];
	int votes[5];
	int g, f, u;
	double sum = 0;

	cout << "Enter the first candidate. \n";

	for ( g = 0 ; g < 5; g++ )
	{
		cin >> candidates[g];

	switch (g)
	{
	case 0:		
		cout << "Enter the second candidate: \n";
	break;
	case 1:
		cout << "Enter the third candidate: \n";
	break;
	case 2:
		cout << "Enter the fourth candidate: \n";
	break;
	case 3:
		cout << "Enter the fifth candidate: \n";
	break;
	}
	}

	cout << "\nEnter votes for " << candidates[0] << ":\n";

	for ( f = 0 ; f < 5; f++ )
	{
	
	cin >> votes[f];
	
	switch (f)
	{
	case 0:
		cout << "\nEnter votes for " << candidates[1] << ":\n";
		break;
	case 1:
		cout << "\nEnter votes for " << candidates[2] << ":\n";
		break;
	case 2:
		cout << "\nEnter votes for " << candidates[3] << ":\n";
		break;
	case 3:
		cout << "\nEnter votes for " << candidates[4] << ":\n";
		break;
	}
	}

	system("cls");

	for (u = 0; u < f; u++)
	{
		sum = sum + votes[u];
	}
	bool y, n;
	string boolValue;

	cout << "Would you like a copy of the results in a .txt file? (y/n) ";
	cin >> boolValue;
	
	if (boolValue == "y")
	{ y = true;
	
	char fileName[20];
	ifstream infile;
	ofstream outfile;
	
	outfile.open(fileName);

	cout << "Please enter a name for your file (including path and extension): ";
	cin >> fileName;
	
	outfile << "\nCandidate      Votes Received      % of Total Votes\n";
	outfile << left << setw(20) << candidates[0] << setw(20) << left << votes[0] << fixed << setprecision(2) << (votes[0]/sum)*100 << "\n";
	outfile << left << setw(20) << candidates[1] << setw(20) << left << votes[1] << fixed << setprecision(2) << (votes[1]/sum)*100 << "\n";
	outfile << left << setw(20) << candidates[2] << setw(20) << left << votes[2] << fixed << setprecision(2) << (votes[2]/sum)*100 << "\n";
	outfile << left << setw(20) << candidates[3] << setw(20) << left << votes[3] << fixed << setprecision(2) << (votes[3]/sum)*100 << "\n";
	outfile << left << setw(20) << candidates[4] << setw(20) << left << votes[4] << fixed << setprecision(2) << (votes[4]/sum)*100 << "\n"; 
	
	infile.close();
	outfile.close();
	}
	else if (boolValue == "n")
	{ n = false;
	cout << "\nCandidate      Votes Received      % of Total Votes\n";
	cout << left << setw(20) << candidates[0] << setw(20) << left << votes[0] << fixed << setprecision(2) << (votes[0]/sum)*100 << "\n";
	cout << left << setw(20) << candidates[1] << setw(20) << left << votes[1] << fixed << setprecision(2) << (votes[1]/sum)*100 << "\n";
	cout << left << setw(20) << candidates[2] << setw(20) << left << votes[2] << fixed << setprecision(2) << (votes[2]/sum)*100 << "\n";
	cout << left << setw(20) << candidates[3] << setw(20) << left << votes[3] << fixed << setprecision(2) << (votes[3]/sum)*100 << "\n";
	cout << left << setw(20) << candidates[4] << setw(20) << left << votes[4] << fixed << setprecision(2) << (votes[4]/sum)*100 << "\n";	
	}
	
	return 0;
}


here's the problem(s):
first, the outfile isn't creating a "save" file. I have no idea where its going. Is it possible to use outfile only, without infile? the only reason infile is in there right now is for troubleshooting reasons.
second, is there some way to code a function that will return the largest value of the array? Reason I ask, is because the program asks for the five candidates and votes, and after the display of the five candidates and so forth, there is supposed to be "The winner of the election is *name*", and I'm not entirely sure how that could return the name based off the entered values.

Any help would be greatly appreciated, as usual.
Last edited on
Did you check where the program executable is? That is where my files have been saved when I create them. As for checking for the greatest value, I do not think there is such a function. You will probably have to check manually.
Ah, there it is. Strangely enough, the file name is ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ, regardless of what I name it.
Ok, so I changed the code to:

1
2
3
4
5
6
7
8
9
char fileName[20];
ifstream infile;
ofstream outfile;
	
cout << "Please enter a name for your file (including path and extension): ";
cin >> fileName;

outfile.open(fileName);


and it works! Aww yea.
Still having trouble with returning the largest value on the array though.
To do it yourself, the algorithm goes something like this:

1
2
3
4
5
// pseudo-code
highest_val_so_far = value-of-first-element;
for_each( element 2..N in list )
    if( element is greater than highest_val_so_far )
        highest_val_so_far = element;


And the way I'd do it is (if you're allowed to use STL algorithms):

1
2
3
#include <algorithm>

cout << "Highest value = " << *std::max_element( &votes[0], &votes[5] ) << endl;

Topic archived. No new replies allowed.