Total number of voting cards: 55
Votes for candidate 1: 1
Votes for candidate 2: 10
Votes for candidate 3: 8
Votes for candidate 4: 13
Votes for candidate 5: 8
Votes for candidate 6: 12
Votes for candidate 7: 3
Percent votes for Mr. Adams : 1.82 %
Percent votes for Mr. Camacho : 18.18 %
Percent votes for Mr. Copeland : 14.55 %
Percent votes for Mr. Simongton : 23.64 %
Percent votes for Mr. Schwarz : 14.55 %
Percent votes for Mr. Smith : 21.82 %
Percent votes for Mr. Steinberg : 5.45 %
I can make it show up in two different ways:
Total number of voting cards : 55
Votes for candidate 1: 1
Votes for candidate 2: 10
Votes for candidate 3: 8
Votes for candidate 4: 13
Votes for candidate 5: 8
Votes for candidate 6: 12
Votes for candidate 7: 3
Percent votes for Mr. Steinberg : 1.82 percent
Percent votes for Mr. Steinberg : 18.18 percent
Percent votes for Mr. Steinberg : 14.55 percent
Percent votes for Mr. Steinberg : 23.64 percent
Percent votes for Mr. Steinberg : 14.55 percent
Percent votes for Mr. Steinberg : 21.82 percent
Percent votes for Mr. Steinberg : 5.45 percent
or
Total number of voting cards : 55
Votes for candidate 1: 1
Votes for candidate 2: 10
Votes for candidate 3: 8
Votes for candidate 4: 13
Votes for candidate 5: 8
Votes for candidate 6: 12
Votes for candidate 7: 3
Percent votes for Mr. Adams : 1.82 percent
Percent votes for Mr. Camacho : 1.82 percent
Percent votes for Mr. Copeland : 1.82 percent
Percent votes for Mr. Simongton : 1.82 percent
Percent votes for Mr. Schwarz : 1.82 percent
Percent votes for Mr. Smith : 1.82 percent
Percent votes for Mr. Steinberg : 1.82 percent
depending on how i close the brackets at the end of my code.
my code is as follows...
Expand|Select|Wrap|Line Numbers
- #include <stdio.h>
- main()
- {
- FILE *input1;
- FILE *input2;
- FILE *input3;
- FILE *output1;
- int votes, cand[7], row, ones, q, cands;
- char vote[81];
- input1 = fopen("q_in1.dat", "r");
- output1 = fopen("q_out1.dat", "w");
- if((input1 == NULL) || (output1 == NULL)){
- printf("Error opening the file\n");
- exit(911);}
- row=0;
- ones=0;
- for(q=0; q<7; q++) cand[q]=0;
- while(fgets(vote, 81, input1) !=NULL){
- row++;
- ones=0;
- for(q=0; q<7; q++){
- if(vote[q]=='1'){
- ones++;
- cands=q;}}
- if(ones==1)cand[cands]++;
- }
- printf("\nTotal number of voting cards : %d\n\n", row);
- for(q=0; q<7; q++){
- printf("Votes for candidate %d: %4d\n", q+1, cand[q]);
- fprintf(output1, "%d\n", cand[q]);}
- printf("\n");
- fclose(input1);
- fclose(output1);
- char cand_name[41], name[52], surname[62];
- int votes_recieved;
- double p;
- input2 = fopen("q_in2.dat", "r");
- input3 = fopen("q_out1.dat", "r");
- if((input2 == NULL) || (input3 == NULL)){
- printf("Error opening the file\n");
- exit(911);
- }
- while(fscanf(input3, "%d", &votes_recieved) != EOF){
- p = (votes_recieved/55.)*100.;
- while(fgets(cand_name, 41, input2) != NULL){
- sscanf(cand_name, "%s %s", name, surname);
- printf("Percent votes for Mr. %-16s: %4.2f percent\n", surname, p);}}
- fclose(input2);
- fclose(input3);
- }
Expand|Select|Wrap|Line Numbers
- while(fscanf(input3, "%d", &votes_recieved) != EOF){
- p = (votes_recieved/55.)*100.;
- while(fgets(cand_name, 41, input2) != NULL){
- sscanf(cand_name, "%s %s", name, surname);
- printf("Percent votes for Mr. %-16s: %4.2f percent\n", surname, p);}}
sscanf(cand_name, "%s %s", name, surname);
then the output becomes what the first output gives, with the names incorrect, but percentages correct
if i leave it the way it is
then the output becomes what the second output gave, with the names correct, but percentages incorrect.