PCM track length code c++ 'bool' issue

Announcements Posted on
    • Thread Starter
    Offline

    0
    ReputationRep:
    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?
    Offline

    0
    ReputationRep:
    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.
    • Thread Starter
    Offline

    0
    ReputationRep:
    (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...
    • Thread Starter
    Offline

    0
    ReputationRep:
    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;
    				}
    			}
    		}
    	}
    Offline

    3
    ReputationRep:
    (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 ==.
    • Thread Starter
    Offline

    0
    ReputationRep:
    (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.
    Offline

    0
    ReputationRep:
    (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.
    Offline

    0
    ReputationRep:
    Or, for that matter, increase the warning level you're using when compiling.
 
 
 
Write a reply… Reply
Submit reply

Register

Thanks for posting! You just need to create an account in order to submit the post
  1. this can't be left blank
    that username has been taken, please choose another Forgotten your password?
  2. this can't be left blank
    this email is already registered. Forgotten your password?
  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
    your full birthday is required
  1. Oops, you need to agree to our Ts&Cs to register
  2. Slide to join now Processing…

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.

Poll
How are you feeling about doing A-levels?
Useful resources

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Quick reply
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.