Need Help! Multi looping :(

Mar 11, 2013 at 10:24am
I made a voting program in which you need to enter one number from 1 ~ 5.
the variables c,d,e,f,g are the number of votes for a certain item.
What I need to do is to determine what gender votes for a particular item,

For ex:
Item 1 : 5 people voted, 2 are males , 3 are females.

Item 2 : 6 people voted, 0 males , 6 females.

I already figured out on how to input the gender, but what I really
can't think of is how do I put it in the if function,

if (b==1){
c=c+1;}
else if (b==2){
d=d+1;}
else if (b==3){
e=e+1;}
else if (b==4){
f=f+1;}
else if (b==5){
g=g+1;}
Mar 11, 2013 at 1:57pm
Seems to be one of the most given homework. You may search for "vote" on top of this in "cplusplus.com" to find some hints.

Ok?
Mar 11, 2013 at 2:27pm
closed account (3qX21hU5)
The cleanest way to do this would be to use a struct to hold all information about the candidates. Here is a example


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
// You struct to hold all info for the candidates
struct candidates
{
    // Holds the total votes they get.
    int votes;
    
    // Holds the total female votes they get
    int maleVotes;
    
    // Holds the total male votes they get.
    int femaleVotes;
};

int main()
{
    // Non working program but shows you the logic behind it.
    // Skiped a lot of stuff.

    candidates a, b, c, d, e;
    
    if (input == 1)
    {
        ++a.votes;
        if (gender == "male")
            ++a.maleVotes;
        else
            ++a.femaleVotes;
    }
    // And so on.

}


If you don't want to make a struck then cross reference their input for their gender with "male" and "female" by adding another if loop and a else inside each of your if loops that will add 1 to either the maleVotes variable or femaleVotes variable.

Hope this helps a bit, let me know if you have any troubles or need more information.
Topic archived. No new replies allowed.