How to append a long to a string in C++? - 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

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.

Related

How to check if my string input followed the correct syntax in C++?

I'm new to this site and this is my first time to ask here.
My problem is I want to check if my string follows a correct pattern or syntax. I'm doing it with C++ String (std::string). I have already done this using C-Style string, however, I want to do it this time in C++ String. Sample problem below:
Input: 2y'' + 3y' - 2y = 0
or y'' = 4y
I want to check if the derivative input is in correct syntax like (a)y'' + (b)y' + (c)y = 0, a second order homogeneous equation. However, I still want to input a non-standard form equation like the second sample input that can be transposed and make it to standard form.
What I did before with it is remove all the white spaces, loop the entire string and check every index. Eg. if 'y' is found the next char should be '\'' or an arithmetic symbol like '-' or '+' or '=' then if it does not match, then, it must return false.
Or maybe I am just implementing this wrong. I'm new to programming and just taking a computer science course. Note: Sorry for my bad English and sorry if I did not written my code here. Its just way too long.
Regular expressions might be the answer. They're commonly used for checking whether a string first a format, or finding parts of a string that do.
RegExr is a great tool to both learn and test your regular expressions.

Appending spaces AND one-word-strings to the end of a string

I'm having a problem which seems simple but I just can not get it to work. I'm using the standard C++ function append() to add BOTH a space, " ", and another one-word-string (str2) to the end of another string (str1)
My code works perfectly fine when I only append one or the other, i.e.:
str1.append(" ");
or:
str1.append(str2);
However, when I try to append both in a row as such:
str1.append(" ");
str1.append(str2);
I immediately get a segmentation error. I am very confused as to how it can handle one append, but not two! Does anyone see a work-around?
Thanks in advance!
So originally str2 was actually a double that I had stored as a string, which should still work. However for some reason C++ wouldn't use it even though it was a string. So instead I switched str2 back into a double using stod(str2), and then back into a string again at concatenation as such:
str2=stod("stuff");
str1.append(to_string(str2));
No idea why this works while the other way doesn't (both methods input a string into append()), but it works!

Store an Ascii char in an array in c++

first of all thanks for reading ^ ^
ok, so I'm making an easy menu for a console project
after I created the options I wanted to add the char(240) as another option but I can't figure out how to declare it I cant just write ≡ because Dev++ won't let me write it, the code is:
char *menu_list[6] = { " SYMBOL GOES HERE "," View ", " Edit ", " Search ", " Reset", " Quit " };
does anyone know how to do this? or if I'm doin it all wrong, can you tell me the right way to do it?
i'm forced to make it work on windows, i can
cout << char(240);
and it works right, but I cannot store that same symbol into menu_list
also I got the code from here
http://www.theasciicode.com.ar/extended-ascii-code/hyphen-ascii-code-240.html
There was a deleted answer that had the correct response: use "\xf0" to represent the character.
Ordinarily you would need to know which code page is being used by Windows to know how a character code maps to a particular character on screen. However by doing the research yourself, you already know that the decimal value 240 represents the character you need. That decimal 240 is the same as hex f0, and the way to embed a hex literal value in a string is to prefix it with \x.
As noted in the link you posted, these codes correspond to code page 437. This is the one used by the command window in English versions of Windows; I'm not sure if alternate language versions of Windows use anything different. Calling this "extended ASCII" is confusing, since there have been many attempts to extend ASCII over the years and none of them are the same.

How can I recognize RTL strings in C++

I need to know the direction of my text before printing.
I'm using Unicode Characters.
How can I do that in C++?
If you don't want to use ICU, you can always manually parse the unicode database (.e.g., with a python script). It's a semicolon-separated text file, with each line representing a character code point. Look for the fifth record in each line - that's the character class. If it's R or AL, you have an RTL character, and 'L' is an LTR character. Other classes are weak or neutral types (like numerals), which I guess you'd want to ignore. Using that info, you can generate a lookup table of all RTL characters and then use it in your C++ code. If you really care about code size, you can minimize the size the lookup table takes in your code by using ranges (instead of an entry for each character), since most characters come in blocks of their BiDi class.
Now, define a function called GetCharDirection(wchar_t ch) which returns an enum value (say: Dir_LTR, Dir_RTL or Dir_Neutral) by checking the lookup table.
Now you can define a function GetStringDirection(const wchar_t*) which runs through all characters in the string until it encounters a character which is not Dir_Neutral. This first non-neutral character in the string should set the base direction for that string. Or at least that's how ICU seems to work.
You could use the ICU library, which has a functions for that (ubidi_getDirection ubidi_getBaseDirection).
The size of ICU can be reduced, by recompiling the data library (which is normally about 15MB big), to include only the converters/locals which are needed for the project.
The section Reducing the Size of ICU's Data: Conversion Tables of the site http://userguide.icu-project.org/icudata, contains information how you can reduce the size of the data library.
If only need support for the most common encodings (US-ASCII, ISO-8859-1, UTF-7/8/16/32, SCSU, BOCU-1, CESU-8), the data library wont be needed anyway.
From Boaz Yaniv said before, maybe something like this will easier and faster than parsing the whole file:
int aft_isrtl(int c){
if (
(c==0x05BE)||(c==0x05C0)||(c==0x05C3)||(c==0x05C6)||
((c>=0x05D0)&&(c<=0x05F4))||
(c==0x0608)||(c==0x060B)||(c==0x060D)||
((c>=0x061B)&&(c<=0x064A))||
((c>=0x066D)&&(c<=0x066F))||
((c>=0x0671)&&(c<=0x06D5))||
((c>=0x06E5)&&(c<=0x06E6))||
((c>=0x06EE)&&(c<=0x06EF))||
((c>=0x06FA)&&(c<=0x0710))||
((c>=0x0712)&&(c<=0x072F))||
((c>=0x074D)&&(c<=0x07A5))||
((c>=0x07B1)&&(c<=0x07EA))||
((c>=0x07F4)&&(c<=0x07F5))||
((c>=0x07FA)&&(c<=0x0815))||
(c==0x081A)||(c==0x0824)||(c==0x0828)||
((c>=0x0830)&&(c<=0x0858))||
((c>=0x085E)&&(c<=0x08AC))||
(c==0x200F)||(c==0xFB1D)||
((c>=0xFB1F)&&(c<=0xFB28))||
((c>=0xFB2A)&&(c<=0xFD3D))||
((c>=0xFD50)&&(c<=0xFDFC))||
((c>=0xFE70)&&(c<=0xFEFC))||
((c>=0x10800)&&(c<=0x1091B))||
((c>=0x10920)&&(c<=0x10A00))||
((c>=0x10A10)&&(c<=0x10A33))||
((c>=0x10A40)&&(c<=0x10B35))||
((c>=0x10B40)&&(c<=0x10C48))||
((c>=0x1EE00)&&(c<=0x1EEBB))
) return 1;
return 0;
}
If you are using Windows GDI, it would seem that GetFontLanguageInfo(HDC) returns a DWORD; if GCP_REORDER is set, the language requires reordering for display, for example, Hebrew or Arabic.

ICU Custom Currency Formatting (C++)

Is it possible to custom format currency strings using the ICU library similar to the way it lets you format time strings by providing a format string (e.g. "mm/dd/yyy").
So that for a given locale (say USD), if I wanted I could have all currency strings come back "xxx.00 $ USD".
See http://icu-project.org/apiref/icu4c/classDecimalFormat.html,
Specifically: http://icu-project.org/apiref/icu4c/classDecimalFormat.html#aadc21eab2ef6252f25eada5440e3c65
For pattern syntax see: http://icu-project.org/apiref/icu4c/classDecimalFormat.html#_details
I didn't used this but from my knowledge of ICU this is the direction.
However I would suggest to use:
http://icu-project.org/apiref/icu4c/classNumberFormat.html and createCurrencyInstance member and then use setMaximumIngegerDigits or other functions to make what you need -- that would be much more localized. Try not assume anything about any culture. Because "10,000 USD" my be misinterpreted as "$ 10" in some countries where "," used for fraction part separation.
So be careful.
You can create a currency instance, then if it is safe to cast it to a DecimalFormat
if (((const NumberFormat*)fmt)->getDynamicClassID() == DecimalFormat::getStaticClassID())
{ const DecimalFormat* df = (const DecimalFormat*) fmt; ...
… then you can call applyPattern on it. See the information on ¤, ¤¤, ¤¤¤ under 'special pattern chars'
Use the ICU library's createCurrencyInstance().