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
Sign in to Reply
  1. AaronT's Avatar
    • Full Member
    PCM track length code c++ 'bool' issue
    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?
    Last edited by AaronT; 23-04-2012 at 12:07.
  2. JGR's Avatar
    • Exalted and Worshipped Member
    • Posts: 1,240
    Re: PCM track length code c++ 'bool' issue
    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. AaronT's Avatar
    • Full Member
    Re: PCM track length code c++ 'bool' issue
    (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...

    Also, I haven't made two threads for this issue...
  4. AaronT's Avatar
    • Full Member
    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;
    				}
    			}
    		}
    	}
  5. davros's Avatar
    • Overlord in Training
    • Location: Skaro
    Re: PCM track length code c++ 'bool' issue
    (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. AaronT's Avatar
    • Full Member
    Re: PCM track length code c++ 'bool' issue
    (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. vox's Avatar
    • Junior Member
    Re: PCM track length code c++ 'bool' issue
    (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. TheUnbeliever's Avatar
    • TSR Demigod
    • Location: Scotland
    • Posts: 5,838
    Re: PCM track length code c++ 'bool' issue
    Or, for that matter, increase the warning level you're using when compiling.
Sign in to Reply
Share this discussion:  
Useful resources
Article updates
Moderators

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

Reputation gems:
The Reputation gems seen here indicate how well reputed the user is, red gem indicate negative reputation and green indicates a good rep.
Post rating score:
These scores show if a post has been positively or negatively rated by our members.