Changing variables in NXC

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
penguinplus
Posts: 12
Joined: 16 Nov 2010, 01:29

Changing variables in NXC

Post by penguinplus »

I am new to NXC so how do I change a variable? I made the variable that turn = 0 and it is all set up. But how do I change it to another number?
I take the place of Penguinplus from the old forums.
h-g-t
Posts: 552
Joined: 07 Jan 2011, 08:59
Location: Albania

Re: Changing variables in NXC

Post by h-g-t »

Not quite sure what you mean here.

If you want to change the variable name throughout the program then use search and replace.

If you want to change the value of turn then any assignment will do -

turn = put in the name of any other variable here
turn = put in any number here
turn = turn + 16*2
put in the name of any other variable here = turn
put in the name of any other variable here = turn + 56

Is that the sort of thing you mean?
A sophistical rhetorician, inebriated with the exuberance of his own verbosity, and gifted with an egotistical imagination that can at all times command an interminable and inconsistent series of arguments to malign an opponent and to glorify himself.
penguinplus
Posts: 12
Joined: 16 Nov 2010, 01:29

Re: Changing variables in NXC

Post by penguinplus »

I try to change a variable in a if statement bot it gives me errors.
I take the place of Penguinplus from the old forums.
haydenstudios
Posts: 252
Joined: 22 Oct 2010, 20:05
Location: The United States of America
Contact:

Re: Changing variables in NXC

Post by haydenstudios »

It often helps to provide your code for us to read. Even more so, it is easier to read if you provide your code this way:

Code: Select all

[code](your code)
[/code]
Maybe you accidentally did the wrong combination of wether or not to use spaces for certain commands. It's a possible exlpanation for why your if statement isn't working, but like I said, it's best if you provide your code for us to see.
-Hayden
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Changing variables in NXC

Post by afanofosc »

penguinplus wrote:I am new to NXC so how do I change a variable? I made the variable that turn = 0 and it is all set up. But how do I change it to another number?
I think you should try reading the NXC Tutorial and the NXC Programmer's Guide. Both of these have many examples of how you use variables.

Code: Select all

task main()
{
  int turn = 0;
  turn++; // increment turn
  turn = 4; // set turn's value to 4
  turn += 4; // add 4 to turns current value
  turn = Random(); // set turn to a random number
}
To change a variable's value you use an assignment statement. lhs = rhs. I.e., the left hand side "equal sign" the right hand side. All of this is fully explained in the tutorial and programmer's guide. And in the example code that comes with BricxCC. There are also a ton of C tutorials on the internet which you could read to help understand the C programming language.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests