0

i am trying to make a program to check and list the genders of a number of student, m being fem and l being male. I'm not sure what's wrong with my code but when i print out the m and l variable they have either really huge. Have been trying to solve this for hours, your help is greatly appreciated, cheers.

P.S sorry for my bad english.

 #include<iostream>
#include<string>
using namespace std;
main()
{
char gender[20];
int jlh,i,j,m,l;

cin>>jlh;
system("cls");
for(i=0;i<jlh;i++)
{   cout<<"Data "<<i+1<<endl;
    cout<<"Enter your gender - "<<endl;
    cin>>gender[i];
}

m,l=0;
for(i=0;i<jlh;i++){
    if(gender[i]=='p'){
        m=m+1;
    }
    else if(gender[i]=='l'){
        l=l+1;
    }
    }
    cout<<endl<<l<<endl;
    cout<<m;
 }
Aldo
  • 1
  • 1
  • what is the value of `m` and `l` before you start incrementing them? – 463035818_is_not_an_ai Oct 11 '17 at 14:19
  • 2
    please read about [mcve]. A mcve could be for example this: `int main() { int m; m = m + 1; std::cout << m; }` much more you really dont need to understand what is wrong here – 463035818_is_not_an_ai Oct 11 '17 at 14:21
  • 1
    Also, don't be afraid to use some whitespace. – zenzelezz Oct 11 '17 at 14:22
  • Welcome to Stack Overflow! Please [edit] your code to reduce it to a [mcve] of your problem. Your current code includes much that is peripheral to your problem - a minimal sample normally looks similar to a good unit test: only performing one task, with input values specified for reproducibility. – Toby Speight Oct 11 '17 at 14:23
  • Hey, in the code i posted above i didn't give them value prior to that, and l has the value of 36 if there are no 'l's inputted and 0(and so on so this one is solved) if i add l=0; before i start incrementing them. But the m variable seems to give random huge numbers with or without the m=0; – Aldo Oct 11 '17 at 14:24
  • My bad, i have edited the comment to reproduce the same problem with only the necessary parts. – Aldo Oct 11 '17 at 14:32
  • 1
    Some people are neither male nor are they female – Ed Heal Oct 11 '17 at 14:57
  • why are you checking 'p' when it is suppose to be 'm'? could be another source of problems – Matthew Oct 11 '17 at 16:33

1 Answers1

2

The line

m,l=0;

does not work as you expect. Look up the comma operator, it evaluates the first operand (just m in this case), discards the result, and evaluates and returns the second operand. So only l is set to zero. I would recommend moving the declaration to this line and initializing the variables in one go, like so

int m=0, l=0;
for (int i=0; i<jlh; i++) 
  ...

I would also move the declaration of variables like i to where they are needed, as shown above; there is no need to put all declaration at the beginning of the function.

Then the output

cout<<endl<<l<<endl;
cout<<m;

places the endl before and after the first variable, but not after the second. You should have an endl after the last line of your output, otherwise your console prompt is right after your value. It would improve readability to have something like this:

std::cout << "Number of females: " << m << std::endl; 
std::cout << "Number of males:   " << l << std::endl;

You should also make sure that not more than 20 values are entered, as your array has this size. But there is not even a need for this (maybe there is in your real code, but not in the MCVE): You can just increment the variables when reading the input, no need to store it in the array. This gets rid off this arbitrary limit. If you really need the values, you should use a std::vector instead of a fixed size array.

Karsten Koop
  • 2,475
  • 1
  • 18
  • 23
  • The program worked after i changed the m,l=0; line, can't believe i spent hours on that basic mistake. I will heed your advices, thank you very much :) – Aldo Oct 11 '17 at 15:03
  • About the array size, i changed it to cin>>jlh; char gender[jlh]; Does this work too? – Aldo Oct 11 '17 at 15:06
  • @Aldo You should probably read [this post](https://stackoverflow.com/questions/15013077/arrayn-vs-array10-initializing-array-with-variable-vs-real-number) - defining an array with variable length is a g++ extension. If you want something equivalent to variable-length arrays, you should definitely look at [std::vector](http://www.cplusplus.com/reference/vector/vector/) (although Karsten suggested a perfect alternative in the answer). Also, note that it's considered polite to mark an answer as accepted if it solved your problem. – hnefatl Oct 11 '17 at 16:08