You are Here: Home

# PCM track length code c++ 'bool' issue

Announcements Posted on
Why bother with a post grad? Are they even worth it? Have your say! 26-10-2016
1. So heres the code:

Code:
//from 0-max do a count
for(sample_count=0 ; sample_count<max_number_samples ; sample_count++)
{
//checks if the sound_data is not equal to zero (when the track has purely zeros at the end, the track ends before the first zero in that line)
if(!(sound_data[sample_count]=0))
{
//if it isn't equal to zero, it adds 1 to the 'length'
counter_1 = counter_1 + 1;
}
//if it is equal to zero, it might not be the end of the track
else if(sound_data[sample_count]=0)
{
//from the last sample_count value-max find if there are any more values which aren't zero. Stopping the counter if/when there is a sound_data value or when max_number_samples is reached
for(counter_2=sample_count + 1; counter_2<(max_number_samples || !(sound_data[counter_2]=0)); counter_2++)
{
//makes a count in case the track hasn't stopped yet
counter_3 = counter_3 + 1;
//if there is a non zero value, the track hasn't ended so adds the length of counter_3 to counter_1 for the extra length
if(!(sound_data[counter_2]=0))
{
counter_1 = counter_1 + counter_3;
}
}
}
}
When i run my program, everything is fine up to before calculating the track length. If i check whats in an array before this code, it is exactly whats expected, however if i check it afterwards, all the values are zeros. Why does the code i've used do that?! And what can i do to rectify the 'bool' error?
2. sound_data[sample_count]=0 sets the value of sound_data[sample_count] to 0.
For an equality test, you may want to consider using the == operator.

Also, you don't really need to make two threads for the same issue.
3. (Original post by JGR)
sound_data[sample_count]=0 sets the value of sound_data[sample_count] to 0.
So whenever I've written sound_data[sample_count]=0 , even if it's inside the if criteria, it will set it to zero? I'm really new to programming...

4. ISSUE FIXED!

New Code:
Code:
//from 0-max do a count
for(sample_count=0 ; sample_count<max_number_samples ; sample_count++)
{

sound_data_value_1 = sound_data[sample_count];
//checks if the sound_data is not equal to zero (when the track has purely zeros at the end, the track ends before the first zero in that line)
if(!(sound_data_value_1=0))
{
//if it isn't equal to zero, it adds 1 to the 'length'
counter_1 = counter_1 + 1;
}
//if it is equal to zero, it might not be the end of the track
else if(sound_data_value_1=0)
{
sound_data_value_2 = sound_data[counter_2];
//from the last sample_count value-max find if there are any more values which aren't zero. Stopping the counter if/when there is a sound_data value or when max_number_samples is reached
for(counter_2=sample_count + 1; counter_2<(max_number_samples || !(sound_data_value_2=0)); counter_2++)
{
//makes a count in case the track hasn't stopped yet
counter_3 = counter_3 + 1;
//if there is a non zero value, the track hasn't ended so adds the length of counter_3 to counter_1 for the extra length
if(!(sound_data_value_2=0))
{
counter_1 = counter_1 + counter_3;
}
}
}
}
5. (Original post by AaronT)
So whenever I've written sound_data[sample_count]=0 , even if it's inside the if criteria, it will set it to zero? I'm really new to programming...
This is a nasty side effect in the C / C++ languages

x = 0 means set x to 0
if (x = 0) means set x to 0 and test whether the result is true or not
if (x==0) means test whether x has value 0

Be very careful with your assignment / equality tests. If you have a half-decent C++ book or some online notes, they should explain about = versus ==.
6. (Original post by davros)
This is a nasty side effect in the C / C++ languages

x = 0 means set x to 0
if (x = 0) means set x to 0 and test whether the result is true or not
if (x==0) means test whether x has value 0

Be very careful with your assignment / equality tests. If you have a half-decent C++ book or some online notes, they should explain about = versus ==.
Thanks! Thats exactly what i needed.
7. (Original post by AaronT)
So whenever I've written sound_data[sample_count]=0 , even if it's inside the if criteria, it will set it to zero?
If you find yourself getting caught out by this, you could always flip the equality test:
Code:
if (0 == x)
Which is equivalent to if (x == 0), but prompts your compiler to raise an error should you attempt an assignment by mistake, as '0' is an integer literal (i.e. constant value) that cannot be modified.
8. Or, for that matter, increase the warning level you're using when compiling.

## Register

Thanks for posting! You just need to create an account in order to submit the post
1. this can't be left blank
2. this can't be left blank
3. this can't be left blank

6 characters or longer with both numbers and letters is safer

4. this can't be left empty
1. Oops, you need to agree to our Ts&Cs to register

Updated: April 25, 2012
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

This forum is supported by:
Today on TSR

### Who is getting a uni offer this half term?

Find out which unis are hot off the mark here

Poll
Useful resources