Hi,
First, thanks in advance to those who is willing to help me. Second, I'm a total noob at C++ programming, so the code i'm posting might be completely wrong nd needs to be redone. Anyways, here's the problem i'm faced with, word for word from the C++ book: A data file called votelist contains a list of candidates voted for by each student. Any line of the file may contains 5 numbers, with each number representing a candidate. Write a program that reads the file and prints a list of the total votes received by each candidate. Also, print the five highest vote getters from highest to lowest. OK, so basically, the way i take it is that i make a random file called "votelist" and put 5 numbers on each line. Then, I read from that file and tally up how many times each number appears. Then, from those numbers, order the 5 numbers that appear the most from highest to lowest. And just to clear things up, each candidate is assigned a number, 1-15, and each student votes for 5 people, thus the five numbers per line. problem is, i don't know to tally the votes froma textfile, much less order them. Here's my code so far. If you have any idea or insight, please help. Not trying to hurry anyone, but this is due in 2 days, on friday. Thanks.
[CODE=cpp]#include <iomanip.h>
#include <iostream.h>
#include <fstream.h>
int main()
{
int vote, one, two, three, four, five;
char a, b, c, d, e, f, g, h, i, j, k, l, m, n, o;
ofstream out_file;
ifstream in_file;
in_file.open("v otelist");
cout << "\t\t\tVote s for the 15 Candidates" << endl;
cout << "C1 " << "C2 " << "C3 " << "C4 " << "C5 " << "C " << "C7 " << "C8 " << "C9 " << "C10 " << "C11 " << "C12 " << "C13 " << "C14 " << "C15"<< endl;
cout << endl;
do
{
in_file >> vote >> vote >> vote >> vote >> vote;
if(vote = 'a')
{
a++;
}
else if(vote = 'b')
{
b++;
}
else if(vote = 'c')
{
c++;
}
else if(vote = 'd')
{
d++;
}
else if(vote = 'e')
{
e++;
}
else if(vote = 'f')
{
f++;
}
else if(vote = 'g')
{
g++;
}
else if(vote = 'h')
{
h++;
}
else if(vote = 'i')
{
i++;
}
else if(vote = 'j')
{
j++;
}
else if(vote = 'k')
{
k++;
}
else if(vote = 'l')
{
l++;
}
else if(vote = 'm')
{
m++;
}
else if(vote = 'n')
{
n++;
}
else if(vote = 'o')
{
o++;
}
} while(! in_file.fail() || ! in_file.eof());
in_file.close() ;
cout << a << " " << b << " " << c << " " << d << " " << e << " " << f << " " << g << " " << h << " " << i << " " << j << " " << k << " " << l << " " << m << " " << n << " " << o << endl;
cout << "The top five candidates are:" << endl;
cout << one << endl;
cout << two << endl;
cout << three << endl;
cout << four << endl;
cout << five << endl;
return 0;
}[/CODE]
First, thanks in advance to those who is willing to help me. Second, I'm a total noob at C++ programming, so the code i'm posting might be completely wrong nd needs to be redone. Anyways, here's the problem i'm faced with, word for word from the C++ book: A data file called votelist contains a list of candidates voted for by each student. Any line of the file may contains 5 numbers, with each number representing a candidate. Write a program that reads the file and prints a list of the total votes received by each candidate. Also, print the five highest vote getters from highest to lowest. OK, so basically, the way i take it is that i make a random file called "votelist" and put 5 numbers on each line. Then, I read from that file and tally up how many times each number appears. Then, from those numbers, order the 5 numbers that appear the most from highest to lowest. And just to clear things up, each candidate is assigned a number, 1-15, and each student votes for 5 people, thus the five numbers per line. problem is, i don't know to tally the votes froma textfile, much less order them. Here's my code so far. If you have any idea or insight, please help. Not trying to hurry anyone, but this is due in 2 days, on friday. Thanks.
[CODE=cpp]#include <iomanip.h>
#include <iostream.h>
#include <fstream.h>
int main()
{
int vote, one, two, three, four, five;
char a, b, c, d, e, f, g, h, i, j, k, l, m, n, o;
ofstream out_file;
ifstream in_file;
in_file.open("v otelist");
cout << "\t\t\tVote s for the 15 Candidates" << endl;
cout << "C1 " << "C2 " << "C3 " << "C4 " << "C5 " << "C " << "C7 " << "C8 " << "C9 " << "C10 " << "C11 " << "C12 " << "C13 " << "C14 " << "C15"<< endl;
cout << endl;
do
{
in_file >> vote >> vote >> vote >> vote >> vote;
if(vote = 'a')
{
a++;
}
else if(vote = 'b')
{
b++;
}
else if(vote = 'c')
{
c++;
}
else if(vote = 'd')
{
d++;
}
else if(vote = 'e')
{
e++;
}
else if(vote = 'f')
{
f++;
}
else if(vote = 'g')
{
g++;
}
else if(vote = 'h')
{
h++;
}
else if(vote = 'i')
{
i++;
}
else if(vote = 'j')
{
j++;
}
else if(vote = 'k')
{
k++;
}
else if(vote = 'l')
{
l++;
}
else if(vote = 'm')
{
m++;
}
else if(vote = 'n')
{
n++;
}
else if(vote = 'o')
{
o++;
}
} while(! in_file.fail() || ! in_file.eof());
in_file.close() ;
cout << a << " " << b << " " << c << " " << d << " " << e << " " << f << " " << g << " " << h << " " << i << " " << j << " " << k << " " << l << " " << m << " " << n << " " << o << endl;
cout << "The top five candidates are:" << endl;
cout << one << endl;
cout << two << endl;
cout << three << endl;
cout << four << endl;
cout << five << endl;
return 0;
}[/CODE]
Comment