1

My program is NOT accepting symbols such as "à" considering some city names in the world have these as characters. I'm using C++. I've tried all sort of streams of input such as getline(cin,string), fgets and such. All these forms in gathering input see the special symbols at "...".

This is a windows console application made in visual studio 2015

for example

string funnyChar = "à";
getline(cin, checkFunny);
if (checkFunny.compare(funnyChar) == 0)
//do things

user inputs àero

ALT+0224ero

or user input àero as

ALT+133ero

within the code, the input stream cannot see the alt code inputed as it is, therefor, within my if statement

if(checkFunny.compare(funnyChar)==0)

will never work.

More or less, I need to be able to take in these (any) special characters so I can save them, and then display them later, but also, NOT accept very specific ones such as † (alt+0134)

Robolisk
  • 1,682
  • 5
  • 22
  • 49

3 Answers3

0

If you're trying to use those characters in the argument list of the program, that's not possible. main must be declared as either main(void)', 'main(int argc, char **argv)', or the semantic equivalent. I believe you would need something like 'main(int argc, wchar_t **argv)' which is not allowed per the standard.

If you're just trying to process the data after getting it elsewhere, I would say to try the wide char versions of those functions. fgetwc for example. Something like this maybe.

#include <iostream>
#include <string>
int main() {
  std::locale loc("");
  std::locale::global (loc);
  wcin.imbue(loc);
  std::wstring city;
  std::wcout << L"Enter your city: ";
  std::wcin >> city;
  std::wcout << L"What's it like in " << city << "?\n";
}

http://www.cplusplus.com/reference/cwchar/

spike.barnett
  • 170
  • 1
  • 8
  • These arent arugment inputs, they are inputs from the user. For example, if the user says the city is "àero" (made up city), then user will input (in a command promt) "ALT+0224 (for "à")ero" – Robolisk Mar 10 '16 at 20:17
  • Have you tried the wide char versions? Like wcin, wcout? For example. #include #include int main() { std::wstring city; std::wcout << L"Enter your city: "; std::wcin >> city; std::wcout << L"What's it like in " << city << "?\n"; } – spike.barnett Mar 10 '16 at 20:29
  • One more thing, if you use something like this, don't mix wcin and cin, or wcout and cout. http://www.cplusplus.com/reference/iostream/wcout/ – spike.barnett Mar 10 '16 at 20:37
  • Okay, try that. Full disclosure, this is a bit of voodoo programming. I've not looked into locales before. – spike.barnett Mar 10 '16 at 21:06
  • Windows has wmain which is main with wchar_t **argv. (https://msdn.microsoft.com/en-us/library/bky3b5dh.aspx) – Phil Lello Mar 10 '16 at 21:44
  • Windows never has been very good at keeping to standards. Joking aside, I misspoke when I said not allowed. I should have said not defined. Those are the only signatures defined in the standard. Anything else is implementation dependent and will hurt your portability. I think the only actual requirement for main is the int return type. – spike.barnett Mar 10 '16 at 22:10
0

C++ usually won't encode extended characters in the source (it is generally ASCII, UTF-7). Try

string funnyChar = "\x85";

and it should start working in the way you expect.

Matt Jordan
  • 2,133
  • 9
  • 10
  • that does not work because when I get the input from the user, it taking int "..." not "à" therefor I cannot even compare. BUT, within visual studio, funnyChar IS in fact à. checkFunny becomes "..." when the user inputs the à. Therefor the compare does not work.. – Robolisk Mar 10 '16 at 20:23
  • I was able to enter the extended-ascii character on the prompt line just fine using ALT+133, but the compiled code did not contain the correct character. It is using a different font, where instead of 133 (the correct character in the default console font), it is encoding it as 224 in the code (the correct character in most TrueType fonts). I assume the same is happening for you, possibly with a different font, but whatever you are using doesn't match the console where you are running this. Make sure you are using the correct values in both places, because these aren't standardized. – Matt Jordan Mar 10 '16 at 20:30
  • Yes, I can see what your talking about when I tested it myself. My other issue that comes with is I want to store the inputted ALT+133 code, but the compiler still see in the input stream being enter as "..." instead of à(ALT+133) when it is inputed with cin – Robolisk Mar 10 '16 at 20:42
  • ALT+133 and ALT+224 both print ... on the input prompt? can you get the ASCII code of what is being stored out of the string, by cout'ing (int)(checkFunny[0])? that might tell whether this is an input console problem? – Matt Jordan Mar 10 '16 at 22:01
0

Setting stdin and stdout mode to U16 will do the trick. Check the following code. (Note that setting the stdout is only required if you want to out the same character again.)

#include <iostream>
#include <string>
#include <io.h>
#include <fcntl.h>

int main()
{
    _setmode(_fileno(stdin), _O_U16TEXT);
    _setmode(_fileno(stdout), _O_U16TEXT);
    std::wstring funnyChar(L"à");
    std::wstring checkFunny;
    std::wcin >> checkFunny;
    if (checkFunny.compare(funnyChar) == 0)
        std::wcout << L"Yay" << std::endl;
    std::wcout << checkFunny << std::endl;
    return 0;
}
Thesane
  • 1,358
  • 10
  • 18