Custom String Formatting#

Introduction#

String formatting in C++ for VEX V5 uses printf-style format specifiers to create strings that include variables, expressions, or values. This method uses format specifiers like %d, %f, %s, etc., to insert values into strings.

Formatting Numbers — Insert values and expressions into a string.

  • %d — Formats integers.

  • %.xf — Sets the number of decimal places to show for floats.

  • %x — Formats a number as hexadecimal.

  • %c — Formats single characters.

  • %s — Formats C-style strings.

Combining Strings — Combine text and values.

  • strcat — Concatenate C-style strings manually.

Checking for Substrings — Test for presence or position of text.

  • strstr — Finds a substring within a string.

Escape Sequences — Format output with special characters.

  • \n — Adds a line break (new line).

Formatting Numbers#

C++ uses format specifiers to control how numbers and other data types appear in strings.

Integers#

%d formats integer values.

Usage:
%d

Parameters

Description

This method has no parameters.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  int count = 42;

  // Display a single variable 
  Brain.Screen.print("Count: %d", count);
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  int x = 1;
  int y = 5;

  // Display multiple variables
  Brain.Screen.print("Position: (%d, %d)", x, y);
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Display a calculation
  Brain.Screen.print("Sum: %d", 5 + 3);
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Display the robot's battery capacity
  Brain.Screen.print("Battery: %d%%", Brain.Battery.capacity());
}

Fixed Decimal Places#

%.xf controls how many decimal places a floating-point number is displayed with.

Usage:
%.xf

Parameters

Description

x

The number of decimal places to show.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Display pi with 2 decimal places
  double pi = 3.1415926535;
  Brain.Screen.print("Pi: %.2f", pi);
}

Hexadecimal#

%x converts numbers to hexadecimal (lowercase) or %X for uppercase.

Usage:
%x or %X

Parameters

Description

This method has no parameters.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Convert 255 to hexadecimal
  int number = 255;

  // Display hexadeximals 
  Brain.Screen.print("Hex: %x", number);

  Brain.Screen.print("Hex: 0x%X", number);
}

Characters#

%c formats single characters.

Usage:
%c

Parameters

Description

This method has no parameters.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  char letter = 'A';

  // Display a single character variable
  Brain.Screen.print("Letter: %c", letter);
}

Strings#

%s formats C-style strings (character arrays).

Usage:
%s

Parameters

Description

This method has no parameters.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  char message[] = "V5";

  // Display a string variable
  Brain.Screen.print("Hello, %s", message);
}

Combining Strings#

strcat can combine two strings together.

Note: Make sure that the string has enough space reserved for the characters that are being appended. Adding characters to a string that is too small can overwrite other variables and cause unexpected behavior.

Usage:
strcat(string1, string2)

Parameters

Description

string1

The string to add onto. Must have space for the new text.

string2

The string you want to add.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Make sure the variable has enough space
  // for all characters (set it to 20)
  char message[20] = "Hello,";
  strcat(message, " V5");

  // Display the combined strings
  Brain.Screen.print("%s", message);
}

Checking for Substrings#

strstr returns a pointer to the position of the first occurrence of a C-style string in another string. This will return NULL if the string is not found.

Usage:
strstr(str, substring)

Parameters

Description

str

A pointer to the string being searched.

substring

The string to search for.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  char message[] = "Hey everyone!";

  // Display if "Hey" is in the variable
  if (strstr(message, "Hey") != NULL) {
      Brain.Screen.print("Hello!");
  }
}

Escape Sequences#

Escape sequences are special characters used inside strings to format text output.

New Line#

\n moves text to a new line when printing.

Note: This only works on the console. Text will not be sent to the console until a new line is started.

Parameters

Description

This method has no parameters.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Display text on two lines
  printf("First line\nSecond line\n");
}