I have a stack of UTF8 characters:
stack<wchar_t> tokenStack;
i try to add to it like this:
void doLeftParen() { tokenStack.push( L"(" ) }
but the compiler doesn't like it:
$ g++ PropositionalLogic.cpp -o PropositionalLogic
PropositionalLogic.cpp:27:39: error: reference to type 'const value_type'
(aka 'const wchar_t') could not bind to an lvalue of type 'const wchar_t [2]'
void doLeftParen() { tokenStack.push( L"(" ) }
I tried searching for the error, but came up empty. I'm not really sure what else i should be searching for. I've also tried just adding a regular ASCII character, but same error. How can i add characters to this stack?
You're trying to push a string not a char, changing the double quotes to single quotes will make it work, also you're missing a semi-colon at the end of the push function
void doLeftParen() { tokenStack.push( L'(' ); }
You are almost right:
From lex.ccon
A character literal that begins with the letter L, such as L'z',
is a wide-character literal.
Characters with prefix L is not a UTF8 character literal.
The compile error in your code is L"(" (w/ double-quotes) is a multicharater literal but the code expects it to be a stack of wide character literals.
Change it to single-quotes:
L'(' // now a wide-char literal
Related
I need a function that convert hexadecimal char pointer to string:
ex:
std::string Myfunction(char* hexacode)
{
std::string output;
//
//
return output;
}
std::string Result = Myfunction("\x55\x8B\xEC\x83\xEC\x14\x53\x56\x8B\x75\x0C");
In short I need to convert in string this parameter or similar.
Because in the output the backslash is a option can be a solution replace backslash with slash if is too complicate keep the backslash.
Many thanks !!
If you want the user to input the string, you don't have to care about anything, string escaping only happens for constant strings in the code file.
If you don't want the constant strings in the code file to be escaped, no function conversion needed, just do this:
"\\x55\\x8B\\xEC\\x83\\xEC\\x14\\x53\\x56\\x8B\\x75\\x0C"
I'm trying run a netsh command with the system() function in C++.
Here is my code:
#include<iostream> // cin / cout
#include<stdlib.h> // system()
using namespace std;
int main(){
system('netsh interface show interface | findstr /C:"Wi-Fi" /C:"Name"');
}
I think that I need to add something before the 'netsh to solve this error but I don't know what character, I already try: system(L'netsh interface show interface | findstr /C:"Wi-Fi" /C:"Name"'); but no success,
You are passing in a multi-character literal instead of a string literal. Using single-quotes '...' creates a single char, which is a numeric type that can be promoted to int, that is why you are getting the error about an int being passed in where a const char* is expected. system() expects a null-terminated C-style string instead, ie an array of char values ending with the '\0' character. In string literal form, you use double-quotes "..." instead to create such an array.
You need to replace the ' characters with ". And then you also need to escape the inner " characters using \, eg:
system("netsh interface show interface | findstr /C:\"Wi-Fi\" /C:\"Name\"");
Alternatively, in C++11 and later, you can use a raw string literal instead to avoid escaping the inner " characters:
system(R"(netsh interface show interface | findstr /C:"Wi-Fi" /C:"Name")");
I am trying to use the ignore function skip a few lines, but the parameters of the function are oddly different. Shouldn't it be a streamsize(amount of characters and a delimiter(to stop ignoring up to the assigned character). The problem i am having is that the 2nd parameter for me it is required to be an integer. While i want to use "\n" it doesn't accept it because it is char.
std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::ignore(std::streamsize,int)': cannot convert argument 2 from 'const char [2]' to 'int'
"\n" (with double quotes) is a string literal, not a char literal. In this case, it's an array of two chars; equivalent to {'\n', '\0'}.
'\n' (with single quotes) is a char literal. It represents a single newline character.
std::istream::ignore accepts only a single character as its delimiter, so you have to use the latter.
Note: std::istream::ignore's second parameter is an int rather than a char so that it can accommodate the extra "end of file" pseudo-character. The eof value has to be different than any valid character value, so the type used for the delimiter must be wider than char.
Is there a simple way to escape all occurrences of \ in a string? I start with the following string:
#include <string>
#include <iostream>
std::string escapeSlashes(std::string str) {
// I have no idea what to do here
return str;
}
int main () {
std::string str = "a\b\c\d";
std::cout << escapeSlashes(str) << "\n";
// Desired output:
// a\\b\\c\\d
return 0;
}
Basically, I am looking for the inverse to this question. The problem is that I cannot search for \ in the string, because C++ already treats it as an escape sequence.
NOTE: I am not able to change the string str in the first place. It is parsed from a LaTeX file. Thus, this answers to a similar question does not apply. Edit: The parsing failed due to an unrelated problem, the question here is about string literals.
Edit: There are nice solutions to find and replace known escape sequences, such as this answer. Another option is to use boost::regex("\p{cntrl}"). However, I haven't found one that works for unknown (erroneous) escape sequences.
You can use raw string literal. See http://en.cppreference.com/w/cpp/language/string_literal
#include <string>
#include <iostream>
int main() {
std::string str = R"(a\b\c\d)";
std::cout << str << "\n";
return 0;
}
Output:
a\b\c\d
It is not possible to convert the string literal a\b\c\d to a\\b\\c\\d, i.e. escaping the backslashes.
Why? Because the compiler converts \c and \d directly to c and d, respectively, giving you a warning about Unknown escape sequence \c and Unknown escape sequence \d (\b is fine as it is a valid escape sequence). This happens directly to the string literal before you have any chance to work with it.
To see this, you can compile to assembler
gcc -S main.cpp
and you will find the following line somewhere in your assembler code:
.string "a\bcd"
Thus, your problem is either in your parsing function or you use string literals for experimenting and you should use raw strings R"(a\b\c\d)" instead.
I'm trying to write a function that takes an input string, a regex (made by std.regex.regex from a rawstring) and an error message string, and attempt to match something from the input string using the regex, displaying the error message if there are no matches. I came up with the following signature so far:
string check_for_match (string input, Regex r, string error_message)
However, this doesn't seem to work, as the compiler complains, saying:
struct std.regex.Regex(Char) is used as a type
So what should I use instead?
It'll compile if you change Regex to Regex!char.
The reason is that Regex is a template that can use any character size: char for UTF-8 patterns, wchar for UTF-16, or dchar for UTF-32. The compiler is saying you need to create a type by passing the required Char argument there to use it here.
Since you are working with string, which is made up of chars, Regex!char is the type to use.
string check_for_match (string input, Regex!char r, string error_message) { return null; }