fork download
  1. #include <fstream>
  2. #include <string>
  3.  
  4. void calculatePercentage(int voteResult[], int percentage[]);
  5.  
  6. const int NUMBER_OF_CANDIDATE = 10;
  7.  
  8. int main()
  9. {
  10. std::ifstream input("votes.txt");
  11.  
  12. std::string candidates[NUMBER_OF_CANDIDATE];
  13. int voteResult[NUMBER_OF_CANDIDATE];
  14. int percentage[NUMBER_OF_CANDIDATE];
  15.  
  16. for (int i = 0; i < NUMBER_OF_CANDIDATE; i++) {
  17. input >> candidates[i] >> voteResult[i];
  18. }
  19. calculatePercentage(voteResult, percentage); // error happened here.
  20.  
  21. return 0;
  22. }
  23.  
  24. void calculatePercentage(int voteResult[], int percentage[])
  25. {
  26. int totalVotes = 0;
  27. for (int i = 0; i < NUMBER_OF_CANDIDATE; i++)
  28. {
  29. totalVotes += voteResult[i];
  30. }
  31. for (int j = 0; j < NUMBER_OF_CANDIDATE; j++)
  32. {
  33. double wk_percentage = static_cast<double>(voteResult[j]) / totalVotes;
  34. percentage[j] = static_cast<int>(wk_percentage*100);
  35. }
  36. }
  37.  
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
Standard output is empty