I am battling to write this function

This is the algorithm given in the textbook:
1. Initialize sumVotes, largestVotes, and winLoc to 0;
2. Retrieve the candidate's name and relevant data.
a. Retrieve the candidate's data into temp.
b. Print the candidate's name and relevant data.
c. Retrieve the total votes received by the candidate and update sumVotes.

1
2
3
4
5
6
    
if(largestVotes < temp.getTotalVotes())
{
      largestVotes = temp.getTotalVotes();
      winLoc = i;
}


2. Output the final lines of the output.


I get this error when compiling this program:
C:\Martin\MalikDownloads\Chapter 10 Source Code\Ch10_ElectionProgram\electionResult.cpp: In function 'void printResults(orderedArrayListType<candidateType>&)':
C:\Martin\MalikDownloads\Chapter 10 Source Code\Ch10_ElectionProgram\electionResult.cpp:149:9: error: 'totalVotes' was not declared in this scope
C:\Martin\MalikDownloads\Chapter 10 Source Code\Ch10_ElectionProgram\electionResult.cpp:153:19: warning: name lookup of 'i' changed [enabled by default]
C:\Martin\MalikDownloads\Chapter 10 Source Code\Ch10_ElectionProgram\electionResult.cpp:139:13: warning: matches this 'i' under ISO standard rules [enabled by default]
C:\Martin\MalikDownloads\Chapter 10 Source Code\Ch10_ElectionProgram\electionResult.cpp:148:15: warning: matches this 'i' under old rules [enabled by default]
Process terminated with status 1 (0 minute(s), 1 second(s))
1 error(s), 3 warning(s) (0 minute(s), 1 second(s))

here is my function description:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  void printResults(orderedArrayListType<candidateType>& cList)
{
        int sumVotes , largestVotes, winLoc = 0;
	//cout<<"See Programming Exercise 11"<<endl;
    string firstN, lastN;

    for(int i = 0; i <= noOfCandidates; i++)
    {
        candidateType temp;

        cout << left
          << setw(8) << firstN << " "
          << setw(8) << lastN << " ";

     cout << right;
     for (int i = 0; i < noOfRegions; i++)
        totalVotes = sumVotes;
     if (largestVotes < temp.getTotalVotes())
     {
         largestVotes = temp.getTotalVotes();
         winLoc = i;
     }

}
}


Here is the candidateType class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef H_candidateType
#define H_candidateType

#include <string> 
#include "personType.h"  
using namespace std;  
const int noOfRegions = 4;  
class candidateType: public personType 
{ 
public:     
	const candidateType& operator=(const candidateType&);
		//Overload the assignment operator for the objects of the
 		//type candidateType.
   
	const candidateType& operator=(const personType&);
	//Overload the assignment operator for the objects so 
 	//that the value of an object of the type personType can 
 	//be assigned to an object of the type candidateType. 
   
	 void setVotes(int region, int votes);
 	//Function to set the votes of a candidate for a
	//particular region.
	//Postcondition: The votes specified by the parameter votes 
	//               are assigned to the region specified by the 
	//               parameter region.

    void updateVotesByRegion(int region, int votes);
 	//Function to update the votes of a candidate for a
	//particular region.
	//Postcondition: The votes specified by the parameter votes
	//               are added to the region specified by the 
	//               parameter region.

   void calculateTotalVotes();
	//Function to calculate the total votes received by a
	//candidate. 
	//Postcondition: The votes received in each region are added.

    int getTotalVotes();
 	//Function to return the total votes received by a
 	//candidate.
	//Postcondition: The total votes received by the candidate
	//               are returned.

    void printData() const;
	//Function to output the candidate’s name, the votes 
 	//received in each region, and the total votes received.

    candidateType();
	//default constructor 
	//Postcondition: Initialize the votes received in each 
	//               region, and the total votes received, to zero.

		//Overload the relational operators.
    bool operator==(const candidateType& right) const;
    bool operator!=(const candidateType& right) const;
    bool operator<=(const candidateType& right) const;
    bool operator<(const candidateType& right) const;
    bool operator>=(const candidateType& right) const;
    bool operator>(const candidateType& right) const;

private:
    int votesByRegion[noOfRegions];
    int totalVotes;
};

#endif


If there is some more information needed I can provide.
The project is very large with about 10 files
> C:\Martin\MalikDownloads\Chapter 10 Source Code\Ch10_ElectionProgram
> \electionResult.cpp:149:9: error: 'totalVotes' was not declared in this scope
totalVotes is a private member of the class, and your function is not a class member function.


1
2
    for (int i = 0; i < noOfRegions; i++)
        totalVotes = sumVotes;

Try to work this so it makes use of this function.
> void setVotes(int region, int votes);

Or perhaps
> void updateVotesByRegion(int region, int votes);
I have changed line 17 in the function printResults to:
1
2
3

    sumVotes = temp.getTotalVotes()  ;

and now I get this from the compiler log:
Chapter 10 Source Code\Ch10_ElectionProgram\electionResult.cpp: In function 'void printResults(orderedArrayListType<candidateType>&)':
C:\Martin\MalikDownloads\Chapter 10 Source Code\Ch10_ElectionProgram\electionResult.cpp:150:19: warning: name lookup of 'i' changed [enabled by default]
C:\Martin\MalikDownloads\Chapter 10 Source Code\Ch10_ElectionProgram\electionResult.cpp:136:13: warning: matches this 'i' under ISO standard rules [enabled by default]
C:\Martin\MalikDownloads\Chapter 10 Source Code\Ch10_ElectionProgram\electionResult.cpp:145:15: warning: matches this 'i' under old rules [enabled by default]
C:\Martin\MalikDownloads\Chapter 10 Source Code\Ch10_ElectionProgram\electionResult.cpp:132:13: warning: variable 'sumVotes' set but not used [-Wunused-but-set-variable]
C:\Martin\MalikDownloads\Chapter 10 Source Code\Ch10_ElectionProgram\electionResult.cpp:132:38: warning: variable 'winLoc' set but not used [-Wunused-but-set-variable]
mingw32-g++.exe -o "bin\Debug\Election Results.exe" "obj\Debug\MalikDownloads\Chapter 10 Source Code\Ch10_ElectionProgram\candidateTypeImp.o" "obj\Debug\MalikDownloads\Chapter 10 Source Code\Ch10_ElectionProgram\electionResult.o" "obj\Debug\MalikDownloads\Chapter 10 Source Code\Ch10_ElectionProgram\personTypeImp.o"
Output file is bin\Debug\Election Results.exe with size 1.02 MB
Process terminated with status 0 (0 minute(s), 4 second(s))
0 error(s), 5 warning(s) (0 minute(s), 4 second(s))


-------------- Run: Debug in Election Results (compiler: GNU GCC Compiler)---------------

Checking for existence: C:\Martin\Election Results\Election Results\bin\Debug\Election Results.exe
Executing: "C:\Program Files\CodeBlocks/cb_console_runner.exe" "C:\Martin\Election Results\Election Results\bin\Debug\Election Results.exe" (in C:\Martin\Election Results\Election Results\.)
Process terminated with status 255 (0 minute(s), 10 second(s))


-------------- Run: Debug in Election Results (compiler: GNU GCC Compiler)---------------

Checking for existence: C:\Martin\Election Results\Election Results\bin\Debug\Election Results.exe
Executing: "C:\Program Files\CodeBlocks/cb_console_runner.exe" "C:\Martin\Election Results\Election Results\bin\Debug\Election Results.exe" (in C:\Martin\Election Results\Election Results\.)
Process terminated with status -1073741510 (0 minute(s), 12 second(s))

The program runs and it then bombs out.
Please help!!!
1. Initialize sumVotes, largestVotes, and winLoc to 0;

Your code int sumVotes, largestVotes, winLoc = 0; only initializes winLoc to 0.

2. Retrieve the candidate's name and relevant data.
You don't do this. You pass in a collection of candidates (cType) but you never do anything by it.

a. Retrieve the candidate's data into temp.
You don't do this. You have local variables for first and last name, but you don't even set their values.
b. Print the candidate's name and relevant data.
You print some variables called firstN and lastN, but they aren't related to any candidate.
c. Retrieve the total votes received by the candidate and update sumVotes.
You need to be sure that calculateTotalVotes() has been called before you call getTotalVotes().
2. Output the final lines of the output.
You don't do this.

I think one reason that your code isn't working is that the instructions are pretty bad. These might be clearer:

1. Initialize sumVotes, largestVotes, and winLoc to 0.
2. For each candidate:
a. Print the candidate's name and relevant data.
b. Retrieve the total votes received by the candidate and update sumVotes.
c. If the total number of votes received by the candidate is more than largestVotes then update largestVotes and winLoc.
d. Print the total number of votes received by the candidate?
3. Use winLoc and largest Votes to print the name (and other info?) of the winner.
Topic archived. No new replies allowed.