I'm trying to output Russian words in Visual Studio 2008. I wrote:
#include <iostream>
#include <locale.h>
using namespace std;
void main()
{
setlocale(LC_ALL,"rus");
cout << "Я хочу видеть это по-русски!";
}
When I pressed Ctrl+S to save the file in Visual Studio, I received the following prompt:
Some Unicode characters in this file could not be saved in the current codepage. Would you like to save it in unicode?
I chose the option "Save with other Encoding" and selected "Cyrillic (Windows) - Codepage 1251." However, when I run my program, the console output appears as follows:
???????? ??? ????
The problem is that you aren't using Unicode characters set (wstring, wcout as #
Bình Nguyên have already mentioned). There are two ways to solve this problem:
1. Use unicode characters set.
2. Go to Control Panel -> Region and Language -> Administrative Tab -> Language for non-Unicode programs -> Change system locale... -> choose Russia.
Related
First, briefly,
My computer with Window11OS showed this console window.
?? ? ?? C++????
C:\Users\MYNAME\source\repos\20220816ConsoleApplication1\x64\Debug\20220816ConsoleApplication1.exe (process 17604) exited with code 0.
and quetionmarks are NOT what I intended.
I think I need to use intentionally UNICODE here.
But I don't know how to do, I failed once; I'll explain later.
So, this is the code that I want to fix. And I'm using Visual Studio as Windows Console Application(C++/WInRT) for this solution-file.
#include <iostream>
int main()
{
using namespace std;
/*표준 출력 스트림으로 문장을 출력함
근데 왜 한글이 물음표로 나올까*/
cout << "나의 첫 번째 C++프로그램" << endl;
return 0;
}
But VS's error-list showed me the below.
Warning C4566 character represented by universal-character-name '\uB098' cannot be represented in the current code page (1252)
What property I've changed to build successfully was the below
VS's upper menu > Project > Properties(the lowest item) > Configuration Properties > C/C++ > Precompiled Headers > Precompiled Header : Use(/Yu) --I CHANGED IT TO-->> Not Using Precompiled Headers
I also had tried to change "Language" property just like the screenshot below, but I had failed to build at all, even question-marks hadn't shown.
What I had tried to show Hangeul-language instead of question-marks but failed.
On Windows, use the following to set the console stdout to Unicode mode. Save the file with UTF-8 w/ BOM encoding, or UTF-8 without BOM and compile with the /utf-8 switch. You Windows needs to support the Korean language or at least use a console font that supports Korean characters such as NSimSun.
test.cpp: compiled with "cl /W4 /EHsc /utf-8 test.cpp"
#include <iostream>
#include <io.h>
#include <fcntl.h>
int main()
{
using namespace std;
_setmode(_fileno(stdout), _O_U16TEXT);
wcout << L"나의 첫 번째 C++프로그램" << endl;
return 0;
}
Output (cut/paste from cmd window):
나의 첫 번째 C++프로그램
How to print "👩" emoji (Unicode code 1F469) in Windows console app using C++?
In example below I followed Printing UTF-8 Text to the Windows Console.
#include <iostream>
#include <io.h>
#include <fcntl.h>
int main()
{
_setmode(_fileno(stdout), _O_U16TEXT);
std::wcout << L"face: 👩\n";
return 0;
}
However it only prints two questionmarks:
.
"Command Prompt" (cmd.exe) app can't render this char so I'm using Windows Terminal that can render it:
The Windows Console cannot display characters outside of Plane 0. The Windows Terminal was designed to improve on the limitations of the Windows Console.
Further reading: How to use unicode characters in Windows command line?
I want it to run just in the cmd but all I get is this white window. Any help?
Code:
//
// pch.cpp
// Include the standard header and generate the precompiled header.
//
#include "pch.h"
#include <iostream>
using namespace std;
int main() {
cout << "test";
return 0;
}
Visual Studio provides different kinds of project you can start with.
You are looking for "Win32 Console Application" which already provides the standard configuration that employs a console.
You can also use an empty C++ project, but then you must find all the required settings that enable the console environment.
The article Unicode apps in the MinGW-w64 wiki explains the following example for an Unicode application, e.g. _main.c_:
#define UNICODE
#define _UNICODE
#include <tchar.h>
int _tmain(int argc, TCHAR * argv[])
{
_tprintf(argv[1]);
return 0;
}
The above code makes use of tchar.h mapping, which allows it to both compile in Unicode and non-Unicode mode. [...] The -municode option is still required when linking if Unicode mode is used.
So I used
C:\> i686-w64-mingw32-gcc main.c -municode -o hello
_tmain.c:1:0: warning: "UNICODE" redefined
#define UNICODE
^
<command-line>:0:0: note: this is the location of the previous definition
to compile a Unicode application. But, when I run it, it returns
C:\> hello Süßer
S³▀er
So the Unicode string is wrong. I used the latest version 4.9.2 of MinGW-w64, i686 architecture and tried the Win32 and POSIX theads variants, both result in the same error. My operating system is 32-bit German Windows 7. When I used the Unicode codepage (chcp 65001), I have to use the font "Lucida Console". With this setting I get a similar error:
C:\> hello Süßer
S��er
I want to use a parameter with "ü" or "ß" in a Windows C++ program.
Solution
nwellnhof is right: The problem is the output on the console. This problem is explained in Unicode part 1: Windows console i/o approaches und Unicode part 2: UTF-8 stream mode. The latter gives a solution for Visual C++ - it worked also with Intel C++ 15. This blog post does "not yet consider the g++ compiler. All this code is Visual C++ specific. However, [the blog author has] done generally the same with g++, and [he] will probably discuss that in a third installment."
I want to open a file, which name is given by a parameter. This works simple, e. g. main.c:
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[])
{
if ( argc > 1 ) {
// The output will be wrong, ...
cout << argv[1] << endl;
// but the name of this file will be right:
fstream fl_rsl(argv[1], ios::out);
fl_rsl.close();
}
return 0;
}
and the compilation without unicode mode
C:\> g++ main.cpp -o hello && hello Süßer
It's console output is still wrong, but the created filename is right. This is okay for me.
I have an issue with Visual Studio's 2012. I am also using "Sams Teach Yourself C++ in One Hour a day, 7th edition".
After using google to find the "best" compilers for C++, Visual Studios seemed to be the tool of choice.
So I downloaded and installed Visual Studios 2012. The very first lesson in the book is (and tells me to run it as a console app by going to File > New > Project >Visual C++ > Win32 > Console Application )
#include <iostream>
int main()
{
std::cout << “Hello World!” << std::endl;
return 0;
}
which doesnt work, at all. it outputs an error message similiar to the following:
1>c:\users\nik\documents\visual studio
2012\projects\consoleapplication4\consoleapplication4\consoleapplication4.cpp(8):
error C2065: '“Hello' : undeclared identifier
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =========="
(there is more lines similiar to the first, but its rather long)
However, after googling and watching a video on youtube the following code works (using File > New > Project >Visual C++ > General > Empty Project )
#include <iostream>
#include "conio.h"
using namespace std;
int main() {
cout << "Hello Nik" << endl;
_getch();
return 0;
}
Does Visual Studio's 2012 have a C++ compiler? or does it just have a visual c++ compiler (if thats even the issue, only reason I think it could be is I can see templates for Visual C++ but none for c++ by itself...) or do I need to download Visual Studio Express to download native c++ ??
Any help would be greatly appreciated as I am feeling some-what out of my depth here...
Thanks.
Besides aphostrophes you may need to disable precompiler headers in project properties.
They are turned on by default in VS2012. If you are not familiar with precompiled headers turn them off.
Right click on project (not solution)
Click properties.
Expand "Configuration properties"
Expand "C/C++"
Choose "Precompiled headers"
Set "Precompiled header" to "Not Using Precompiled Headers"
More information about precompiled headers and stdafx.h file at Wikipedia
The apostrophes you used are wrong:
“Hello World!”
should be
"Hello World!"
Notice even how SO recognizes the difference. You should at least type the code you see in the book instead of copying and pasting it. ;-)
The Win32 console application is actually quite different from the empty project. Win32 utilize a message (input) queue which you poll in a loop and your program respectively utilizes the Win32 API and performs certain operations.
The empty project is a bit less dependent on Win32 or anything that Windows provides in terms of API unless you make it dependent on it. This would be the simples hello world app in you empty project:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
return 0;
}
Just try this::
"Hello World!" instead of “Hello World!”.
The difference between
“Hello World!” and
"Hello Nik" is the apostrophe.
Aslo is the error persists than just check visual c++ library linker.
There is aslo definitely no need for conio.h
If your going to copy from a book at least copy it correctly.
Using namespace std;
would be pretty smart in this case.
In order to fix your error you have to delete std:: of std::cout and std::endl, and write using namespace std; underneath #include iostream and and change “ ” with " ".
#include <iostream>
using namespace std;
int main()
{
cout <<"Hello World" << endl;
return 0;
}
In Visual studio 2012
file>new projet>visual c++ (Project win32)>application settings(application console+Not Using Precompiled)>in right box in you Project (right click, add>new element>file c++).