In the file quizin1.dat (which you should cp to your directory), each
line represents a voting card of a registered voter. The character '1'
in the i-th indicates that the vote was cast for the i-th candidate. The
first 10 columns were for ten candidates for the Governor of California.
The names of these candidates are in the input file quizin2.dat (cp it to
your directory).
Open the files quizin1.dat and quizin2.dat by using the file
pointers *finput1 and *finput2. Use the file pointer *foutput1 for the
output file quizout1.dat. Read the file quizin1.dat by using fgets().
Count the total number of voting cards, and the number of votes
cast for each of ten candidates. Use a character array: votes[80]
for votes, and an integer array: cand[10] for the candidates.
Print these results on the screen, and the numerical results in the output
file quizout1.dat. The printout on the screen should be as shown below,
while the file quizout1.dat should contain integers cand[0], cand[1],
..., cand[9] (either in one line or in separate lines).
Close the input file quizin1.dat and the output file quizout1.dat.
Then, open the created output file quizout1.dat for reading by
using the file pointer *finput3. Read the numerical data from this
file by using while(fscanf(finput3,...), and calculate the percentage
of votes that each of the ten candidates received.
Within the while(fscanf(finput3),...) loop,
use fgets(cand_name,40,finput2) in conjunction with
sscanf(cand_name, "%s %s", name, surname) to scan the surname of the
candidate and make the printout on the screen and in the output file
quizout2.dat, poited to by the pointer *foutput2, as shown below.
Use the format %-16s when printing the string surname.
Close the files quizout1.dat and quizin2.dat.
...........................................................................
Your output should look like:
Total number of voting cards: 60
Votes for candidate 1: 2
Votes for candidate 2: 7
...........................
...........................
...........................
Votes for candidate 9: 8
Votes for candidate 10: 5
Percent votes for candidate Adams is 3.33 %
Percent votes for candidate Camacho is 11.67 %
.........................................................
.........................................................
.........................................................
Percent votes for candidate Tatro is 13.33 %
Percent votes for candidate Jordan is 8.33 %
Total percent votes for all candidates is 100.00 %
*/
My code so far:
#include<stdio.h>
int main(void)
{
FILE *finput1, *finput2, *finput3, *foutput1, *foutput2;
char votes[80], name[41], surname[41];
int k, total_votes, cand[10], cand_name=0;
double percent, total_percent;
finput1 = fopen("quizin1.dat", "r");
finput2 = fopen("quizin2.dat", "r");
finput3 = fopen("quizout1.dat", "r");
foutput1 = fopen("quizout1.dat", "w");
foutput2 = fopen("quizout2.dat", "w");
printf("\n");
while(fgets(votes,80,finput1)!=NULL)
{
fscanf(finput1, "%lf", votes);
for(k=0, total_votes=0; k<81; k++)
total_votes+= votes[k];
printf("\nTotal number of voting cards: %d\n\n",total_votes);
fprintf(foutput1, " \nTotal number of voting cards: %d\n\n",total_votes);
printf("Votes for candidate %d: %lf\n", k, votes);
fprintf(foutput1, "Votes for candidate %d: %lf\n", k, votes);
}
fclose(finput1);
fclose(finput2);
fclose(foutput1);
while(fscanf(finput3, "%-16s %lf", &surname, &percent)!=EOF)
{
sscanf(cand_name, "%-16s", surname);
cand_name++;
printf("Percent votes for candidate %-16s is %lf\n", surname, percent);
fprintf(foutput2, "Percent votes for candidate %-16s is %lf\n", surname,
percent);
total_percent+= percent;
printf("\nTotal percent votes for all candidates is %.2lf\n\n",
total_percent);
fprintf(foutput2, "\nTotal percent for all candidates is %.2lf\n\n",
total_percent
);
}
fclose(finput3);
fclose(foutput2);
}
What happens when I try to run it:
warning: passing argument 1 of 'sscanf' makes pointer from integer without
a cast
referring to the line: sscanf(cand_name, "%-16s", surname);
What I still need to figure out how to do:
1) How to get the % of votes for each candidate
2) How to have #s (1,2,3,4, etc.) instead of surnames for "Votes for
candidate 1: .. etc"
3) How to total up the number of percents for all candidates (unless what
I did was right?)
4) Does my program even make sense?

New Topic/Question
Reply




MultiQuote





|