Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. // Counts the total number of votes
  5. int votes_cast (FILE* voteLines)
  6. {
  7. int count = 0;
  8. char readVote[100];
  9.  
  10. while (fgets(readVote, 100, voteLines) != NULL)
  11. count++;
  12. return count;
  13. }
  14.  
  15. // Counts how many votes were cast for the specified party by each constituency
  16. int votes_cast_in_constituency_party (FILE* votePerCon, char* con, char* party)
  17. {
  18. int count = 0;
  19. char lineOfText[100];
  20.  
  21. while(fgets(lineOfText, 100, votePerCon) != NULL){
  22. if(strstr(lineOfText, con) != NULL && strstr(lineOfText, party) != NULL){
  23. count++;
  24. }
  25. }
  26. return count;
  27. }
  28.  
  29.  
  30. int main()
  31. {
  32. int voteLines = 0, votePerCon = 0;
  33. char con[20] = "The Shadows", party[20] = "Wizards";
  34.  
  35. // Opens votes.txt file for reading
  36. FILE *votesTxt;
  37. votesTxt = fopen("votes.txt", "r");
  38.  
  39. // Calls function that counts the total number of votes
  40. voteLines = votes_cast(votesTxt),
  41. printf("%d\n", voteLines);
  42.  
  43. // Calls function that counts votes per constituency
  44. votePerCon = votes_cast_in_constituency_party(votesTxt, con, party),
  45. printf("%d\n", votePerCon);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement