views:

73

answers:

2
#include <iostream>
#include <string>
using namespace std;

string mystring1, mystring2, mystring3 = "grové";

int main(){
  mystring1 = "grové";
  getline( cin, mystring2 );  //Here I type "grové" (without "")
  cout << "mystring1= " << mystring1 << endl;
  cout << "mystring2= " << mystring2 << endl;
  cout << "mystring3= " << mystring3 << endl;
  return 0;
}

The output of above code is:

mystring1= grov8
mystring2= grové
mystring3= grov8

although when I cut and paste the code here it comes as:

mystring1= grovΘ
mystring2= grové
mystring3= grovΘ

Why does the content of mystring2 differ from mystring1 and mystring3?

+5  A: 

Assuming you use Microsoft Windows: Your source code has a different encoding from that of the windows command line.

Type chcp in the command line to see the current console codepage. (Mine is 850)

You have three options:

  • Change the codepage/encoding of your source code to the codepage/encoding of your console.
  • Change the codepage/encoding of the console to the codepage/encoding of your source file.
  • Use a library (or Windows API) to change the encoding on the fly.
Sofahamster
A: 

Windows console has cp 866 (on may cyrilic) encoding and you file has other (UTF-8 or CP1251(my cyrilic)).

You may use Win API functions to solve this problem

den bardadym