13 Replies - 25345 Views - Last Post: 19 March 2009 - 03:03 AM Rate Topic: -----

#1 b-k24   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 02-February 09

How to assign multiple values to a single variable in C?

Posted 19 February 2009 - 02:07 PM

how to assign multiple values to a variable..i tried enum {0, 1, 2, 3, 4, 5} i; but it didn't compile.

target[0],target[1],target[2],target[3... i have an array of images int i;//how can i get this to refer to everything in my array  target[i]// i want to used this to refer to all my images

This post has been edited by b-k24: 19 February 2009 - 02:10 PM

Is This A Good Question/Topic? 0
  • +

Replies To: How to assign multiple values to a single variable in C?

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,233
  • Joined: 14-September 07

Re: How to assign multiple values to a single variable in C?

Posted 19 February 2009 - 02:11 PM

In short you can't. 'i' in your second question is the index variable, not to mention it is an integer and thus cannot be assigned an image or any other variable type for that matter. You can already access array data through the subscript operator. Maybe I'm not understanding the question correctly?
Was This Post Helpful? 0
  • +
  • -

#3 b-k24   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 02-February 09

Re: How to assign multiple values to a single variable in C?

Posted 19 February 2009 - 02:25 PM

so i can do this?
 i={0,1,2,3,4,5};
have i equal numbers 0-5?

i just want to have i equal to all these values individually so if i do this if(i==2)then do whatever.

This post has been edited by b-k24: 19 February 2009 - 02:29 PM

Was This Post Helpful? 0
  • +
  • -

#4 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,233
  • Joined: 14-September 07

Re: How to assign multiple values to a single variable in C?

Posted 19 February 2009 - 02:42 PM

Oh, then you just need to sum the elements:

for(int x = 0; x < arrSize; x++)
{
	 i += arr[x];
}


Was This Post Helpful? 0
  • +
  • -

#5 b-k24   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 02-February 09

Re: How to assign multiple values to a single variable in C?

Posted 20 February 2009 - 03:31 AM

I don't know but for some reason am still not getting it to work..I am trying to get collision for my array of objects i have a working collision detection i tested it on one of my objects, now i have add more objects and made and array of all the objects hoping i could just used target[i] to cover all of my objects in the collision detection.


int x[6]={0,1,2,3,4,5};
int i;


 for(i=0; i < 6; i++ )
{
x[i]= i+1;
} 


SO what I did is make an array X, six values then the loop as you showed me
but its still giving collision for my 1st object why??

This post has been edited by b-k24: 20 February 2009 - 03:37 AM

Was This Post Helpful? 0
  • +
  • -

#6 achal3754   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-January 09

Re: How to assign multiple values to a single variable in C?

Posted 20 February 2009 - 03:40 AM

View Postb-k24, on 20 Feb, 2009 - 02:31 AM, said:

I don't know but for some reason am still not getting it to work..I am trying to get collision for my array of objects i have a working collision detection i tested it on one of my objects, now i have add more objects and made and array of all the objects hoping i could just used target[i] to cover all of my objects in the collision detection.


int x[6]={0,1,2,3,4,5};
int i;


 for(i=0; i < 6; i++ )
{
x[i]= i+1;
} 


SO what I did is make an array X, six values then the loop as you showed me
but its still giving collision for my 1st object why??

x[i]= i+1; This is an assignment operation not a comparison operation
Was This Post Helpful? 0
  • +
  • -

#7 b-k24   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 02-February 09

Re: How to assign multiple values to a single variable in C?

Posted 20 February 2009 - 06:19 AM

please explain what you mean by that
Was This Post Helpful? 0
  • +
  • -

#8 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,233
  • Joined: 14-September 07

Re: How to assign multiple values to a single variable in C?

Posted 20 February 2009 - 07:25 AM

'=' means: set what is to the left of this sign equal to what is on the right

'==' means: is the left hand side equal to or equivalent to the right hand side
Was This Post Helpful? 0
  • +
  • -

#9 b-k24   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 02-February 09

Re: How to assign multiple values to a single variable in C?

Posted 20 February 2009 - 07:45 AM

i dnt want to sound dumb but is it my left to right or the code's left to right?
Was This Post Helpful? 0
  • +
  • -

#10 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,233
  • Joined: 14-September 07

Re: How to assign multiple values to a single variable in C?

Posted 20 February 2009 - 08:01 AM

Those are one and the same:

lhs = rhs;

lhs == rhs;


Was This Post Helpful? 0
  • +
  • -

#11 b-k24   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 02-February 09

Re: How to assign multiple values to a single variable in C?

Posted 20 February 2009 - 08:22 AM

ok thanks how can i use this to slove my problem?
Was This Post Helpful? 0
  • +
  • -

#12 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,233
  • Joined: 14-September 07

Re: How to assign multiple values to a single variable in C?

Posted 20 February 2009 - 08:27 AM

What is your goal? You have an initialized array and then you loop through and check it to see if it equals (i+1)? Is that the collision you're referring to?

int x[6]={0,1,2,3,4,5};
int i;

 for(i=0; i < 6; i++ )
{
		if(x[i] == i+1)
			//do something if x[i] is equal to i+1
}



Is that right?

This post has been edited by KYA: 20 February 2009 - 11:18 AM

Was This Post Helpful? 0
  • +
  • -

#13 b-k24   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 02-February 09

Re: How to assign multiple values to a single variable in C?

Posted 20 February 2009 - 11:08 AM

I already have the collision function working fine...now in the collision function i used target[i] (which at the time of testing i set i=0;)in which loads the image target[0] for my collision detection( I have 6 images target[0],target[1],target[2] etc). But now i want to use the collision function for all my array of images so i was wondering if i could get ("i" //variable) to equal to 0,1,2,3,4,5 which corresponds with all my images target[0],target[1],target[2] etc.So in the end all i will have in my collision function is target[i] which will cover collision for my 6 images.



EDIT: home and tried it,many different ways and still i only get collision for just one of my targets, ahhhhhhhh this thing is driving me nuts!

This post has been edited by b-k24: 20 February 2009 - 03:54 PM

Was This Post Helpful? 0
  • +
  • -

#14 zan007   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-March 09

Re: How to assign multiple values to a single variable in C?

Posted 19 March 2009 - 03:03 AM

View PostKYA, on 20 Feb, 2009 - 07:27 AM, said:

What is your goal? You have an initialized array and then you loop through and check it to see if it equals (i+1)? Is that the collision you're referring to?

int x[6]={0,1,2,3,4,5};
int i;

 for(i=0; i < 6; i++ )
{
		if(x[i] == i+1)
			//do something if x[i] is equal to i+1
}



Is that right?


hey use your own class and overload the operator as per ur requirements that will solve your problem
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1