SoulfireFox
SoulfireFox

Reputation: 13

How to append a long to a string in C++?

I'm working on a school project and I'm making a VEX Robotics program. It's in C++, and I am very new to this language. I can't import any new libraries to help me and I want to display a value on a screen. Unfortunately, I need to make it say "Left Stick tilt: " and then the tilt value of the VEX controller's left stick and the same with the right stick. The code should work aside from the fact that I can't simply add the two together and have the value of the controller tilt converted to numerical characters. Here's my code:

Controller1.Screen.setCursor(1, 1);
Controller1.Screen.print("Left Stick tilt: " + Controller1.Axis3.position());
Controller1.Screen.setCursor(2, 1);
Controller1.Screen.print("Right Stick tilt: " + Controller1.Axis2.position());

Could anyone experienced with the VEX system help me? (I'm using VEXcode V5 on a chromebook, if it makes any difference)

Edit: so far everyone has recommended things within libraries. I was not clear enough; I cannot use any libraries, including the standard library, due to the limitations of VEXcode V5

Upvotes: 0

Views: 590

Answers (1)

eerorika
eerorika

Reputation: 238321

How to append a long to a string in C++?

In order to append long to a string, you must convert the integer to a string. You can for example use std::to_string.

You can append to another string like this:

long l = 42;
std::string s = "look at this long: ";
s += std::to_string(l);

Alternatively, you can use a format string. For example:

std::string s = std::format("look at this long: {}", l);

However, for purposes of output, don't necessarily need to append to the string. Instead, you could keep them separate, output the string, and then output the long.

Upvotes: 4

Related Questions