ROBOTC.net forums
http://robotc.net/forums/

converting int to string
http://robotc.net/forums/viewtopic.php?f=1&t=9229
Page 1 of 1

Author:  albornzineC [ Mon Aug 18, 2014 5:30 pm ]
Post subject:  converting int to string

i am trying to code a program that will allow me to see a number on the nxt display screen and be able to change that number by pressing the nxt buttons and my problem is that when i go to display the number i use the line: nxtDisplayCenteredTextLine(6, answer); and answer is a int. i thought that it would just display the int that i have answer as, but it gives me this error:**Error**:Procedure call Parameters don't match declaration for 'nxtDisplayCenteredTextLine(const long nLineNumber, const unsigned string & sFormatString, ...)'.

here is the rest of the code for that function:

Code:
#pragma config(Sensor, S3,     touch,          sensorTouch)
#pragma config(Motor,  motorA,          left,          tmotorNXT, PIDControl, encoder)
#pragma config(MotorPidSetting,  motorB,  255, 255, 65535, 255, 255,   65535, 65535, 65535)
#pragma config(MotorPidSetting,  motorC,  255, 255, 65535, 255, 255,   65535, 65535, 65535)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#define nxtorange 3
#define nxtleft 2
#define nxtright 1
#define nxtgrey 0

int Question1()
{
   int answer
   nxtDisplayCenteredTextLine(1, "left = add"); // Displays instructions
   nxtDisplayCenteredTextLine(2, "right = subtract"); // Displays instructions
   nxtDisplayCenteredTextLine(3, "press grey when done"); // Displays instructions
   nxtDisplayCenteredTextLine(6, answer)

   while(sensorvalue[nxtgrey] = -1)
   {
      if(nNxtButtonpressed == nxtright)// checks if the right btn is pushed
      {
         answer = answer + 1
         nxtDisplayClearTextLine(6)
         nxtDisplayCenteredTextLine(6, answer)
      }

      if(nNxtButtonPressed == 2)// checks if the left btn is pushed
      {
         answer = answer - 1
         nxtDisplayClearTextLine(6)
         nxtDisplayCenteredTextLine(6, answer)
      }

   }

   return answer;
}

Author:  Indy [ Mon Aug 18, 2014 10:33 pm ]
Post subject:  Re: converting int to string

Use the sprintf routine.

Declare a buffer string variable:
Code:
char buf[5];


To display convert the integer to a string:
Code:
sprintf(buf, "%d", answer);
nxtDisplayCenteredTextLine(6, buf);


Hope that helps.

Author:  mightor [ Mon Aug 18, 2014 11:33 pm ]
Post subject:  Re: converting int to string

nxtDisplayTextLine(6, "%d", answer) should also work.

= Xander

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/