Results are out! Find what you need...fast. Get quick advice or join the chat
x

Unlock these great extras with your FREE membership

  • One-on-one advice about results day and Clearing
  • Free access to our personal statement wizard
  • Customise TSR to suit how you want to use it

PCM track length code c++ 'bool' issue

Announcements Posted on
Find your uni forum to get talking to other applicants, existing students and your future course-mates 27-07-2015
Win top prizes in our mega results day giveaway... 24-07-2015
  1. Offline

    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?
  2. Offline

    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.
  3. Offline

    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...
  4. Offline

    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;
    				}
    			}
    		}
    	}
  5. Offline

    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 ==.
  6. Offline

    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.
  7. Offline

    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.
  8. Offline

    ReputationRep:
    Or, for that matter, increase the warning level you're using when compiling.

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. By joining you agree to our Ts and Cs, privacy policy and site rules

  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
Would you consider Clearing if you missed your offer?
Results and Clearing

Results are coming...

No sweat. Here's all you need to make sure you're ready

new on tsr

What's your life ambition?

Graduating, travelling, owning a business?

Useful resources
x

Think you'll be in clearing or adjustment?

Hear direct from unis that want to talk to you

Get email alerts for university course places that match your subjects and grades. Just let us know what you're studying.

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