PCM track length code c++ 'bool' issue
From C++ to PHP, debugging to webhosting; help and discussion about writing your latest program to running your website. NOT for help when your PC won't work.
| Announcements | Posted on | |
|---|---|---|
-
PCM track length code c++ 'bool' issue
So heres the code:
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?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; } } } }Last edited by AaronT; 23-04-2012 at 12:07. -
Re: PCM track length code c++ 'bool' issueSo 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...(Original post by JGR)
sound_data[sample_count]=0 sets the value of sound_data[sample_count] to 0.
Also, I haven't made two threads for this issue... -
Re: PCM track length code c++ 'bool' issue
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; } } } } -
Re: PCM track length code c++ 'bool' issueThis is a nasty side effect in the C / C++ languages(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...
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 ==. -
Re: PCM track length code c++ 'bool' issueThanks! Thats exactly what i needed.(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 ==. -
Re: PCM track length code c++ 'bool' issueIf you find yourself getting caught out by this, you could always flip the equality test:(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?
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.Code:if (0 == x)