I can't use accents with string in C++ - c++

I can set the accents with SetConsoleOutputCP(1252) or locale::global(locale"FR-fr"), but I can't use it with strings. Seems like it's one or the other. I can output text with accents, or I can output the string with accents, not both. Any ideas?
The code below can be used to reproduce the problem. Simply add locale::global(locale"FR-fr") or SetConsoleOutputCP(1252):
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
int main()
{
string mot;
cout << "Inscrivez un mot avec des accents (é ou è): ";
cin >> mot;
cout << mot;
}

I suggest that you set both the input and output code pages:
SetConsoleCP(1252); // input
SetConsoleOutputCP(1252); // output

Related

c++ . Reading txt and outputting by console using Spanish letters

I have a problem reading a txt file and getting it out of the console. I need to use Spanish letters. The txt file is encoded in "utf-8 with BOM"
This is text of the texInput.txt file:
Hello. Look how it shows the letter " ñ ",
and the letters with tilde: "á é í or ú"
I want to process texts in Spanish.
Please help.
Here the main.cpp code:
#include <fstream>
#include <iostream>
#include <string>
#include <locale.h>
using namespace std;
wstring inputStr;
wchar_t wc;
wifstream te;
void openTxt();
void readTxt();
void closeTxt();
void wait();
int main(){
setlocale(LC_ALL,"Spanish");
openTxt();
readTxt();
closeTxt();
wait();
return 0;
}
void openTxt(){
te.open("texInput.txt",ios::in);
if (te.fail()){
cout<<"No se pudo abrir el archivo de texto";
exit(1);
}
}
void readTxt(){
while(te.good()){
te.get(wc);
inputStr.push_back(wc);
}
wcout<<inputStr<<endl;
}
void closeTxt(){
te.close();
}
void wait(){
wcout<<L"type something to exit: "<<endl;
wcin>>wc;
}
This is what it looks like on the console:
Hello. Look how it shows the letter " ñ ",
and the letters with tilde: "á é í or ú"
I want to process texts in Spanish.
Please help.
type something to exit:

Is there a simple way to print out special characters from a file to console in c++?

I have a problem where I am attempting to add appropriate articles to the beginning of French country names. That part wasn't an issue but trying to print it out hasn't worked so well. There are special characters in the file and while it seems to read them just fine it won't print them out just fine. I am using visual studios and here are examples of words from the text file named "FrenchCountriesUnfinished.txt" that I am trying to print out
l'Algérie,
la Norvège,
la Zélande,
la Suède,
le Zaïre
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream file("FrenchCountriesUnfinished.txt");
string str;
while (getline(file, str)) {
cout << str << endl;
}
return 0;
}

Why string receives numbers in this while loop?

A string is a variable-length sequence of characters. Why does it receive anything and prints it out?
#include <iostream>
#include <string>
using namespace std;
int main (){
string word;
while (cin >> word){
cout << word << endl;
}
return 0;
}
In this program, we read into a string, not an int. How can I fall out of this while loop i.e hit an invalid input?
Reading into a string will not fail, all input is valid. You may add any validation you like once the string is read.
Your question is a little vague, but if you're asking how to end the loop you can do it with an end-of-file. On Linux this you can generate one from the console with Control-D, and on Windows with Control-Z plus Enter.
Because you are taking the input in a string and string is a sequence of characters .so it takes anything you input from the keyboard either it is number or alphabet or any special character .
How can I check for invalid input?
If you could define what you consider to be "invalid input" you can filter for it in one of the std::string helper methods. In your example you eluded to numbers not being strings... so if you want to do something with pure numbers...
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main (){
string word;
while (cin >> word){
bool isNumber = (word.find_first_not_of("0123456789") == std::string::npos);
if (isNumber){
cout << "it's a number! " << word << endl;
}else{
cout << word << endl;
}
}
return 0;
}

wcin gets wrong character input?

The following code:
#include <iostream>
using std::wcin;
using std::wcout;
using std::locale;
int main()
{
locale::global(locale("Portuguese_Brazil"));
wcout << "wcin Test using \"ção\": "; // shows that wcout works properly
wchar_t wcinTest[] = L"";
wcin >> wcinTest;
wcout << wcinTest << " should be \"ção\".";
return 0;
}
Results in:
wcin Test using "ção": ção
╬Æo should be "ção".
The ╬ character is U+2021 or 8225, and the ç is U+00E7 or 231.
I changed mult-bytes option, set and not set UNICODE in project properties. Nothing worked.
I already set the console font into Consolas, a true type font capable of displaying the ç character correctly.
I'd like this as simple and reproducible possible to use as a standard practice for future UNICODE console applications.
Any ideas?
wcinTest is a wchar_t buffer of length 1;
You overflow it when you read into it. Use a std::wstring insead.
This finally worked:
#include <iostream>
#include <string>
#include <Windows.h>
using std::cin;
using std::cout;
using std::string;
int main()
{
SetConsoleOutputCP(1252);
SetConsoleCP(1252);
cout << "wcin Test using \"ção\": "; // shows that wcout works properly
string wcinTest;
cin >> wcinTest;
cout << wcinTest << " should be \"ção\".";
return 0;
}
I'm too newbie to understand why I need both SetConsoleOutputCP and SetConsoleCP. I though maybe just SetConsoleCP would fix everything, but no, I need both: SetConsoleOutputCP fixed cout; and SetConsoleCP fixed cin.
Thanks anyway #StoryTeller

the output is not what i typed.It is --> (畳慨汩朠灵慴찀쳌쳌쳌)

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
char x[20];
cout << "enter something\n";
cin.getline(x,20);
ofstream o("d:/tester.txt");
//o.write( (char*)&x , sizeof(x) );
for(int i = 0 ; i<=19 ; i++ ) {
o.put(x[i]);
}
}
I am not getting that output in the file the one which i enter during program . for eg. the output is 畳慨汩朠灵慴찀쳌쳌쳌 on writing suhail gupta.
What is the problem with the code ? Even when i use o.write( (char*)&x , sizeof(x) ); (the commented statement) i get the same output.
What is the reason?
Your program involves undefined behavior. The x array is not fully initialized and you read from the uninitialized indices. Besides, you always write 20 bytes, independent of what you read from the user.
I guess you use some text editor like Notepad. The latter has bugs when trying to guess the encoding. It appears that it guesses the file is UTF16 and displays 20/2 == 10 characters instead.
To solve the problem, store to the file exactly the number of characters entered by the user. Use std::string to make it easier.
Edit: The C++ way:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
string x;
cout << "enter something\n";
getline(cin, x);
ofstream o("d:/tester.txt");
o << x;
}