-4

this countGender function need to receive what gender and return 2 value which is female or male

    int countGender(string gender)
    {
        int numGender[2] = {0};
        
        if(gender == "F")
        numGender[1]++;
        else if(gender == "M")
        numGender[2]++;
        
        return numGender[2];
    }

display the 2 value in the main function

    int main()
    {
        string gender;
        int numGender[2];
        int n;
        
        cout<<"Enter number of respondents:";
        cin>>n;
        
        for(int i=0; i<n; i++)
        {
        cout<<"\nEnter Gender (F-Female, M-Male):";
        cin>>gender;
        numGender=countGender(gender);
        }
        cout<<"\nFemale - "<<numGender[1];
        cout<<"\nMale - "<<numGender[2];
        return 0;
    }
Dequavis
  • 1
  • 1
  • `numGender[2]++;` and `return numGender[2];` introduce _undefined behavior_. Indexing in c++ starts at zero. What do you actually want to achieve? – πάντα ῥεῖ Feb 06 '22 at 10:57
  • In the countGender function, i want to use array to store 2 value which is numGender[1] to represent female and numGender[2] to represent male. In the main function, i want to call that function and display the 2 value. Or maybe there is another alternative? – Dequavis Feb 06 '22 at 11:06
  • So you might use 2 reference parameters for output. The function can pass only one value back to the called via the return value. Something like `void countGender(string gender, int& countMale, int& countFemale);` – πάντα ῥεῖ Feb 06 '22 at 11:08
  • ohhh okay now i undestand, thank you so much for your advice:) – Dequavis Feb 06 '22 at 11:31

1 Answers1

0

You're mismatching effect of function and object you want to store results. THe countGender function looks like a mess resulting of shotgun debugging.

You can't return an array in a function. More of, to actually perform counting, you have to pass existing value on each iteration. One of logical things to do is to pass the array by reference. Also, array in C++ have zero-based indexes.

void countGender(int (&numGender)[2], string gender)
{    
    if(gender == "F") 
        numGender[0]++; 
    else if(gender == "M") 
        numGender[1]++; 
}

int main()
{
    string gender;
    int numGender[2] = {};
    int n;
    
    cout<<"Enter number of respondents:";
    cin>>n;
    
    for(int i=0; i<n; i++)
    {
        cout<<"\nEnter Gender (F-Female, M-Male):";
        cin>>gender;
        countGender(numGender,gender);
    }
    cout<<"\nFemale - "<<numGender[0];
    cout<<"\nMale - "<<numGender[1];
    return 0;
}
Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
  • thank you so much for your comment, now i understand and I really appreciate you taking the time to write – Dequavis Feb 06 '22 at 11:34
  • Hi, just want to ask. For the countGender function, what is the difference when i use int return type instead of void? (btw both works) or i can just use either one? – Dequavis Feb 14 '22 at 13:55
  • @Dequavis difference is that you would need to return something relevant (you have to do so in all control paths or else), e,g. a enum telling which gender was chosen, but `countGender` would be superfluous in that case. What you as trying to return wasn't relevant at all, you was trying to return a possibly uninitialized element of array created inside of function but you actually returned value at memory location beyond that array. – Swift - Friday Pie Feb 17 '22 at 06:33