array vector task

ok so this is the part of the code which is not working for me
Tally is a vector containing integers and I only want to count the number of time the integers in tally repeats themselves..but this part of the code is not working can someone help me? thanks in advance!!!

cout<<endl<<endl<<"We will now find out how many times values are repeated in your list."<<endl<<endl;

for (i=0;i<Numberofpossiblenum;i++)
{
for (j=0;j<Numberofpossiblenum-1;j++)
{
if(Tally[i] = Tally[j])
{
counter+=1;

Tally.erase(Tally.begin()+j-1);
}
cout<<endl<<"Value"<<Tally[i]<<"repeated"<<counter<<"times!"<<endl<<endl;
}

}

return 0;
}
Firstly, please enclose your code in code tags. It will make it much easier for us to read.

Secondly, the line:

if(Tally[i] = Tally[j])

uses a single equals sign. This causes the value of Tally[j] to be assigned to Tally[i], after which they will both be equal. This means that the if statement will always be true.

Presumably, you meant to use a double equals sign to check whether they are equal? I'm surprised your compiler didn't warn you about this - most modern ones do.

Thirdly, you're erasing members of your vector while in the middle of iterating over it. This will mean that its size will shrink while you're looping. You don't show us how you're calculating Numberofpossiblenum, but it's possible that it will be bigger than the number of elements in your shortened vector, which will cause errors.

Last edited on
MikeyBoyyyyy...you're the man...thank you soooo much..!!! what silly errors by my side..thanks again
You're welcome :)
Please have a look at the following statement:

Tally.erase(Tally.begin()+j-1);

if j=0, in that case what will it erase..??

I think, it will try to delete the previous element of index '0'. This might causing to cash.
Last edited on
Topic archived. No new replies allowed.