1

I want to make an calculator in a spoken language another than English e.g. Hindi. I tried to make one using '''cout''' function but it gave me unexpected errors. Please Tell what should I do

Code Sample

cout<<name<<", "<<"कृपया ऑपरेटर दर्ज करें--> ";
cin>>op;
switch (op)
{
    case '+':
     cout<<name<<", "<<"कृपया पहला नंबर दर्ज करें--> ";
     cin>>x;
     cout<<name<<", "<<"कृपया दूसरा नंबर दर्ज करें--> ";
     cin>>y;

Result:

    कृपया ऑपरेटर दर्ज करें--> +
कृपया पहला नंबर दर्ज करें-->
कृपया दूसरा नंबर दर्ज करें-->

3 Answers3

1

I am not familiar with Hindi font used in console, but you need to do the following things:-

  • Set the correct code page using std::locale OR use unicode (for examplestd::wstring and std::wcout).
  • Set your console to a font that can display those characters.

I would suggest you to find the character set Hindi, like English has ASCII (classification ISO646 series).

You can find more info about wstring and wcout , here posted by a fellow user.Remember to save the code file to UTF8 (or in the correct locale) otherwise the characters will simply 'disappear' from the code.

Abhishek Dutt
  • 1,308
  • 7
  • 14
  • 24
0

For Display output in Hindi or any none language, we Firstly need to set UTF encoding to output encoding

#include <string>
#include <iostream>
#include <Windows.h>

int main() {
    std::string test = u8"Greek: αβγδ; German: Übergrößenträger";
    SetConsoleOutputCP(65001);
    std::cout << test;
}
Vaibhav Mahajan
  • 222
  • 1
  • 5
0

You can use setlocale function in C++. You can include locale.h header (if you need more C specific functionality, this functionality can give possibility to play with currency, language and so on). Example code:

include <locale.h>

int main(){
    setlocale(LC_ALL,"en_US.utf8");
    
    // Your code
    
    return 0;
}

Explanation: setlocale function sets locale information to be used by the current program instead of changing whole locale. setlocale function accepts two parameters: 1. category (LC_ALL); 2. locale ("en_US.utf8"). Locales can be different. Reference: setlocale in c++ (reference) This function helped me a lot in my projects.