Custom String Formatting#
Introduction#
String formatting in C++ for VEX IQ 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.
int x = 1;
int y = 5;
// Display the variables using format specifiers
Brain.Screen.print("Position: (%d, %d)", x, y);
// Display a calculation using format specifiers
Brain.Screen.print("Sum: %d", 5 + 3);
// Display the robot's battery capacity using format specifiers
Brain.Screen.print("Battery: %d%%", Brain.Battery.capacity());
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.
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 count = 42;
Brain.Screen.print("Count: %d", count);
// Output: Count: 42
Fixed Decimal Places#
%.xf
controls how many decimal places a floating-point number is displayed with.
Usage:
%.xf
Parameters |
Description |
---|---|
|
The number of decimal places to show. |
// Display pi with 2 decimal places
double pi = 3.1415926535;
Brain.Screen.print("Pi: %.2f", pi);
// Output: Pi: 3.14
Hexadecimal#
%x
converts numbers to hexadecimal (lowercase) or %X
for uppercase.
Usage:
%x
or %X
Parameters |
Description |
---|---|
This method has no parameters. |
// Convert 255 to hexadecimal
int number = 255;
Brain.Screen.print("Hex: %x", number);
// Output: Hex: ff
Brain.Screen.print("Hex: 0x%X", number);
// Output: Hex: 0xFF
Characters#
%c
formats single characters.
Usage:
%c
Parameters |
Description |
---|---|
This method has no parameters. |
char letter = 'A';
Brain.Screen.print("Letter: %c", letter);
// Output: Letter: A
Strings#
%s
formats C-style strings (character arrays).
Usage:
%s
Parameters |
Description |
---|---|
This method has no parameters. |
char message[] = "IQ";
Brain.Screen.print("Hello, %s", message);
// Output: Hello, IQ
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 or it may start writing into memory that belongs to other variables.
Usage:
strcat(string1, string2)
Parameters |
Description |
---|---|
|
The string to add onto. Must have space for the new text. |
|
The string you want to add. |
char message[] = "Hello,";
strcat(message, " IQ");
Brain.Screen.print("%s", message);
// Output: Hello, IQ
Checking for Substrings#
strstr
returns a pointer to the position of the first occurrence of a C-style string in another string.
Usage:
strstr(str, substring)
Parameters |
Description |
---|---|
|
A pointer to the string being searched. |
|
The string to search for. |
Returns: Pointer to the first occurrence, or NULL
if not found.
char message[] = "Hey everyone!";
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. |
// Display text on two lines
printf("First line\nSecond line\n");
Tab Spacing#
\t
inserts a tab space between words or numbers.
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. |
// Display the quantity of barrels with tab spacing
int quantity = 2;
printf("Barrels:\t%d", quantity);