Visual studio not executing code correctly - c++

Yeah so ive tried lots of stuff but the output keeps coming out all messed up. Heres a picture showing whats going on. As you see the letter variable gets all weird.

The problem is that "letter: " + letter" doesn't do what you think it does, it adds the integral value of letter to the const char* string literal "letter". Easiest fix is string("letter: ") + letter.

Related

weird visual studio to linux results

I have this project where we have to convert a made up simple coding language given through a txt into a c++ language and currently I am having a problem with checking my work through linux (where it will be tested on). On the other hand Visual Studio where I wrote my code works how it should be.
From what I observed something weird is going on when I am taking each line in the text, separating each word and placing it in a vector. How I do this is by starting with an empty string, looking at each character of the line, and when I see a space or a tab or any kind of character that signifies a separator then I push back that string to a vector then make the string empty again for the next characters. So something line "STR man = bloo" turns into <"STR", "man", "=", "bloo"> and so on.
In visual studio it worked like this, but it seems like in linux.. somehow the second element seems to add a weird character which would be "man ". Its not a space or an empty line and when I tried to look at size with a format std::cout << name << ":" << size << std::endl, it couts not the right format or result.
enter image description here
enter image description here
Looking at visual Studio.
first line: "size" how many elements in that vector.
second line: is a name that I want to check if its already in a vector along with the length of that name
third line: shows all the elements in that vector (which should only show 1 since the size is 1) along with its length as well.
Looking at the linux result
-first line: "size" equals 1. Which is right
second line: the name im checking and its length, which is right
third line: is where the problem occurs. The name is 1 character greater than what it is suppose to be and instead of adding onto whats printed out, it replaces the first 3 letters instead?
fourth line: should not even happen because the size is only 1 and its a basic for loop of (int i = 0; i < size; i++)
Ive tried to use brute force and just pop back the last character of the second element causing my visual studio code to fail the tests but that still fails the conditional of if the name is = to the name in the vector (which should be true) in linux. Whatever character I add on the second element when I cout that value, such as << name << "." << endl; it seems to always go and replaces first letters of that string.
Pls help
Sorry for the delay, the problem was that I was not handling invisible characters such as \r.

Right to left isolate string. C++

does anyone have experience in Unicodes?
I am facing a tough problem with Farsi unicodes.
I have an std::wstring s = (L"\u0634\u0646\u0628\u0647"); which is a Farsi word. When I debug it, I see that the underlying word is exactly what I want, but reversed. So I have researched and found that u2067 is for right to left reading the string.
NOTE:
I cannot reverse the string manually because Farsi characters are changing their shape regardless of their position in the string.
So I added the 2067 int the beginning and got
std::wstring s = (L"\u2067\u0634\u0646\u0628\u0647");.
But now the underlying string is the same , just added a square in the beginning if the string instead of reversing.
Does anyone have experince with this stuff? Please suggest a solution. Thanks!
The underlying string will be the same. You haven't changed the order of bytes, which is written right there in the code. But a renderer that understands Unicode should take those bytes and display the characters right-to-left. That's a visual thing. It has nothing to do with the encoding. From your question, it's not entirely clear what else you expected. It may be that you are viewing the string in a debugger, and the debugger does not support this feature of Unicode. If you try outputting the string to a proper console you ought to see it as you expect.

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.

boost regex match non-whitespace and angle brackets

I may be asking a duplicate question, but I've spent a couple of hours googling this to no avail!
I'm trying to extract a string from some SIP URLs parsed by a program I'm working on. Here's an excerpt of the code. I'm passing in sipUrl, and have all the right includes etc:
static const boost::regex sipRegExp ("(sip:\\S+?#(?=\\S)[^>]+);");
boost::cmatch result;
boost::match_results<string::const_iterator> results;
boost::match_flag_type flags = boost::format_perl;
string newSipUrl;
cout << sipUrl << endl;
bool toggle = boost::regex_search(sipUrl, result, sipRegExp, flags);
if (toggle) {
cout << result[1].str() << endl;
newSipUrl = result[1].str();
}
cout << "new url: " << newSipUrl << endl;
I'm basically trying to extract the sip:user#IP from strings like "\"alex#192.168.1.2\"<sip:alex#192.168.1.2>;tag=fe310852" or "\"bob\"<sip:bob#foo.com>;", however, I can't get it to match! It worked fine when I wasn't using lookahead to try and remove the last angle bracket, but ever since then it fails to match.
Posting this just before running out of the door, so it may need more info. If anyone can spot something glaringly obvious, then that'd be a great help! And please feel free to point me at links that I might have missed!
Have you tried something simpler such as regex against:
`sip:[a-zA-Z]*#[0-9a-zA-Z.]*`
works on terminal but haven't tried it through boost yet. If you start of with something simple then add bit by bit to make it more specific then it will be easier to track which part of the regex isn't working.
You missed the > before the semicolon:
"(sip:\\S+?#(?=\\S)[^>]+)>;"
Although actually you probably don't need the semicolon at all. Something like Scott's answer should be sufficient.
I ended up going with a modification of #David Knipe's comment - the winning regex was:
sip:\\S+#[^\\s>;]+
Which matches with or without angle brackets, up to the colon. Both answers provided did work, but being able to remove the lookahead was quite nice. I also went with the + modifiers to make some effort to find a valid URI and not a blank one.
Thanks for the help!

How to delete an already written character in C++ console

I'm trying to make a C++ program to read a password.
I made the program to cout *, not the characters I write but my problem is when I want to delete character because they're wrong.
Example:
My constant password is 12345
If I enter 1235 the program will show **** and I have to delete the last character. It's simple to remove it from the string but I want the last * to disappear from the console just as it happens when you introduce you windows password.
Is it possible? If it is, can someone explain how?
Outputting the backspace character '\b' may help to move the output point back.
Specifically, outputting the string "\b \b" should blank out the last character output.
printf("\b ");
This statement surely works because after the cursor goes one character back and space given in above printf statement will overwrite the printed character on console output.
When I am writting to console using putch function ( from conio.h ) to simulate backspace key
simple
std::cout << '\b';
or
printf("\b ");
not works
I have to write:
cout << '\b' << " " << '\b';
or
putch('\b');
putch(' ');
putch('\b');
Try backspace \b or erase the whole line and print it again.
Sipmly write the '\b' character to stdout std::cout<<"\b". If you are using cpp or printf("\b") for pure C
I faced the same thing recently, using the \b was** moving the cursor backward** but it was not deleting the char (or * ). I realize after that the next character you enter after \b is gonna replace the (character to be deleted). So, I came up with something like this
printf(“\b \b”);
using the space key after \b will replace the character to be deleted and again using the \b will take you one position back again (to the position where was incorrect character was, but this time the character will not be there) This way your output will look smooth. hope it will be helpful to you