I can get the locale name of my system using the code below compiled in Visual Studio 2013. If I compile this same code in VS2015 I get nothing back! Is this a bug? How do you get your current system locale's name using VS2015 then?
#include "stdafx.h"
#include <iostream>
#include <locale>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << std::locale("").name().c_str() << endl;
}
In VS2015 they made it so the name if the locale is always equal to the argument you pass to the constructor (if it's valid):
// c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xlocinfo line 360
void _Construct(const string &_Str,
category _Cat)
{ // construct a locale with named facets
bool _Bad = false;
_Init();
if (_Cat != none)
{ // worth adding, do it
_TRY_BEGIN
_BEGIN_LOCINFO(_Lobj(_Cat, _Str.c_str()))
if (_Badname(_Lobj))
_Bad = true;
else
{ // name okay, build the locale
_Locimp::_Makeloc(_Lobj, _Cat, _Ptr, 0);
// The two lines below were added in VS2015
_Ptr->_Catmask = _Cat;
_Ptr->_Name = _Str.c_str(); // <--- Here they set the name forcefully
}
I believe you have to use setlocale() instead:
std::cout << setlocale(LC_ALL, "") << endl;
To have it in std::locale you can do
std::locale loc(setlocale(LC_ALL, ""));
This works in both VS2013 and VS2015.
Related
Sorry for what may sound simple, but I am trying to draw just a simple box in Visual Studio 2017 using the unicode characters from https://en.wikipedia.org/wiki/Box-drawing_character using the code below
#include <iostream>
using namespace std;
int main()
{
cout << "┏━━━━━━━━━━━━━━━━━┓" << endl;
cout << "┃" << endl;
and so on...
However, whenever I run it all of the above code simply outputs as a ? wherever there should be a line.
So is it possible to output code like this directly to the console or for each character do I have to write the numeric values for each character?
Windows console supports UTF-16LE UNICODE.
You can use some box-driving library like PDCurses for example.
Otherwise you can use the following approach
#include <windows.h>
#include <cwchar>
class output_swap {
output_swap(const output_swap&) = delete;
output_swap operator=(output_swap&) = delete;
public:
output_swap( ) noexcept:
prevCP_( ::GetConsoleCP() )
{
::SetConsoleCP( CP_WINUNICODE );
::SetConsoleOutputCP( CP_WINUNICODE );
}
~output_swap() noexcept {
::SetConsoleCP( prevCP_ );
::SetConsoleOutputCP( prevCP_ );
}
private:
::DWORD prevCP_;
};
void draw_text(const wchar_t* text)
{
static ::HANDLE _out = ::GetStdHandle(STD_OUTPUT_HANDLE);
::DWORD written;
::WriteConsoleW( _out, text, std::wcslen(text), &written, nullptr );
}
int main(int argc, const char** argv) {
output_swap swap;
draw_text(L"┏━━━━━━━━━━━━━━━━━┓\n");
draw_text(L"┃ OK ┃\n");
draw_text(L"┗━━━━━━━━━━━━━━━━━┛\n");
return 0;
}
Also check you console font, in the console settings. You are probably need a raster font, but this is also working for Consolas for example.
If you need console io streams, which can work with unicode as well as box driwing you can use my library
Windows console apps can output wide strings (L"...") directly to the terminal if the mode is set correctly. Note the use of wcout as well. Save the following source in UTF-8 encoding:
#include <iostream>
#include <io.h>
#include <fcntl.h>
using namespace std;
int main()
{
_setmode(_fileno(stdout), _O_U16TEXT);
wcout << L"┏━━━━━━━━━━━━━━━━━┓" << endl;
wcout << L"┃" << endl;
}
Compile with "cl /EHsc /utf-8 test.cpp". Output is:
┏━━━━━━━━━━━━━━━━━┓
┃
I am writing a simple program down below in C++ using Visual Studio 2015 and when I #include <iostream> I get a bunch of errors like in the image below:
My code can be found below and in the image.
// FirstProject.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
int main()
{
std::cout << "Hello World!";
int pause;
pause = 0;
std::cin >> pause;
return 0;
}
Code and Errors
The Errors came from Visual Studio 2015 not being able to properly locate the include files.
I am using the Visual Studio 2013 TR2 filesystem library. I am seeing a bug when converting a UNC path to a string:
#include "StdAfx.h"
#include <filesystem>
#include <iostream>
//------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
namespace fs = std::tr2::sys;
fs::path fsPath = "//server/dir";
std::string sPath = fsPath;
std::cout << sPath.c_str() << "\n";
}
This will output "\server\dir", not "\\server\dir".
Is there a fix or a workaround for this? Am I doing something wrong?
Well I found a workaround that works for me. If I use
sPath = fsPath.string();
I can now pass that string to the std::ifstream constructor. The path string will be "//server/dir" rather than "\\server\dir".
I'm pretty new to boost and want to use it's boost::locale to make suitable conversations to lower case. But I've got a link problem with boost::locale::generator. I linked my project with libboost_locale-mt.a and switched header search paths correctly, but it isn't enough for some reason. I'm using Xcode, and I got linking mistake on this code:
#include <boost/locale.hpp>
int main(int argc, const char * argv[])
{
using namespace boost::locale;
using namespace std;
generator gen;
std::locale loc = gen("");
locale::global(loc);
cout.imbue(loc);
cout << "This is lower case " << to_lower("Hello World!") << endl;
return 0;
}
I am still fairly new to NetBeans, and am writing code for class in C++. I am currently on my third project, and I have run into an error I can't seem to resolve when trying to compile+run my project. I have quadruple-checked my code, going so far as to copy code from a previous project. I have tried quiting, rebooting the computer, and starting NetBeans up again. I ran CppCheck on my code and it found no errors.
The error message:
build/Debug/MinGW-Windows/main.o: In function `main':
C:/Users/Martin/Documents/NetBeansProjects/Lab3/main.cpp:52: undefined reference to `Dictionary::Dictionary()'
C:/Users/Martin/Documents/NetBeansProjects/Lab3/main.cpp:52: undefined reference to `Dictionary::~Dictionary()'
I tried copying code from a previous project, and even with the exact same code as a previous project which works, it's still having this problem. Basically, the build is failing to recognize the Dictionary class.
What things can I check that might cause this problem? Any obscure (or even obvious) settings I can check? Should I just start a new project and copy my code over?
Edit: Adding main():
#include <cstdlib>
#include <iostream>
#include "Dictionary.h"
using namespace std;
/*
* argv[1] dictionary file
* argv[2] boggle board file
* argv[3] output file
*/
int main(int argc, char** argv) {
if (argc > 3) {
Dictionary dict;
dict.loadDictFile(argv[1]);
} else {
cout << "Not enough arguments. Needed: ./lab3 [dictionary file] "
"[board file] [output file]" << endl;
}
return 0;
}
And Dictionary.h:
#ifndef DICTIONARY_H
#define DICTIONARY_H
#include <string>
#include <set>
using namespace std;
class Dictionary {
public:
Dictionary();
Dictionary(const Dictionary& orig);
virtual ~Dictionary();
virtual void loadDictFile(char * fileName);
virtual bool find(string word);
private:
set<string> dict;
set<string> fullDictionary; // Contains all words, not just those 4+ char long.
};
#endif /* DICTIONARY_H */
And Dictionary.cpp:
#include "Dictionary.h"
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <set>
//using namespace std;
Dictionary::Dictionary() {
}
Dictionary::Dictionary(const Dictionary& orig) {
dict = orig.dict;
fullDictionary = orig.fullDictionary;
}
Dictionary::~Dictionary() {
}
void Dictionary::loadDictFile(char* fileName) {
ifstream infile;
infile.open(fileName);
if (infile) {
while(!infile.eof()) {
string line;
getline(infile, line);
fullDictionary.insert(line);
if (line.size() > 3) {
dict.insert(line);
}
}
} else {
cout << "Dictionary File not loaded: " << fileName << endl;
}
}
bool Dictionary::find(string word){
if (dict.find(word) != dict.end()) {
return true;
} else {
return false;
}
}
Found my problem. Netbeans didn't consider the Dictionary class to be part of my project, so it wasn't compiling Dictionary.cpp. I added it in the Project window by right-clicking the Source Files folder and using Add existing item... menu option. Now it compiles fine.
Does anyone know why the class wouldn't be added if I used Netbean's New File interface and added to the project specifically?